一、SpringMVC概述:
Spring MVC工作在控制層, 替代的是servlet
Servlet不好的地方:
1). 一個請求對應一個servlet, servlet類的數量特別多。
2). 處理請求參數麻煩
request.getParameter()
request.getParameter()
User u = new User();
u.setxx();
u.setxxx();
MyService.addUser(u);
3). 響應
1. 請求重定向
2. 請求轉發
3. 返回的ajax
處理對象向json字符串的轉換
處理json類型的response流程
4). 文件上傳
5). 其他。。。
二、 Spring MVC環境搭建(注解方式)
1). 添加jar包
2). 編寫springmvc.xml
~~~
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!--配置注解形式的處理器映射器和處理器適配器-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--配置包掃描-->
<context:component-scan base-package="com.neuedu.controller"></context:component-scan>
<!--配置視圖解析器,不配置,使用默認的-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<!-- <property name="suffix" value="jsp"></property>-->
</bean>
~~~
3). 編寫web.xml
~~~
<!-- 配置spring mvc的前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
~~~
4). 編寫控制器
~~~
@Controller
@RequestMapping("/test")
public class ActiveController {
@RequestMapping("/test")
public ModelAndView getActive()
{
}
}
~~~
5). 在springmvc.xml中配置靜態資源
~~~
<!-- 配置靜態資源 -->
<!-- <mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/html/" mapping="/html/**"/>
<mvc:resources location="/upload/" mapping="/upload/**"/>
<mvc:resources location="/fonts/" mapping="/fonts/**"/> -->
<!-- 如果路徑映射不到controller上,采用服務器默認查找方式 -->
<mvc:default-servlet-handler/>
~~~
6 ). 將資源放在WEB-INF下,修改相應的配置
~~~
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>
<mvc:resources location="/WEB-INF/img/" mapping="/img/**"/>
<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>
<mvc:resources location="/WEB-INF/html/" mapping="/html/**"/>
<mvc:resources location="/WEB-INF/upload/" mapping="/upload/**"/>
<mvc:resources location="/WEB-INF/fonts/" mapping="/fonts/**"/>
~~~