Spring 3.x.x MVC jsp组合框出现问题

时间:2013-02-05 18:50:19

标签: java spring spring-mvc

我在使用Spring-MVC 3.x.x和jsp尝试获得一个简单的组合框(由表单:select制作)时,已经进行了几天没有任何进展。有几个例子通过扩展现在已弃用的“SimpleFormController”来实现,但是我没有找到任何使用Spring 3.0.x注释的简洁示例。另外,我已经查看了Spring的参考documentation,但我无法获得控制器和视图(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";
    }
}

针对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>


2 个答案:

答案 0 :(得分:0)

我过去使用它的方法是创建一个名为OptionValue的bean,它包含两个属性值和描述。将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"/>

答案 1 :(得分: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";
}
}

<强>针对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>