0


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

控制器类(例如 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";
    }
}

主页.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>


4

2 回答 2

0

感谢 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";
}
}

主页.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>
于 2013-02-06T14:45:42.893 回答
0

我过去使用的方法是创建一个名为 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"/>
于 2013-02-05T19:10:31.263 回答