我是Spring MVC的新手,我正在尝试从控制器将值获取到jsp表单选择列表。
显示选择列表,但为空。我尝试了其他示例,但仍然找不到,这是问题所在。

控制器:

@Controller
public class MainController {
       @RequestMapping(value = "/", method = RequestMethod.GET)
       public ModelAndView login() {
          return new ModelAndView("login", "command", new Person());
       }


       @RequestMapping(value = "/", method = RequestMethod.POST)
       public String login(@ModelAttribute("xxx")Person person, ModelMap model) {
           Map<String,String> list = new LinkedHashMap<String,String>();
           list.put("opt1", "value1");
           list.put("opt2", "value2");
           model.put("list", list);

          return "newView";
       }
}


newView.jsp:

<form:form method="POST" action="/xxx/saveUser" command="">
   <table>
    <tr>
      <form:select path="name">
        <form:options items="${list}" />
      </form:select>
    </tr>
</table>
</form:form>


Person.java模型:

public class Person {
       private String name;
//Getters and setters and more values etc...
}


提前致谢

最佳答案

看看这个问题。

How to populate dropdown box in Spring MVC

这为您提供了答案。

07-24 09:49
查看更多