0

我的要求是根据所选值返回视图(选择表单)

视图.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>

如果选择的值是 A(1),则 view1 是要显示的视图,否则显示 view2。

控制器方法:

@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")

   }

我该怎么做?谢谢。

4

1 回答 1

1

多个视图是完全可能的。

考虑到 HTML:

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

那么控制器方法应该是:

@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
    }
}
于 2013-06-12T18:00:00.583 回答