springmvc的介绍:
和struts一样是一个轻量级的web框架,但是springmvc基于注解,更加简单方便,所以取代了stuts。
springmvc的原理:
1、Http请求:客户端请求提交到DidpatcherServlet
2、寻找处理器:由DispatcherServlet控制器查询一个或多个HandleMapping找到处理请求的Controller
3、调用处理器:DispatcherServlet将请求提交到Controller
4、调用业务处理和返回结果:Controller调用业务逻辑处理后,返回ModelAndView.
5、处理视图映射并返回模型:DispatcherServlet查询一个或多个ViewResoler视图,找到ModelAndView指定的视图。
6、Http响应:视图负责将结果显示到客户端
常用注解:
1、@RequestMapping(用在类上或者方法上可以作为匹配url,当用rest风格时候,还可以绑定访问方式)
2、@RequestParam(用于当前台传来的参数与方法中的形参名不一致时,放在形参前面,统一参数名)
3、@RequestBody(接收前台传来的json格式的数据)。
4、@ResponseBody(以json格式返回数据到前台)
5、@Controller(放在类上面)
入门:
1、配置springmvc.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置Spring MVC自动扫描的路径 -->
<context:component-scan base-package="com.hsia.springmvc" />
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath">
<value>/view/</value>
</property>
</bean>
<bean id="htmlviewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".html" />
<property name="order" value="0"></property>
<property name="contentType" value="text/html;charset=UTF-8"></property>
</bean>
<bean id="stringConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
</beans>
2、web.xml
<?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>springmvc-param4</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>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup><!-- 启动时自动加载配置文件 -->
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>
org.springframework.web.filter.HiddenHttpMethodFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、java测试类
package com.hsia.springmvc.action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("action")
public class TestController{
@RequestMapping(value="user",method=RequestMethod.POST)
public String addUser() {
System.out.println("add ");
return "index";
}
@RequestMapping(value="user",method=RequestMethod.DELETE)
public String deleteUser() {
System.out.println("delete ");
return "index";
}
@RequestMapping(value="user",method=RequestMethod.GET)
public String queryUser() {
System.out.println("query ");
return "index";
}
@RequestMapping(value="user",method=RequestMethod.PUT)
public String UpdateUser() {
System.out.println("update ");
return "index";
}
}