Spring mvc Spring形式:选项

Spring mvc Spring形式:选项,spring-mvc,Spring Mvc,是否有任何方法可以将选项标记为默认选中的选项,就像HTML选项中的selected属性一样tag like?如果标记的路径值与选项值的值匹配,它将自动被选中。你不需要什么特别的东西 有没有办法将选项标记为默认选中的 只用 我假设您也在使用Spring MVC。如果您的业务逻辑在默认情况下需要选择某个选项,请将该业务逻辑移动到控制器,而不是JSP @RequestMapping(method = RequestMethod.GET) public ModelAndView helloWorld()

是否有任何方法可以将选项标记为默认选中的选项,就像
HTML选项中的
selected
属性一样
tag like

如果标记的路径值与选项值的值匹配,它将自动被选中。你不需要什么特别的东西

有没有办法将选项标记为默认选中的

只用
我假设您也在使用Spring MVC。如果您的业务逻辑在默认情况下需要选择某个选项,请将该业务逻辑移动到控制器,而不是JSP

@RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("HelloWorldPage");

        // first we need to give the countries list to the model
        model.addObject("countries", countryService.getAllCountries());

        // creating the form
        ExampleForm form = new ExampleForm(); 

        // setting the default to Germany (de)             
        form.setCountryCode = "de";
        // adding the form (with the default country set) to the model
        model.addObject("form", form);

        return model;
}
在JSP中,我们将国家传递给选项,spring将自动选择德国:

<form:form method="post" commandName="form">

    <%-- other fields ... --%>

    <form:select path="countryCode">
        <form:options items="${countries}" itemValue="countryCode" itemLabel="countryName"/>
    </form:select>

    <%-- other fields ... --%>

</form:form>


我理解这一点,但某些业务逻辑需要我在第一次加载时拥有一个默认的选定项(后续加载,选择列表将消失,并由隐藏字段替换)。将其设置为命令对象中该字段的默认值。
@RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld(){

        ModelAndView model = new ModelAndView("HelloWorldPage");

        // first we need to give the countries list to the model
        model.addObject("countries", countryService.getAllCountries());

        // creating the form
        ExampleForm form = new ExampleForm(); 

        // setting the default to Germany (de)             
        form.setCountryCode = "de";
        // adding the form (with the default country set) to the model
        model.addObject("form", form);

        return model;
}
<form:form method="post" commandName="form">

    <%-- other fields ... --%>

    <form:select path="countryCode">
        <form:options items="${countries}" itemValue="countryCode" itemLabel="countryName"/>
    </form:select>

    <%-- other fields ... --%>

</form:form>