Spring MVC-注解接受参数



4.2注解方式参数

接收参数:

1.       HttpServletRequest可以直接定义在参数的列表,可以使用

2.      在参数列表上直接定义要接收的参数名称,只要参数名称能匹配的上就能接收所传过来的数据,可以自动转换成参数列表里面的类型,注意的是值与类型之间是可以转换的

3.       数据写到页面,方法的返回值采用ModelAndView new ModelAndView("index", map);,相当于把结果数据放到request里面

4.       在参数列表中直接定义Modelmodel.addAttribute("p",person);把参数值放到request类里面去,建议使用

5.       Ajax调用springmvc的方法:直接在参数的列表上定义PrintWriterout.write(result);把结果写到页面,建议使用的

6.       请求方式的指定:@RequestMapping( method=RequestMethod.POST )可以指定请求方式,前台页面就必须要以它制定好的方式来访问,否则出现405错误

7.       重定向:controller内部重定向,redirect:加上同一个controller中的requestMapping的值,controller之间的重定向:必须要指定好controller的命名空间再指定requestMapping的值,redirect:后必须要加/,是从根目录开始


自定义注册属性转换器
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder){
		binder.registerCustomEditor(Date.class,
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}

@Controller//用来标注当前类是springmvc的控制层的类
@RequestMapping("/test")//controller的唯一标识或者命名空间
public class TestController {
	
	
	
	
	@RequestMapping("/hello.do")//用来访问控制层的方法的注解
	public String hello(){
		System.out.println("hello springmvc with annotation");
		return "jsp1/index";
	}
	
	
	
	@RequestMapping("/toPerson.do")
	public String toPerson(HttpServletRequest request){
		String result = request.getParameter("name");
		System.out.println(result);
		return "jsp1/index";
	}
	
	@RequestMapping("/toPerson1.do")
	public String toPerson1(String name, Integer age, String address, Date birthday){
		System.out.println(name + " "+ age + " "+ address + " " +birthday);
		return "jsp1/index";
	}
	
	/**
	 * 
	 * desc:传递的参数的名字必须要与实体类的属性set方法后面的字符串匹配的上才能接收到参数,首字符的大小写不区分
	 * 请求中传的参数只要是能和参数列表里面的变量名或者实体里面的set后面的字符串匹配的上就能接收到
	
	 */
	@RequestMapping("/toPerson2.do")
	public String toPerson2(Person person, User user){
		System.out.println(person);
		System.out.println(user);
		return "jsp1/index";
	}
	
	
	@RequestMapping("/toPerson3.do")
	public String toPerson3(String[] name){
		for(String result : name){
			System.out.println(result);
		}
		return "jsp1/index";
	}
	
	/**
	 * 
	 * desc:方法的返回值采用ModelAndView, new ModelAndView("index", map);
	 * ,相当于把结果数据放到request里面
	
	 */
	@RequestMapping("/toPerson4.do")
	public ModelAndView toPerson4() throws Exception{
		Person person = new Person();
		person.setName("james");
		person.setAge(28);
		person.setAddress("maami");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse("1985-04-22");
		person.setBirthday(date);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("p", person);
		return new ModelAndView("index", map);
	}
	
	/**
	 * 
	 * desc:直接在方法的参数列表中来定义Map,这个Map即使ModelAndView里面的Map,
	 * 由视图解析器统一处理,统一走ModelAndView的接口
	 * 也不建议使用
	
	 */
	@RequestMapping("/toPerson5.do")
	public String toPerson5(Map<String, Object> map) throws Exception{
		Person person = new Person();
		person.setName("james");
		person.setAge(28);
		person.setAddress("maami");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse("1985-04-22");
		person.setBirthday(date);
		map.put("p", person);
		return "index";
	}
	/**
	 * 
	 * desc:在参数列表中直接定义Model,model.addAttribute("p", person);把参数值放到request类里面去,建议使用
	
	 */
	@RequestMapping("/toPerson6.do")
	public String toPerson6(Model model) throws Exception{
		Person person = new Person();
		person.setName("james");
		person.setAge(28);
		person.setAddress("maami");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse("1985-04-22");
		person.setBirthday(date);
		//把参数值放到request类里面去
		model.addAttribute("p", person);
		return "index";
	}
	
	/**
	 * 
	 * desc:ajax的请求返回值类型应该是void,参数列表里直接定义HttpServletResponse,
	 * 获得PrintWriter的类,最后可把结果写到页面
	 * 不建议使用
	
	 */
	/*<script type="text/javascript">
  		$(function(){
  			$("#mybutton").click(function(){
  				$.ajax({
  					url:"test/ajax1.do",
  					type:"post",
  					dataType:"text",
  					data:{
  						name:"zhangsan"
  					},
  					success:function(responseText){
  						alert(responseText);
  					},
  					error:function(){
  						alert("system error");
  					}
  				});
  			});
  		});
  		
  	</script>
  	*/
	@RequestMapping("/toAjax.do")
	public String toAjax(){
		return "ajax";
	}
	@RequestMapping("/ajax.do")
	public void ajax(String name, HttpServletResponse response){
		String result = "hello " +name;
		try {
			response.getWriter().write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * desc:直接在参数的列表上定义PrintWriter,out.write(result);把结果写到页面,建议使用的
	
	 */
	@RequestMapping("/ajax1.do")
	public void ajax1(String name, PrintWriter out){
		String result = "hello " +name;
		out.write(result);
	}
	
	
	@RequestMapping("/toForm.do")
	public String toForm(){
		return "form";
	}
	
	/**
	 * 
	 * desc:controller内部重定向,redirect:加上同一个controller中的requestMapping的值
	
	 */
	@RequestMapping("/redirectToForm.do")
	public String redirectToForm(){
		return "redirect:toForm.do";
	}
	
	/**
	 * 
	 * desc:controller之间的重定向:必须要指定好controller的命名空间再指定requestMapping的值,
	 * redirect:后必须要加/,是从根目录开始
	
	 */
	@RequestMapping("/redirectToForm1.do")
	public String redirectToForm1(){
		return "redirect:/test1/toForm.do";
	}
	
	
	
	/**
	 * 
	 * desc:@RequestMapping( method=RequestMethod.POST )可以指定请求方式,前台页面就必须要以它制定好的方式来访问,否则出现405错误

	 */
	@RequestMapping(value="/toPerson7.do", method=RequestMethod.POST )
	public String toPerson7(Person person){
		System.out.println(person);
		return "jsp1/index";
	}
}




4.2注解方式参数

接收参数:

1.       HttpServletRequest可以直接定义在参数的列表,可以使用

2.      在参数列表上直接定义要接收的参数名称,只要参数名称能匹配的上就能接收所传过来的数据,可以自动转换成参数列表里面的类型,注意的是值与类型之间是可以转换的

3.       数据写到页面,方法的返回值采用ModelAndView new ModelAndView("index", map);,相当于把结果数据放到request里面

4.       在参数列表中直接定义Modelmodel.addAttribute("p",person);把参数值放到request类里面去,建议使用

5.       Ajax调用springmvc的方法:直接在参数的列表上定义PrintWriterout.write(result);把结果写到页面,建议使用的

6.       请求方式的指定:@RequestMapping( method=RequestMethod.POST )可以指定请求方式,前台页面就必须要以它制定好的方式来访问,否则出现405错误

7.       重定向:controller内部重定向,redirect:加上同一个controller中的requestMapping的值,controller之间的重定向:必须要指定好controller的命名空间再指定requestMapping的值,redirect:后必须要加/,是从根目录开始


自定义注册属性转换器
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder){
		binder.registerCustomEditor(Date.class,
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值