如何从 <form:select> Spring MVC 传递数据
Posted
技术标签:
【中文标题】如何从 <form:select> Spring MVC 传递数据【英文标题】:How to pass data from <form:select> Spring MVC 【发布时间】:2013-06-17 14:35:42 【问题描述】:我想和你分享我的问题。
我在 jsp 页面上构建并在我的页面上使用了一些 <form:select>
,当我请求页面 search.jsp
并且用户必须选择一个时,它使用 <form:select>
从数据库中提取和呈现数据:
<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>
我需要将用户选择的请求参数传递给我的控制器,这是控制器的代码:
@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
传递我选择的所有参数谢谢。
【问题讨论】:
【参考方案1】:我相信你应该在<form:form>
中使用<form:select>
。
应该是这样的:
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:
@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:
public class MyForm
private String nameOfInstitution;
public String getNameOfInstitution()
return nameOfInstitution;
public void setNameOfInstitution(String nameOfInstitution)
this.nameOfInstitution = nameOfInstitution;
希望这会有所帮助。
【讨论】:
帮助了我,但需要为我的所有Search
属性创建新的 command
对象以上是关于如何从 <form:select> Spring MVC 传递数据的主要内容,如果未能解决你的问题,请参考以下文章
如何添加 optgroup do dijit.form.Select 或其他小部件类型
Spring <form:select> 标签在 Spring mvc + Hibernate 应用程序中导致错误