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

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

Java代码 复制代码  收藏代码
  1. package org.springframework.samples.mvc.convert;   
  2.   
  3. import java.util.Collection;   
  4. import java.util.Date;   
  5.   
  6. import org.springframework.format.annotation.DateTimeFormat;   
  7. import org.springframework.format.annotation.DateTimeFormat.ISO;   
  8. import org.springframework.stereotype.Controller;   
  9. import org.springframework.web.bind.annotation.PathVariable;   
  10. import org.springframework.web.bind.annotation.RequestMapping;   
  11. import org.springframework.web.bind.annotation.RequestParam;   
  12. import org.springframework.web.bind.annotation.ResponseBody;   
  13.   
  14. @Controller  
  15. @RequestMapping("/convert/*")   
  16. public class ConvertController {   
  17.   
  18.     @RequestMapping("primitive")   
  19.     public @ResponseBody String primitive(@RequestParam Integer value) {   
  20.         return "Converted primitive " + value;   
  21.     }   
  22.   
  23.     // requires Joda-Time on the classpath   
  24.     @RequestMapping("date/{value}")   
  25.     public @ResponseBody String date(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date value) {   
  26.         return "Converted date " + value;   
  27.     }   
  28.        
  29.     @RequestMapping("collection")   
  30.     public @ResponseBody String collection(@RequestParam Collection<Integer> values) {   
  31.         return "Converted collection " + values;   
  32.     }   
  33.   
  34.     @RequestMapping("formattedCollection")   
  35.     public @ResponseBody String formattedCollection(@RequestParam @DateTimeFormat(iso=ISO.DATE) Collection<Date> values) {   
  36.         return "Converted formatted collection " + values;   
  37.     }   
  38.   
  39.     @RequestMapping("bean")   
  40.     public @ResponseBody String bean(JavaBean bean) {   
  41.         return "Converted " + bean;   
  42.     }   
  43.   
  44.     @RequestMapping("value")   
  45.     public @ResponseBody String valueObject(@RequestParam SocialSecurityNumber value) {   
  46.         return "Converted value object " + value;   
  47.     }   
  48.   
  49.     @RequestMapping("custom")   
  50.     public @ResponseBody String customConverter(@RequestParam @MaskFormat("###-##-####") String value) {   
  51.         return "Converted '" + value + "' with a custom converter";   
  52.     }   
  53.   
  54. }  
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.

Java代码 复制代码  收藏代码
  1. @RequestMapping("primitive")   
  2. public @ResponseBody String primitive(@RequestParam Integer value) {   
  3.     return "Converted primitive " + value;   
  4. }  
	@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.

Java代码 复制代码  收藏代码
  1. // requires Joda-Time on the classpath   
  2. @RequestMapping("date/{value}")   
  3. public @ResponseBody String date(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date value) {   
  4.     return "Converted date " + value;   
  5. }  
	// 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.

Java代码 复制代码  收藏代码
  1. @RequestMapping("collection")   
  2. public @ResponseBody String collection(@RequestParam Collection<Integer> values) {   
  3.     return "Converted collection " + values;   
  4. }  
	@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.

Java代码 复制代码  收藏代码
  1. @RequestMapping("formattedCollection")   
  2. public @ResponseBody String formattedCollection(@RequestParam @DateTimeFormat(iso=ISO.DATE) Collection<Date> values) {   
  3.     return "Converted formatted collection " + values;   
  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类,该类有几个属性:

Java代码 复制代码  收藏代码
  1. private Integer primitive;   
  2.   
  3. @DateTimeFormat(iso=ISO.DATE)   
  4. private Date date;   
  5.   
  6. @MaskFormat("(###) ###-####")   
  7. private String masked;   
  8.   
  9. // list will auto-grow as its dereferenced e.g. list[0]=value   
  10. private List<Integer> list;   
  11.   
  12. // annotation type conversion rule will be applied to each list element   
  13. @DateTimeFormat(iso=ISO.DATE)   
  14. private List<Date> formattedList;   
  15.   
  16. // map will auto-grow as its dereferenced e.g. map[key]=value   
  17. private Map<Integer, String> map;   
  18.   
  19. // nested will be set when it is referenced e.g. nested.foo=value   
  20. private NestedBean nested;  
	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,该类的属性为:

Java代码 复制代码  收藏代码
  1. private String foo;   
  2.   
  3. private List<NestedBean> list;   
  4.   
  5. private Map<String, NestedBean> map;  
	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"

猜你喜欢

转载自zxb1985.iteye.com/blog/1814099