Spring MVC:将字符串传递到后端
Spring MVC : Passing string to backend
我想问用户他们喜欢披萨还是三明治,我想将其传递给后端。这是我的代码:
<div id="food" class="modal">
<div class="modal-content animate" >
<div class="imgcontainer">
<span onclick="document.getElementById('food').style.display='none'" class="close" title="Close Modal">×</span>
</div>
<form id="foodform" action="food" method="post">
<input type="radio" id='pizza' name='pizza' value="pizza">
<label > pizza </label>
<input type="radio" id='burger' name='burger' value="burger">
<label > burger </label>
<input type="submit" name="submitted" value="submit" />
</form>
</centre>
</div>
@RequestMapping(value = "/food", method = RequestMethod.POST)
public String food(@Validated @ModelAttribute String food, Model model) {
System.out.println(food);
return "home";
}
这是控制器代码。但是什么也没有打印出来。请帮忙
尝试这样的事情:
@RequestMapping(value = "/food", method = RequestMethod.POST)
public String food(HttpServletRequest request, @RequestParam String food){
System.out.println(food);
return "home";
}
我想问用户他们喜欢披萨还是三明治,我想将其传递给后端。这是我的代码:
<div id="food" class="modal">
<div class="modal-content animate" >
<div class="imgcontainer">
<span onclick="document.getElementById('food').style.display='none'" class="close" title="Close Modal">×</span>
</div>
<form id="foodform" action="food" method="post">
<input type="radio" id='pizza' name='pizza' value="pizza">
<label > pizza </label>
<input type="radio" id='burger' name='burger' value="burger">
<label > burger </label>
<input type="submit" name="submitted" value="submit" />
</form>
</centre>
</div>
@RequestMapping(value = "/food", method = RequestMethod.POST)
public String food(@Validated @ModelAttribute String food, Model model) {
System.out.println(food);
return "home";
}
这是控制器代码。但是什么也没有打印出来。请帮忙
尝试这样的事情:
@RequestMapping(value = "/food", method = RequestMethod.POST)
public String food(HttpServletRequest request, @RequestParam String food){
System.out.println(food);
return "home";
}