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.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";
}
}
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.
The controller defines two simple operations – the GET for displaying data in the form, and the POST for the create operation, via form's submit. To access our form backing object, we need to inject it via the @ModelAttribute annotation.
The commandName attribute is the most important attribute in the form tag, which specifies the model attribute name that contains a backing object and the properties of this object will be used to populate the generated form.
Form Backing Object/Command Object This is a POJO that is used to collect all information on a form. It contains data only. It is also called a Command Object in some Spring tutorials.
I believe you should use <form:select>
inside the <form:form>
.
It should look like this:
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;
}
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With