基于Spring MVC的Web应用开发(8) - Convert

12 篇文章 0 订阅

本文介绍SpringMVC中的Convert,Convert是我认为的SpringMVC最吸引人,最优雅的特性,下面通过例子程序领略一下:

package org.springframework.samples.mvc.convert;

import java.util.Collection;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/convert/*")
public class ConvertController {

	@RequestMapping("primitive")
	public @ResponseBody String primitive(@RequestParam Integer value) {
		return "Converted primitive " + value;
	}

	// requires Joda-Time on the classpath
	@RequestMapping("date/{value}")
	public @ResponseBody String date(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date value) {
		return "Converted date " + value;
	}
	
	@RequestMapping("collection")
	public @ResponseBody String collection(@RequestParam Collection<Integer> values) {
		return "Converted collection " + values;
	}

	@RequestMapping("formattedCollection")
	public @ResponseBody String formattedCollection(@RequestParam @DateTimeFormat(iso=ISO.DATE) Collection<Date> values) {
		return "Converted formatted collection " + values;
	}

	@RequestMapping("bean")
	public @ResponseBody String bean(JavaBean bean) {
		return "Converted " + bean;
	}

	@RequestMapping("value")
	public @ResponseBody String valueObject(@RequestParam SocialSecurityNumber value) {
		return "Converted value object " + value;
	}

	@RequestMapping("custom")
	public @ResponseBody String customConverter(@RequestParam @MaskFormat("###-##-####") String value) {
		return "Converted '" + value + "' with a custom converter";
	}

}

 

逐一方法看过来:

1.

	@RequestMapping("primitive")
	public @ResponseBody String primitive(@RequestParam Integer value) {
		return "Converted primitive " + value;
	}

@RequestParam自动获取URL提交的参数名为value的值并赋值到Integer value变量,

访问"http://localhost:8080/web/convert/primitive?value=1"

浏览器显示"Converted primitive 1"

 

2.

	// requires Joda-Time on the classpath
	@RequestMapping("date/{value}")
	public @ResponseBody String date(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date value) {
		return "Converted date " + value;
	}

@PathVariable自动获取URL路径中date/后面的值,判断该值是否满足ISO.DATE的类型(yyyy-MM-dd),最后将这个字符串转成Date value,

访问"http://localhost:8080/web/convert/date/2012-03-30"

浏览器显示"Converted date Fri Mar 30 00:00:00 CST 2012"

 

3.

	@RequestMapping("collection")
	public @ResponseBody String collection(@RequestParam Collection<Integer> values) {
		return "Converted collection " + values;
	}

如果提交了同名参数的多个值(例如html的多选框),这些值会自动的封装到Collection<Integer> values里,

访问"http://localhost:8080/web/convert/collection?values=1&values=2"  

浏览器显示"Converted collection [1, 2]"

 

4.

	@RequestMapping("formattedCollection")
	public @ResponseBody String formattedCollection(@RequestParam @DateTimeFormat(iso=ISO.DATE) Collection<Date> values) {
		return "Converted formatted collection " + values;
	}

访问"http://localhost:8080/web/convert/formattedCollection?values=2012-03-30&values=2012-12-25"

浏览器显示"Converted formatted collection [Fri Mar 30 00:00:00 CST 2012, Tue Dec 25 00:00:00 CST 2012]"

 

以上说的都是convert简单类型,下面讲解如何convert自定义的Java类,写一个JavaBean类,该类有几个属性:

	private Integer primitive;
	
	@DateTimeFormat(iso=ISO.DATE)
	private Date date;

	@MaskFormat("(###) ###-####")
	private String masked;

	// list will auto-grow as its dereferenced e.g. list[0]=value
	private List<Integer> list;

	// annotation type conversion rule will be applied to each list element
	@DateTimeFormat(iso=ISO.DATE)
	private List<Date> formattedList;

	// map will auto-grow as its dereferenced e.g. map[key]=value
	private Map<Integer, String> map;

	// nested will be set when it is referenced e.g. nested.foo=value
	private NestedBean nested;

注意到该JavaBean还嵌套了一个自定义的Java类NestedBean,该类的属性为:

	private String foo;

	private List<NestedBean> list;
	
	private Map<String, NestedBean> map;

看一下详细用法:

 

5.

访问"http://localhost:8080/web/convert/bean?primitive=1"

浏览器显示"Converted JavaBean primitive=1"

 

6.

访问"http://localhost:8080/web/convert/bean?date=2012-03-30"

浏览器显示"Converted JavaBean date=Fri Mar 30 00:00:00 CST 2012"

 

7.

访问"http://localhost:8080/web/convert/bean?masked=(123)-456-7890"

浏览器显示"Converted JavaBean masked=(123) 456-7890"

 

8.

访问"http://localhost:8080/web/convert/bean?list[0]=1&list[1]=2"

浏览器显示"Converted JavaBean list=[1, 2]"

 

9.

访问"http://localhost:8080/web/convert/bean?formattedList[0]=2012-03-30&formattedList[1]=2012-12-25"

浏览器显示"Converted JavaBean formattedList=[Fri Mar 30 00:00:00 CST 2012, Tue Dec 25 00:00:00 CST 2012]"

 

10.

访问"http://localhost:8080/web/convert/bean?map[1]=a&map[2]=b"

浏览器显示"Converted JavaBean map={1=a, 2=b}"

 

11.

访问"http://localhost:8080/web/convert/bean?nested.foo=stephansun"

浏览器显示"Converted JavaBean nested=NestedBean foo=stephansun"

 

12.

访问"http://localhost:8080/web/convert/bean?nested.list[0].foo=stephansun"

浏览器显示"Converted JavaBean nested=NestedBean list=[NestedBean foo=stephansun]"

 

13.

访问"http://localhost:8080/web/convert/bean?nested.map[1].foo=stephansun"

浏览器显示"Converted JavaBean nested=NestedBean map={1=NestedBean foo=stephansun}"

 

14.

访问"http://localhost:8080/web/convert/value?value=1"

浏览器显示"Converted value object SocialSecurityNumber [value=1]"

 

15.

访问"http://localhost:8080/web/convert/custom?value=ab-cd-efgh"

浏览器显示"Converted 'ab-cd-efgh' with a custom converter"

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Field error in object 'admin' on field 'id': rejected value [null]; codes [typeMismatch.admin.id,typeMismatch.id,typeMismatch.java.lang.Integer,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [admin.id,id]; arguments []; default message [id]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'id'; nested exception is java.lang.NumberFormatException: For input string: "null"] at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:157) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124) ~[spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161) [spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131) [spring-web-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:871) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:777) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) [servlet-api.jar:4.0.FR] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855) [spring-webmvc-5.0.2.RELEASE.jar:5.0.2.RELEASE]
最新发布
05-31

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值