在login.jsp用户中输入用户名和密码,如果用户名或密码不正确,则将错误消息回显至$ {error}。但是当我键入登录按钮时(http://localhost:8080/project_name/login.jsp),it导航到“ http://localhost:8080/project_name/login”,并显示找不到资源[/ project_name / login]。WebContent / login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Login</title>
</head>
<body>
<form action="login" method="post">
<table>
<tr>
<td>username: </td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>password: </td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="login"></td>
<td></td>
</tr>
</table>
</form>
<font color="red">${error}</font>
</body>
</html>
WebContent / WEB-INF / web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>dbquery</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
src / com / wise / controller / LoginController.java
@Controller
public class LoginController {
@Autowired
LoginService service;
@Autowired
HttpServletRequest request;
@RequestMapping(value="/login")
public ModelAndView doLogin() {
String loginUrl = "/login.jsp";
String successUrl = "/WEB-INF/jsp/query.jsp";
String username = request.getParameter("username");
String password = request.getParameter("password");
return service.doLogin(loginUrl, successUrl, username, password);
}
}
在WebContent / WEB-INF / web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>dbquery</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dbquery</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dbquery</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
并添加新的dbquery-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context.xsd">
<mvc:annotation-driven />
<context:component-scan
base-package="com.wise.controller" />
<mvc:default-servlet-handler />
</beans>