How to pass data from <form:select> Spring M

2019-01-19 04:21发布

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.

1条回答
萌系小妹纸
2楼-- · 2019-01-19 04:43

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.

查看更多
登录 后发表回答