@RequestMapping(value = "/login", method = RequestMethod.<a href="
https://www.baidu.com/s?wd=GET&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLPhR3mywBrjNbuAmYnjcL0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDsrH6YPWbvP1bknWcdn1R1rf" target="_blank" class="baidu-highlight">GET</a>)
public ModelAndView myMethod(HttpServletRequest request,
HttpServletResponse response,
//@RequestParam("username")String username, @RequestParam("password")String password
String username,String password,//如果和前台的一样一样简写成这样
ModelMap modelMap) throws Exception {
username = new String(username.<a href="
https://www.baidu.com/s?wd=get&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLPhR3mywBrjNbuAmYnjcL0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHDsrH6YPWbvP1bknWcdn1R1rf" target="_blank" class="baidu-highlight">get</a>Bytes("iso8859-1"),"UTF-8");//编码问题 (服务器一帮都是iso的)
System.out.println(username+":"+password);
modelMap.put("loginUser", username);
modelMap.put("msg", "I'm in session");
return new ModelAndView("/view/hello", modelMap);
}
这里是一个方法,springMVC中参数可以直接现在方法的参数列表中,如果是对象属性的话也可以直接使用对象例如(User user,...)
刚才的方法对应的前台:
<form action="login">
username:
<input name="username" />
<br>
password:
<input name="password" type="password" />
<br>
<input type="submit" value="登入" />
</form>本回答被提问者采纳