<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVCDemo1</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/config/applicationContext*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
```
4、创建Spring配置文件applicationContext.xml,放到WEB-INF/config/目录下
Bean Name:被请求地址
Bean Class : 用来处理请求的控制器
``` bash
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="/login.html" class="com.xhay1122.controller.LoginController" scope="prototype" />
</beans>
```
5、业务逻辑控制器LoginController.java
实现业务逻辑控制器的方式主要四种(本教程使用的是第一种方式).
1).实现Controller接口 org.springframework.web.servlet.mvc.Controller
2).继承AbstarctController抽象类 org.springframework.web.servlet.mvc.AbstractController
3). MultiActionController org.springframework.web.servlet.mvc.multiaction.MultiActionController
4).通过注解快速实现业务逻辑控制器 @Controller配合@RequestMapping实现
``` bash
package com.xhay1122.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* 项目名称:SpringMVCDemo1
* 类名称:LoginController
* 类描述:
* 创建人:xhay
* 创建时间:2015年4月23日 下午4:09:56
* 修改人:xhay
* 修改时间:2015年4月23日 下午4:09:56
* 修改备注:
* @version 1.0
* 软件工程创新实验室
*/
public class LoginController implements Controller{
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//获取表单提交数据
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
//创建模型视图对象用来返回
ModelAndView mav = new ModelAndView();
if("admin".equals(userName) && "admin".equals(userPwd)){
//将页面需要使用的数据保存
mav.addObject("currentUserName", userName);
//设置视图名称
mav.setViewName("/index.jsp");
}
else{
mav.addObject("error", "用户名或密码错误");
mav.setViewName("/login.jsp");
}
return mav;
}
}
```
6、登录页面
``` bash
<%@ 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=ISO-8859-1">
<title>用户登录</title>
</head>
<body>
${error }
<form action="login.html" method="post">
用户名:<input type="text" name="userName" /><br />
密 码:<input type="text" name="userPwd" /><br />
<input type="submit" value="登陆" />
</form>
</body>
</html>