0

my requirement is to return view according to the selected value (select form)

view.jsp:

      <form  method="post" action="/aaa">
                      <select id="attr1" name="attr1">

                            <option value="1">A</option>
                            <option value="2">B</option>

                        </select>
                  <input type="submit" value="submit" />
</form>

if the value selected is A(1) is selected, view1 is the view to display, else view2 is displayed.

Controller method:

@RequestMapping(value = "/aaa", method = RequestMethod.POST)
public ModelAndview methodName ( HttpServletRequest request,
        HttpServletResponse response){

   attribute=request.getParameter("attr1");

   if (attribute==1) return new ModelAndView("view1")
   else if (attribute==2) return new ModelAndView ("view2")

   }

How can I do it? Thanks.

RaisMEd
  • 95
  • 1
  • 2
  • 9

1 Answers1

1

Multiple views are perfectly possible.

Considering the HTML:

<select id="attr1" name="attr1">
    <option value="1">A</option>
    <option value="2">B</option>
</select>

Then the controller method should be:

@RequestMapping(value = "/aaa", method = RequestMethod.POST)
public ModelAndView methodName(@RequestParam(value = "attr1") int attribute) {
    if (attribute == 1) {
        return new ModelAndView("view1");
    }
    else if (attribute == 2) {
        return new ModelAndView("view2");
    }
    else {
        return null; // Empty 200 OK just to be sure if other attr is received
    }
}
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • If you must use `HttpServletRequest` and `HttpServletResponse` let me know. The above answer is simpler though. – acdcjunior Jun 12 '13 at 18:06
  • I use HttpServletRequest to get the parametrs from the view. – RaisMEd Jun 12 '13 at 18:10
  • Yeah, `@RequestParam(value = "attr1") int attribute` maps automatically the param `attr1` to the variable `attribute`. – acdcjunior Jun 12 '13 at 18:14
  • it's better, I get the value selected but no view displayed (404). I'm verifying may be I have some errors.. else I will ask!! :) thank you soooooo much. – RaisMEd Jun 12 '13 at 18:29
  • Make sure the views `view1` and `view2` exist with those names. If there's something else I can do for you, just let me know... – acdcjunior Jun 12 '13 at 18:31
  • hi, the 2 views works fine when i return just one of them,but not if i do like above – RaisMEd Jun 12 '13 at 18:42
  • Probleme is that method return always null. else { return null; } – RaisMEd Jun 12 '13 at 18:58
  • Ok, let me check what may be going on. – acdcjunior Jun 12 '13 at 19:05
  • Your `action` attribute of the form may be incorrect. Where is your app deployed at? If it is deployed at, for instance, `http://localhost:8080/mySpringApp`, then the form action should be `
    ` - you have to put the full path there.
    – acdcjunior Jun 12 '13 at 19:11
  • yes, it's correct, probleme is method always return the last condition, when I return another view instead of null, i get it when I submit. – RaisMEd Jun 12 '13 at 19:31
  • I tried with this ModelAndView mav= new ModelAndView(); if (type==1) { mav=new ModelAndView("view1"); } else if (attr==2) { mav= new ModelAndView("view2"); } return mav; but nothing – RaisMEd Jun 12 '13 at 19:39
  • What are the types of your `type` and `attr`? If they are strings, you must use `if (type.equals("1")` and `else if (attr.equals("2")`. – acdcjunior Jun 12 '13 at 19:50
  • YESSS, ;) , they are a strings, really I apreciate your help, thank you so muchh friend. – RaisMEd Jun 12 '13 at 19:59