如何从 <form:select> 传递数据春季MVC [英] How to pass data from <form:select> Spring MVC
问题描述
我想和你分享我的问题.
I would like to share my problem with you.
我建立在jsp页面上并在我的页面上使用了一些<form:select>
它使用<form:select>
从数据库中提取和呈现数据当我请求页面 search.jsp
并且用户必须选择一个:
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.jsp
and user have to select one:
<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";
}
}
如您所见,我的 SecondActionPage()
方法使用 @RequestParam
批注从 url 获取参数,然后验证它们并将它们传递给另一个方法以提取对应的数据请求参数...但问题是我无法传递它们.它只是像这样显示我 http://localhost:8080/controller/result?
和之后?没有什么通过.我怎样才能从 serach.jsp
中传递我选择的所有参数,谢谢.
As you can see my SecondActionPage()
method use @RequestParam
annotation 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.jsp
thank you.
推荐答案
我相信你应该在
里面使用
.
I believe you should use <form:select>
inside the <form:form>
.
它应该是这样的:
search.jsp:
search.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;
}
}
希望这会有所帮助.
这篇关于如何从 <form:select> 传递数据春季MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!