如何从 <form:select> Spring MVC 传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17218693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass data from <form:select> Spring MVC
提问by java_user
I would like to share my problem with you.
我想和你分享我的问题。
I built on jsp page and use on my page some of <form:select>it use <form:select>to extract and render data from DB when I request the page search.jspand user have to select one:
我建立在 jsp 页面上并在我的页面上<form:select>使用其中一些用于<form:select>在我请求页面search.jsp和用户必须选择一个页面时从数据库中提取和呈现数据:
<form action="result" method="get" >
<table>
<tr>
<th>Date fro DB:</th>
<td><form:select path="listOfDates">
<form:option value="NONE"> --SELECT--</form:option>
<form:options items="${listOfDates}"></form:options>
</form:select>
</td>
</tr>
<tr>
<th>Name of company from DB:</th>
<td><form:select path="listOfInstitutionsNames">
<form:option value="NONE"> --SELECT--</form:option>
<form:options items="${listOfInstitutionsNames}"></form:options>
</form:select>
</td>
</tr>
<tr>
<th>Type of company from DB:</th>
<td>
<form:select path="listOfInstitutionsTypes">
<form:option value="NONE"> --SELECT--</form:option>
<form:options items="${listOfInstitutionsTypes}"></form:options>
</form:select>
</td>
</tr>
<tr>
<td><input type="submit" value="Извлечь"/></td>
</tr>
</table>
</form>
I need to pass what user select as request parameter to my controller, here is the code of controller:
我需要将用户选择的内容作为请求参数传递给我的控制器,这是控制器的代码:
@Controller
public class HomeController{
@Autowired
private ControllerSupportClass controllerSupportClass;
@RequestMapping(value="/search", method=RequestMethod.GET)
public String search(Model model) {
List<Date> listOfDates = controllerSupportClass.findAllDatesForm();
List<String> listOfInstitutionsNames = controllerSupportClass.findAllInstitutionsForm();
List<String> listOfInstitutionsTypes = controllerSupportClass.findAllTypesForm();
model.addAttribute("listOfInstitutionsTypes", listOfInstitutionsTypes);
model.addAttribute("listOfInstitutionsNames", listOfInstitutionsNames);
model.addAttribute("listOfDates", listOfDates);
return "search";
}
@RequestMapping(value ="/result", method=RequestMethod.GET)
public String SecondActionPage(@RequestParam String particularDate,
@RequestParam String nameOfInstitution,
@RequestParam String typeName,
Model model) throws Exception {
if(particularDate !="" && nameOfInstitution.trim() !="" && typeName.trim()=="") {
controllerSupportClass.findWithDateAndName(nameOfInstitution, particularDate, model);
} else if(particularDate.trim() !="" && nameOfInstitution.trim() =="" && typeName.trim() !="") {
controllerSupportClass.findWithAddedDateAndType(typeName, particularDate, model);
} else if(particularDate.trim() !="" && nameOfInstitution.trim() =="" && typeName.trim() ==""){
controllerSupportClass.findWithAddedDate(particularDate, model);
} else if(particularDate.trim() !="" && nameOfInstitution.trim() !="" && typeName.trim() !="") {
throw new Exception("Search by choose all parameters is not exceptable");
} else {
throw new Exception("You didn't put any search parameters");
}
return "search";
}
}
As you can see my SecondActionPage()method use @RequestParamannotation to get parameters from url and after validate them and pass them to another method to extract data corresponding on request parameters... But problem is that I can't to pass them. It just show me like this http://localhost:8080/controller/result?and after ? nothing passing. How can I pass this all my choosen parameters from serach.jspthank you.
正如您所看到的,我的SecondActionPage()方法使用@RequestParam注释从 url 获取参数,然后验证它们并将它们传递给另一个方法以提取与请求参数对应的数据......但问题是我无法传递它们。它只是显示我这样http://localhost:8080/controller/result?和之后?没有什么通过。我怎么能把我选择的所有参数都传递给serach.jsp谢谢。
回答by Shinichi Kai
I believe you should use <form:select>inside the <form:form>.
我相信你应该<form:select>在<form:form>.
It should look like this:
它应该是这样的:
search.jsp:
搜索.jsp:
<form:form modelAttribute="myform" action="result" method="get" >
<form:select path="nameOfInstitution">
<form:option value="NONE"> --SELECT--</form:option>
<form:options items="${listOfInstitutionsNames}"></form:options>
</form:select>
...
</form:form>
HomeController.java:
HomeController.java:
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String search(Model model) {
...
model.addAttribute("myform", new MyForm());
return "search";
}
@RequestMapping(value = "/result", method = RequestMethod.GET)
public String SecondActionPage(@RequestParam String nameOfInstitution,
Model model,
@ModelAttribute("myform") MyForm myform)
throws Exception {
...
}
MyForm.java:
MyForm.java:
public class MyForm {
private String nameOfInstitution;
public String getNameOfInstitution() {
return nameOfInstitution;
}
public void setNameOfInstitution(String nameOfInstitution) {
this.nameOfInstitution = nameOfInstitution;
}
}
Hope this helps.
希望这可以帮助。

