如何填充Spring MVC中的下拉列表

时间:2010-05-31 11:18:34

标签: spring-mvc

想在jsp页面上填充下拉列表

我的页面看起来像

 <form:form method="POST" action="addRss.htm" commandName="addNewRss" cssClass="addUserForm">
                <div class="floatL">
                    <div class="padding5">
                        <div class="fieldContainer">
                            <strong>Title:</strong>&nbsp;
                        </div>
                        <form:errors path="title" cssClass="error"/>
                        <form:input path="title" cssClass="textArea" />
                    </div>

                    <div class="padding5">
                        <div class="fieldContainer">
                            <strong>Description:</strong>&nbsp;
                        </div>
                        <form:errors path="description" cssClass="error"/>
                        <form:input path="description" cssClass="textArea"  />
                    </div>
                    <div class="padding5">
                        <div class="fieldContainer">
                            <strong>Language:</strong>&nbsp;
                        </div>
                        <form:errors path="language" cssClass="error"/>
                        <form:select path="language" cssClass="textArea"  />
                    </div>
                </div>
                <div class="floatR">
                   <div class="padding5">
                        <div class="fieldContainer">
                            <strong>Link:</strong>&nbsp;
                        </div>
                        <form:errors path="link" cssClass="error"/>
                        <form:input path="link" cssClass="textArea" />
                    </div>
                    <div class="padding5">
                        <div class="fieldContainer">
                            <strong>Url:</strong>&nbsp;
                        </div>
                        <form:errors path="url" cssClass="error"/>
                        <form:input path="url" cssClass="textArea" />
                    </div>
                    <div class="padding5">
                        <div class="fieldContainer">
                            <strong>Url</strong>&nbsp;
                        </div>
                        <form:errors path="url" cssClass="error"/>
                        <form:input path="url" cssClass="textArea" />
                    </div>
                </div>
                <input type="submit" class="floatR" value="Add New Rss">
            </form:form>

和我的控制器

public class AddRssController extends BaseController {

    private static final String[] LANGUAGES = { "AL", "AK", "AZ", "AR", "CA",
         "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA",
         "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT",
         "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR",
         "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WV",
         "WI", "WY" };

    public AddRssController() {
      setCommandClass(RSS.class);
      setCommandName("addNewRss");
   }

      @Override
   protected Object formBackingObject(HttpServletRequest request)
                     throws Exception {
      RSS rantForm = (RSS) super.formBackingObject(request);
     // rantForm.setVehicle(new Vehicle());
      return rantForm;
   }

   @Override
   protected Map referenceData(HttpServletRequest request) throws Exception {
      Map referenceData = new HashMap();

      referenceData.put("language", LANGUAGES);

      return referenceData;
   }

   @Override
   protected ModelAndView onSubmit(Object command, BindException bindException)
                     throws Exception {
      RSS rss = (RSS) command;
      rssServiceImplementation.add(rss);
      return new ModelAndView(getSuccessView());
   }
}

和我的BaseController

public class BaseController extends SimpleFormController implements Controller {

   public UserServiceImplementation userServiceImplementation;

   public UserServiceImplementation getUserServiceImplementation() {
        return userServiceImplementation;
   }

   public void setUserServiceImplementation(UserServiceImplementation userServiceImplementation) {
        this.userServiceImplementation = userServiceImplementation;
   }

   public RssServiceImplementation rssServiceImplementation;

   public RssServiceImplementation getRssServiceImplementation() {
        return rssServiceImplementation;
   }

   public void setRssServiceImplementation(RssServiceImplementation rssServiceImplementation) {
        this.rssServiceImplementation = rssServiceImplementation;
   }

}

但它不起作用

有什么建议吗?

3 个答案:

答案 0 :(得分:5)

我解决了问题

<form:select path="language" cssClass="textArea">
     <option value="Select" label=" - Select - " ></option>
     <form:options items="${languages}"/>
</form:select>

答案 1 :(得分:0)

下面是下拉列表中显示数据的代码。

<form:select path="language">
        <form:options items="${languages}" />
</form:select>

其他有用的链接。

1。mkyong example for dropdown of Spring MVC

2。similar stackoverflow question

答案 2 :(得分:0)

您也可以尝试这种方法。

<div class="">
        <form:label path="language" for="language"></form:label>
        <form:select path="language" placeholder="Language" id="language"
            class="">
                <c:forEach items="${languages}" var="language">
                   <form:option value="${language}">${language} 
                   </form:option>
                </c:forEach>
        </form:select>
</div>