【问题标题】:Spring 3.x.x MVC jsp combobox troubleSpring 3.x.x MVC jsp 组合框麻烦
【发布时间】:2013-02-05 18:50:18
【问题描述】:


        我一直在兜圈子几天没有任何进展,试图获得一个与 Spring-MVC 3.x.x 和 jsp 一起使用的简单组合框(由 form:select 制作)。有几个示例是通过扩展现已弃用的“SimpleFormController”来实现的,但是我没有找到任何使用 Spring 3.0.x 注释的简洁示例。此外,我已经查看了 Spring 的引用 documentation ,但我无法获得控制器和视图 (jsp) 的 sn-p,这可以引导我运行组合框组件。
        到目前为止,我没有成功的尝试是这样的:(任何评论将不胜感激)

控制器类(例如 MyController.java)

@Controller
public class MyController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showHomePage(ModelMap model) {

        Map<String,String> country = new LinkedHashMap<String,String>();
        country.put("US", "United Stated");
        country.put("CHINA", "China");
        country.put("SG", "Singapore");
        country.put("MY", "Malaysia");
        model.put("countryList", country);
        return "home";
    }
}

home.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%><br>
<html>
<body>
<form:form method="POST" commandName="country">
    <form:select path="country">
        <form:options items="${countryList}" />
    </form:select>
</form:form>
</body>


【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    我过去使用它的方法是创建一个名为 OptionValue 的 bean,它具有两个属性 value 和 description。将 OptionValue 的列表添加到模型中。表单选项标签需要知道要查找哪些属性的值和描述。我在下面添加了一个示例。

    @Controller
    public class MyController {
        @RequestMapping(value = "/", method = RequestMethod.GET)
        public String showHomePage(ModelMap model) {
    
            List<OptionValue> country = new ArrayList<OptionValue>();
            country.add(new OptionValue("US", "United Stated"));
            country.add(new OptionValue("CHINA", "China"));
            country.add(new OptionValue("SG", "Singapore"));
            country.add(new OptionValue("MY", "Malaysia"));
            model.put("countryList", country);
            return "home";
        }
    }
    

    在你的jsp中。

    <form:options items="${countryList}" itemValue="value" itemLabel="description"/>
    

    【讨论】:

      【解决方案2】:

      感谢 Manuel 的澄清,我终于想出了一个实用且令人满意的解决方案。在这里,我将其复制到使其工作的主要组件:

      CountryBean 类(例如 CountryBean.java)

      @Component
      public class CountryBean {
          private String value;
          private String description;
      
      public CountryBean(){
      }
      public CountryBean(String value, String description){
          this.value=value;
          this.description=description;
      }
      
      public String getValue() {
          return value;
      }
      public void setValue(String value) {
          this.value = value;
      }
      public String getDescription() {
          return description;
      }
      public void setDescription(String description) {
          this.description = description;
      }
      }
      

      CountryFormBean 类(例如 CountryFormBean.java)

      public class CountryFormBean {
      
      private CountryBean countryBean;
      
      public setCountryBean (CountryBean countryBean){
          this.countryBean=countryBean;
      }
      
      public CountryBean getCountryBean(){
          return countryBean
      }    
      

      控制器类(例如 MyController.java)

      @Controller
      public class AttendanceController {
      private List<CountryBean> countryBeanList;
      public List<CountryBean> getCountryBeanList() {
          return countryBeanList;
      }
      @Autowired
      public void setCountryBeanList(List<CountryBean>  countryBeanList) {
          this.countryBeanList = countryBeanList;
      }
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String showHomePage(@ModelAttribute("countryFormBean") CountryFormBean countryFormBean, BindingResult result, ModelMap model) {
      
      
          countryBeanList.add(new CountryBean("US", "United Stated"));
          countryBeanList.add(new CountryBean("CHINA", "China"));
          countryBeanList.add(new CountryBean("SG", "Singapore"));
          countryBeanList.add(new CountryBean("MY", "Malaysia"));
      
          model.addAttribute("countryBeanList", countryBeanList);
          return "home";
      }
      }
      

      home.jsp

      <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
      <html>
      <body>
      <form:form method="POST" commandName="countryFormBean">
       <form:select path="countryBean"  items="${countryBeanList}" itemValue="value" itemLabel="description"/>
      </form:form>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多