标签:
400错误是请求错误
Model是map格式
@Controller public class HelloController { @RequestMapping(value="/t04") //public String welcome(@RequestParam(value="username",required=false) String username){ public String welcome(String username,Model model ){ System.out.println("以后靠你了"+username); model.addAttribute("username",username);
//model.addAttribute(username);--输出页面就得改为${string} return "index"; } }
访问:http://localhost:8080/springmvc2/t04?username=hanlu
输出页面+ <h1>${username }</h1>
@RequestMapping(value="/hello2") public ModelAndView hello(String username){ System.out.println("hello2----"); ModelAndView model = new ModelAndView(); model.setViewName("index"); model.addObject("username", username); return model; }
访问页面:http://localhost:8080/springmvc2/hello2?username=hanlu
如果是对象的话model.addObject("user", new User());
model.addObject("username", username);改model.addObject(username);时要将
输出页面+ <h1>${username }</h1>改为${string}
//注意string要小写
下面还有一种方法“不推荐使用,可以了解了解
//不推荐使用 @RequestMapping(value="/hello3") public String hello(String username,Map<String,Object> model){ System.out.println("hello3---"); model.put("username", username);//输出页面${username} return "index"; }
标签:
原文地址:http://www.cnblogs.com/xuerong/p/4942185.html