如何从 <form:select> 传递数据春季MVC

编程入门 行业动态 更新时间:2024-10-26 06:35:14
本文介绍了如何从 <form:select> 传递数据春季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 获取参数,然后验证它们并将它们传递给另一个方法以提取对应的数据请求参数...但问题是我无法传递它们.它只是像这样显示我 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 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

本文发布于:2023-10-12 01:32:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:春季   数据   form   lt   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!