Announcement Announcement Module
No announcement yet.
Passing variables in url Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing variables in url

    I have a spring mvc app and what i want to do is,

    if there is an variable in the url when loading a jsp page (ie., read the variable then get something from the database (using dao) using that variable.

    How do i pass the variable to the controller?

    Does someone know how i can do this, or know of a site that does something similar.


  • #2
    any ideas?


    • #3
      Check the reference guide, chapter 6 which eplains about the different controllers.

      But here is the short answer. In the formBackingObject and referenceData method you have access to your HttpServletRequest object. From this you can get variables which are put there with GET/POST.

      String param = (String) request.getParameter("arg1");


      • #4

        i can now get the variable from the url, but how can i give this variable to my controller so i can use it to query to db.



        • #5
          In your controller java that returns that jsp view you can use

          String arg1 = ServletRequestUtils.getRequiredStringParameter(req uest, "arg1");

          If you have a method in your dao which accepts a different type, you'll need to use the suitable getRequired method.

          Record record = myDao.getRecord(arg1);


          • #6
            You don't really 'give' anything to the controller. As I stated checkout the javadocs of the formBackingObject and referenceData methods on the SimpleFormController

            When you just simple implemented the Controller interface check the javadocs of that.

            Also read the chapter about spring webmvc which explains how to use views/controllers in detail.

            The most simple answer is in most methods in the controller you have access to the HttpServletRequest object, this object has an getParameter method which you can use to retrieve parameters from the request. Those parameters are either there through a GET request (parameters on the URL like to ones you did) or by a POST request (most of the times form are getting posted, and the input fields are then put in the request parameters).