Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

jsp - How to pass data from <form:select> Spring MVC

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...