SpringMVC的参数类型转换

package com.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.entity.CollectionVO;
import com.entity.Person;

@Controller
@RequestMapping("/model2/")
public class HelloAnnotation3 {
	//SpringMVC参数类型转换
	//1.int String  http://localhost:8080/SpringMVCDemo/model2/m1?name=zhangsan&age=18
	@RequestMapping("m1")
	public String m1(int age,String name){
		System.out.println(age+","+name);
		//如果地址栏age参数为空,由于int不能接收null所以会报错,所以一般用包装类接收
		return "index";
	}
	
	//2.包装类Integer http://localhost:8080/SpringMVCDemo/model2/m2?age=18
	@RequestMapping("m2")
	public String m2(Integer age){
		System.out.println(age);
		return "index";
	}
	
	//3.数组  http://localhost:8080/SpringMVCDemo/model2/m3?arr=abc&arr=aaa&arr=hello
	@RequestMapping("m3")
	public String m3(String[] arr){
		System.out.println(arr.length);//3
		return "index";
	}
	
	//4.自定义类 http://localhost:8080/SpringMVCDemo/model2/m4?name=zhangsan&age=18
	@RequestMapping("m4")
	public String m4(Person p){
		System.out.println(p);
		System.out.println(p.getName()+","+p.getAge());
		return "index";//注意,容器自动将此处的person提交给视图层了
	}
	
	//以上都无需加参数注解
	//5.List http://localhost:8080/SpringMVCDemo/model2/m5?list=aa&list=bb
	@RequestMapping("m5")
	public String m5(@RequestParam List list){
		System.out.println(list.size());
		System.out.println(list);//[aa, bb]
		return "index";
	}
	
	//6.Map http://localhost:8080/SpringMVCDemo/model2/m6?k1=aaa&k2=bbb&k3=ccc
	@RequestMapping("m6")
	public String m6(@RequestParam Map map){
		System.out.println(map);//{k1=aaa, k2=bbb, k3=ccc}
		return "index";
	}
	
	//7.自定义复合对象类型,避免如5,6那样加注解,可以在entity中写一个CollectionVO封装
	//http://localhost:8080/SpringMVCDemo/model2/m7?list=aa&list=bb&map%5Bk1%5D=value1
	@RequestMapping("m7")
	public String m7(CollectionVO collectionVO){
		System.out.println(collectionVO.getList().size());//2
		//传map时,本来只用写map[k1]=value,但直接将中括号写在地址栏不解析,而应该写
		//左中括号%5B 右中括号%5D
		System.out.println(collectionVO.getMap());//{k1=value1}
		return "index";
	}
	
	//8.日期参数的处理,默认传参时是斜杠的形式  yyyy/MM/dd HH:mm:ss
	//http://localhost:8080/SpringMVCDemo/model2/m8?date=2018/8/8 20:30:59
	@RequestMapping("m8")
	public String m8(Date date){
		System.out.println(date);//Wed Aug 08 20:30:59 CST 2018
		return "index";
	}
	
	//9.自定义日期参数格式
	//http://localhost:8080/SpringMVCDemo/model2/m9?date=2018-8-8 20:30:59
	@RequestMapping("m9")
	public String m9(@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date date){
		//单纯加上述注解不起作用,还要补充一下注解驱动内容,详见springMVC-servlet.xml
		System.out.println(date);//Wed Aug 08 20:30:59 CST 2018
		return "index";
	}

}

自定义日期参数格式时,在springMVC-servlet.xml中加如下配置:

<!-- 自定义日期参数格式补充注解驱动内容 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean>
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

CollectionVO.java

package com.entity;

import java.util.List;
import java.util.Map;

public class CollectionVO {
	
	private List list;
	private Map map;
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Map getMap() {
		return map;
	}
	public void setMap(Map map) {
		this.map = map;
	}
	

}

猜你喜欢

转载自blog.csdn.net/yicha_i/article/details/82775300