gpt4 book ai didi

spring - 如何从
Spring MVC 传递数据

转载 作者:行者123 更新时间:2023-12-04 03:04:26 27 4
gpt4 key购买 nike

我想和你分享我的问题。

我建立在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>

我需要将用户选择的作为请求参数传递给我的 Controller ,这是 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 传递我选择的所有参数谢谢。

最佳答案

我相信你应该使用 <form:select><form:form> .

它应该是这样的:

搜索.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;
}
}

希望这会有所帮助。

关于spring - 如何从 <form :select> Spring MVC 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17218693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com