如何在Controller获取以multipart/form-data方式提交表单的参数值

如题所述

form里面的method是不是a.com,如果是的话,那说明是正确的。你可以把method修改为a.com?w=a。这样就达到你的要求了。 以上仅供参考!
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-02-18
@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>本回答被提问者采纳
相似回答