Announcement Announcement Module
Collapse
No announcement yet.
Add parameter to Spring MVC controller class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add parameter to Spring MVC controller class

    For a class as follows:

    public class TestController extends MultiActionController

    public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ...
    ...
    ...

    The test method is triggered from the javascript by issuing ...cmd=test

    Obviously the test method "by default" has two parameters: request and response.

    Can I add third parameter such as :

    test(HttpServletRequest request, HttpServletResponse response, String str)

    or

    test(String str, HttpServletRequest request, HttpServletResponse response)

    If so, how can I do it (I mean by triggerring from the front codes: such as x.htm?cmd=test)
    ?


    Thanks


    Scott
    Last edited by scott; Sep 16th, 2008, 05:16 PM.

  • #2
    you can have signatures of methods in the multiaction controller as follows

    Code:
    public ModelAndView myMethod(HttpServletRequest, req, HttpServletResponse res, HttpSession sess, Object command) throws Exception{
    }
    you could also have a three parameter method
    Code:
    public ModelAndView myMethod(HttpServletRequest, req, HttpServletResponse res, HttpSession sess) throws Exception{
    }

    Comment


    • #3
      If I have:

      test(HttpServletRequest request, HttpServletResponse response, String str)

      in the java class.

      How can I trigger it from the javascript: such as using x.htm?cmd=test

      But how to pass the third parameter str?


      Scott

      Comment


      • #4
        MultiActionControllers in the non-annotated Spring MVC model don't support explicit binding of request parameters. So you'll have to work directly with the HttpServletRequest object's API. You may find ServletRequestUtils helpful for common tasks in manipulating this API.

        The example method you cite won't work at all - see the Javadoc for MultiActionController for allowed method signatures.

        Note that the answer is different if you are using annotated controllers.

        Comment

        Working...
        X