配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
RequestMapping注解
作用范围:类或者方法上(作用于类上,类中的方法必须为类中映射的子路径下)
属性:path和value作用一样指定当前路径,
method限制方法的请求方式
params请求中必须包含的参数名或者请求中必须包含参数的名和值
headers头请求必须包含的值
请求参数的绑定
绑定基本类型,javabean类型,集合
集合
用户名字:<input type="text" name="list[0].username"/>
用户年龄:<input type="text" name="list[0].age"/>
用户名字:<input type="text" name="map['one'].username"/>
用户年龄:<input type="text" name="map['one'].age"/>
请求中参数与控制器中方法参数名一致
出现乱码配置过滤器
<!-- 拦截器-->
<filter>
<filter-name>characterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
自定义类型转换器
public class StringtoDateConverts implements Converter<String , Date> {
@Override
public Date convert(String s) {
if (s == null)
{
throw new RuntimeException("未输入");
}
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parse(s);
} catch (ParseException e) {
throw new RuntimeException("转换错误");
}
}
}
实例代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="user/hello?username=heh">first hello </a>
<form action="user/save" method="get">
姓名:<input type="text" name="username"/>
密码:<input type="text" name="password"/>
金额:<input type="text" name="money"/>
用户名字:<input type="text" name="list[0].username"/>
用户年龄:<input type="text" name="list[0].age"/>
用户名字:<input type="text" name="map['one'].username"/>
用户年龄:<input type="text" name="map['one'].age"/>
<input type="submit" value="提交"/>
</form><br/><br/><br/>
<form action="user/saveAccount" method="get">
姓名:<input type="text" name="username"/>
年龄:<input type="text" name="age"/>
日期:<input type="date" name="date"/>
<input type="submit" value="提交"/>
</form>
<a href="user/test">first hello </a>
</body>
</html>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</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>/</url-pattern>
</servlet-mapping>
<!-- 拦截器-->
<filter>
<filter-name>characterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springmvc.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 开启注解扫描-->
<context:component-scan base-package="com.zheng"/>
<!-- 开启视图解析-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 开启springMVC框架注解分析-->
<mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
<bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.zheng.utils.StringtoDateConverts"/>
</set>
</property>
</bean>
</beans>
控制器
@Controller
@RequestMapping(path = "/user")
public class HelloController {
@RequestMapping(path = "/hello", method = {RequestMethod.GET}, params = {"username"},headers = {"Accept"})
public String sayHello(String username)
{
System.out.println("Hello SpringMVC"+username);
return "success";
}
@RequestMapping(path = "/save", method = {RequestMethod.GET}, params = {"username"},headers = {"Accept"})
public String save(Account account)
{
System.out.println("Hello SpringMVC"+account);
return "success";
}
@RequestMapping(path = "/saveAccount", method = {RequestMethod.GET}, params = {"username"},headers = {"Accept"})
public String saveAccount(Account2 account)
{
System.out.println("Hello SpringMVC"+account);
return "success";
}
// 获取原生API
@RequestMapping(path = "/test", method = {RequestMethod.GET}, headers = {"Accept"})
public String test(HttpServletRequest request, HttpServletResponse response)
{
System.out.println("Hello SpringMVC");
System.out.println(request);
HttpSession session = request.getSession();
System.out.println(response);
return "success";
}
}
常用注解
@RequestParam(value = "username")用于方法参数,解决传入参数和方法参数名不一致封装不上的问题。
@RequestBody 用于获取请求体
@PathVariable(name=“id”)用于rest方
@RequestHeader(value=“Accept”)用于获取指定的头部信息
@CookieValue(value=“JSSIONID”)获取Cookie的值
有返回值,为了给对象封装,作用于调用的方法
@ModelAttribute 作用于调用方法之前有两种方式,有返回值和无返回值。
有返回值作用于方法是指,封装了对象后将返回值给后面的调用方法。
无返回值作用于方法是指,参数中要提供一个map<String , User> 将对象封装到map中的,然后调用的方法就可以在参数中使用注解根据map中的key值调用User对象
public String test(HttpServletRequest request, HttpServletResponse response,@ModelAttribute("abc") User user)
@SessionAttributes注解作用于类上,会将requst域中的值存储到session中
Model对象是一个map,数据会存储到request域中。model.addAttribute(String,Object)存储,modlMap.get(String)取出对象,清除session中的值SessionStatus.setComple()清楚session中的值
网友评论