1.描述
1.1.数据绑定
SpringMVC通过URL Mapping注解进行URL映射和数据传输.
主要有:
- @RequestMapping 通用绑定,可以放在类上面作为全局路径使用
//控制器注解
@Controller
//配置该Controller下的全局路径
@RequestMapping("/test")
public class TestController...
- @GetMapping Get请求绑定
- @PostMapping Post请求绑定
1.2.接收请求参数
- 使用Controller的方法请求参数接收
- 使用Map接收(不支持数据类型),参数前要使用@RequestParam
- 使用JavaBean接收
1.3.URI绝对路径与相对路径

相对地址案例
-
<form action="./apply" method="post">
-
页面地址:http://localhost:8080/[上下文路径]/form.html
-
提交地址:http://localhost:8080/[上下文路径]/apply
1.4.关联对象赋值
- 表单中有用户信息以及用户联系人信息
- JavaBean中则需要把联系人对象作为用户属性,并且表单中联系人信息要这样表示
<form ...>
<input name="username"/>
...
<input name="contract.username">
...
</form>
JavaBean中这样编写
public class User {
private String username;
...
private Contract contract;
...
}
public class Contract {
private String username;
...
}
1.5.日期类型转换
- 可以在参数前使用@DateTimeFormat(pattern = "yyyy-MM-dd")
- 在JavaBean属性前使用@DateTimeFormat(pattern = "yyyy-MM-dd")
- applicationContext.xml中配置conversionService日期转换器
<!--配置日期转换器-->
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.vicent.springmvc.controller.converter.MyDateConverter"/>
</set>
</property>
</bean>
并且编写转换器类
package com.vicent.springmvc.controller.converter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyDateConverter implements Converter<String, Date> {
public Date convert(String s){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
try {
Date date = simpleDateFormat.parse(s);
return date;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}
1.6.中文乱码问题
- Get请求乱码 -- server.xml增加URIEncoding属性
打开tomcat->conf->server.xml文件

找到此段代码添加URIEncoding="UTF-8"

- Post请求乱码 -- web.xml配置CharacterEncodingFilter
<filter>
<filter-name>characterFilter</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>characterFilter</filter-name>
<!--拦截所有路径-->
<url-pattern>/*</url-pattern>
</filter-mapping>
- Response响应乱码 -- Spring配置
StringHttpMessageConverter
<mvc:annotation-driven conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=utf-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
1.7.响应输出
- @ResponseBody --直接产生响应文本
@GetMapping("/g")
@ResponseBody
public String getRequest(@RequestParam(value = "manager_name") String managerName){
System.out.println("managerName is:" + managerName);
return "This is get request";
}
- @ModelAndView --返回视图对象,跳转视图
@GetMapping("/view")
public ModelAndView showView(Integer userId){
ModelAndView mav = new ModelAndView("/view.jsp");
User user = new User();
if (userId == 1){
user.setUserName("Tom");
}else {
user.setUserName("张三");
}
mav.addObject("u",user);
return mav;
}
jsp代码
<html>
<head>
<title>Test JSP</title>
</head>
<body>
<h1>This is test JSP!</h1>
<h2>UserName:${u.userName}</h2>
</body>
</html>
重定向使用
ModelAndView mav = new ModelAndView("redirect:/view.jsp");
- 工程下载地址:
https://github.com/DeveloperZhang/SwiftStudyDemo
2.总结
此篇是SpringMVC的简单实例,包括基本的传值,跳转和一些数据转换操作.
网友评论