SpringMVC4 学习笔记(十三)【校正请求参数名】

当表单中提交的参数名和处理器中读取的参数名不一致的时候,如果不做处理,是无法正确读取到参数的


 比如这样:

姓名: 年龄:

@Controller
@RequestMapping("/test")
public class MyController {
	
	@RequestMapping("/register.do")
	public ModelAndView doRegister(String name, String age) {
		System.out.println(name);
		System.out.println(age);
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("name", name);
		mv.addObject("age", age);
		mv.setViewName("/WEB-INF/jsp/welcome.jsp");
		return mv;
	}

}
只能正确读取到age,读取不到name

所以需要给name加上@RequestParam注释,校正请求参数名:

@RequestMapping("/register.do")
	public ModelAndView doRegister(@RequestParam("pname") String name, String age) {
		System.out.println(name);
		System.out.println(age);
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("name", name);
		mv.addObject("age", age);
		mv.setViewName("/WEB-INF/jsp/welcome.jsp");
		return mv;
	}














你可能感兴趣的:(SpringMVC)