springmvc接受json数据可以通过名称、通过对象,也可以通过路径。当使用路径占位符时,会出现中文乱码。此时,需要转换。即
@GetMapping("/girl5") //http://localhost:8080/girl?name=张柏芝2&age=18
public ModelAndView test5(User user){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","我是页面url传参进来的,姓名是:" + user.getName()+" 年龄是:"+user.getAge());
modelAndView.setViewName("girl");
return modelAndView;
}
@GetMapping("/girl2") //http://localhost:8080/girl?name=张柏芝2&age=18
public ModelAndView test3(@RequestParam String name, @RequestParam Integer age){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","我是页面url传参进来的,姓名是:" + name+" 年龄是:"+age);
modelAndView.setViewName("hello");
return modelAndView;
}
@GetMapping( value = "/boy/{name}/{age}")
// http://localhost:8080/boy/张三/17
public ModelAndView test2(@PathVariable String name, @PathVariable Integer age) throws UnsupportedEncodingException {
name = new String(name.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);//获取再转换
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg","我是页面url路径传参,姓名是:" + name+" 年龄是:"+age);
modelAndView.setViewName("boy");
return modelAndView;
}
标签:name,springmvc,age,路径,new,占位,乱码,modelAndView,ModelAndView From: https://www.cnblogs.com/liang9479/p/17067851.html