我认为servlet中的某个地方存在错误。 从索引链接到用户时会导致404错误。 如果你发现它,请告诉我。
项目结构 -
web.xml -
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>SQL Manager</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml -
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.dubinets.app"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Database Information -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.dubinets.app.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--UserDao and UserService beans-->
<bean id="userDao" class="com.dubinets.app.dao.UserDaoImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
</bean>
<bean id="userService" class="com.dubinets.app.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<context:component-scan base-package="com.dubinets.app"/>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
</bean>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
index.jsp -
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>SQl Manager</title>
</head>
<body>
<a href="<c:url value="/users"/>" target="_blank">Users list</a>
</body>
</html>
users.jsp -
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ taglib prefix="from" uri="http://www.springframework.org/tags/form" %>
<%@ page session="false" %>
<html>
<head>
<title>Users Page</title>
</head>
<body>
<a href="../../index.jsp">Back to main menu</a>
<br/>
<br/>
<h1>User List</h1>
</body>
</html>
UserController.java -
import com.dubinets.app.model.User;
import com.dubinets.app.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
public class UserController {
private UserService userService;
@Autowired(required = true)
@Qualifier(value = "userService")
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping(value = "users", method = RequestMethod.GET)
public String listUsers(Model model){
model.addAttribute("user", new User());
model.addAttribute("listUsers", this.userService.listUsers());
return "users";
}
@RequestMapping(value = "/users/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute("user") User user){
if(user.getId() == 0){
this.userService.addUser(user);
}else {
this.userService.updateUser(user);
}
return "redirect:/users";
}
@RequestMapping("/remove/{id}")
public String removeUser(@PathVariable("id") int id){
this.userService.removeUser(id);
return "redirect:/users";
}
@RequestMapping("edit/{id}")
public String editUser(@PathVariable("id") int id, Model model){
model.addAttribute("user", this.userService.getUserById(id));
model.addAttribute("listUsers", this.userService.listUsers());
return "users";
}
@RequestMapping("userdata/{id}")
public String userData(@PathVariable("id") int id, Model model){
model.addAttribute("user", this.userService.getUserById(id));
return "userdata";
}
}
的index.jsp
点击用户列表(users.jsp)