<?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>SpringMVCDemo2LoginAndFile</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>*.do</url-pattern>
</servlet-mapping>
<!-- 乱码解决 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
```
4、创建Spring配置文件applicationContext.xml,放到WEB-INF/config/目录下
使用注解方式配置
multipartResolver: 文件上传配置
``` 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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射,解决@ResponseBody乱码问题, 需要在annotation-driven之前,否则乱码问题同样无法解决 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!--
向 spring 容器中注册 DefaultAnnotationHandlerMapping。
向 spring 容器中注册 AnnotationMethodHandlerAdapter。
配置一些 messageconverter。
解决了 @Controller 注解的使用前提配置,即 HandlerMapping 能够知道谁来处理请求。
-->
<mvc:annotation-driven />
<context:component-scan base-package="com.xhay1122.controller" />
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
```
5、业务逻辑控制器
*登陆:LoginController.java*
``` bash
package com.xhay1122.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/loginController")
public class LoginController{
@RequestMapping(value={"/login"})
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)){
System.out.println("登陆成功");
//将页面需要使用的数据保存
//设置视图名称
mav.setViewName("redirect:/index.jsp");
}
else{
System.out.println("登陆失败");
mav.addObject("error", "用户名或密码错误");
mav.setViewName("/login");
}
return mav;
}
}
```
*补充:*
Spring用ModelAndView实现forward、redirect。
forward是服务器内部重定向,程序收到请求后重新定向到另一个程序,客户端并不知道。浏览器地址栏的URL并不会改变。
redirect则是服务器收到请求后发送一个HTTP状态头给客户,让客户将再请求一次,这里多了两次网络通信的来往。浏览器地址栏的URL会改变。
若用Servlet的api,是如下的方式来实现
forward: request.getRequestDispatcher(“/somePage.jsp”).forwardrequest, response);
redirect: response.sendRedirect(“/somePage.jsp”);
spring web framework利用ModelAndView也能实现forward、redirect
``` bash
forward: return ModelAndView(“forward:/somePage”);
redirect: return ModelAndView(“redirect:/somePage”);