我们在使用SpringMVC时,常常需要把表单中的参数映射到我们对象的属性中,我们可以在默认的spring-servlet.xml加上如下的配置即可做到普通数据类型的转换,如将String转换成Integer和Double等:
|
1
|
<mvc:annotation-driven />
|
或
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
其实 <mvc:annotation-driven /> 标签会默认创建并注册一个 RequestMappingHandlerMapping(在Spring3.2之前是DefaultAnnotationHandlerMapping) 和 RequestMappingHandlerAdapter (Spring3.2之前是AnnotationMethodHandlerAdapter),当然,如果上下文有对应的显示实现类,将该注解注册的覆盖掉。该注解还会创建一个ConversionService,即 FormattingConversionServiceFactoryBean。
如果想把一个字符串转换成其它实体类型,spring没有提供这样默认的功能,我们需要自定义类型转换器。
这里有个实体类Employee。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.proc;
public class Employee {
private String name;
private Integer age;
private String gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", gender=" + gender
+ "]";
}
}
|
规定将字符串转换成实体类的规则为: name-age-gender
那么接下来就写一个类型转换器,需要实现一个接口org.springframework.core.convert.converter.Converter
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.proc;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
@Component
public class EmployeeConverter implements Converter<String, Employee> {
@Override
public Employee convert(String str) {
Employee emp=null;
//字符串格式 name-age-gender
if(str!=null && str.split("-").length==3){
emp=new Employee();
String[] arr=str.split("-");
emp.setName(arr[0]);
emp.setAge(Integer.parseInt(arr[1]));
emp.setGender(arr[2]);
}
return emp;
}
}
|
这里我们为转换器加上@Component注解,是为了让Spring自动扫描该转换器到容器中。这样就不用通过配置文件配置<bean>了
接下来编写控制器
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.proc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class EmployeeController {
@RequestMapping("/add")
public String add(@RequestParam("employee") Employee employee){
System.out.println(employee);
return "add";
}
}
|
这里可以看到,参数的名字为employee,所有要为请求定义一个employee参数,该参数传入需要转换的字符串
除此之外,我们还需要在spring配置文件中配置,如下类容。让转换器生效
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.proc"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<ref bean="employeeConverter"/>
</set>
</property>
</bean>
</beans>
|
测试代码:http://localhost:8080/springmvc-1/add?employee=caoyc-18-male
结果输出:Employee [name=caoyc, age=18, gender=male]
上面配置文件中我们使用配置了一个ConversionServiceFactoryBean。虽然可以这样,但不建议。我们通常用org.springframework.format.support.FormattingConversionServiceFactoryBean
为什么要这样用呢?这样用的好处是什么呢?
使用FormattingConversionServiceFactoryBean可以让SpringMVC支持@NumberFormat和@DateTimeFormat等Spring内部自定义的转换器。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.cnblogs.com/caoyc/p/5639652.html
相关文章
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 22
-
ubuntu 16.04 LTS 安装mongodb 3.2.8教程
2025-05-25 62 -
利用Java Apache POI 生成Word文档示例代码
2025-05-29 69 -
2025-05-25 75
-
2025-05-29 22

