java - 数据绑定(bind)到 Spring MVC 中的下拉框

标签 java jsp spring-mvc

我有一个简单的 spring mvc web 应用程序。我想将列表绑定(bind)到下拉列表中。在这种情况下,列表项通常绑定(bind)到下拉列表中;但如果我选择项目并单击提交按钮,它总是传递“0”而不是值。

这是我的 Controller 。

public EditEmployeeController() {
        setCommandClass(Employee.class);
        setCommandName("employee");
}

@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
    Map referenceData = new HashMap();        
    List roleList = roleService.listRoles();
    referenceData.put("roleList", roleList);
    return referenceData;
}

@Override
protected ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response,
        Object command,
        BindException errors) throws Exception {
    Employee employee = (Employee) command;
    employeeService.updateEmployee(employee);
    return new ModelAndView(getSuccessView());
}

这是我的 View (jsp)

<c:nestedPath path="employee">
            <form method="post" action="./EditEmployee.htm">
........
.......

Select Role :                
<c:bind path="roleID">
 <select name="roleLists">
   <s:forEach items="${roleList}" var="role" >
       <option value="<s:out value="${role.roleID}"/>" <s:if test="${role.roleID == status.value}"> selected="selected"</s:if>><s:out value="${role.title}"/></option>
        </s:forEach>
  </select>
</c:bind>
<input type="submit" value="Update employee"/
</form>
</c:nestedPath>

有什么办法可以解决我的问题……??

提前致谢!

最佳答案

使用Spring表单标签

添加<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>在你的jsp中

并使用下面的代码

<form:select path="role"  id="role" >
    <form:options items="${roleList}" itemValue="roleID" itemLabel="title"/>
</form:select>

关于java - 数据绑定(bind)到 Spring MVC 中的下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3886087/

相关文章:

java - Spring Boot中使用Userole访问JSP页面的权限

java - Spring/Hibernate项目@Transactional注释值不被使用并自动替换

java - 我该如何解决这个: The method received in the request-line is known by the origin server but not supported by the target resource

java - 无法将 Gradle 构建为 Make 项目

java - 一个post请求对象可以在另一个post请求中转发吗?

java - modelAndView返回后执行任务

java - 从scala访问java二维 vector

java - 从表中的按钮调用 servlet

rest - 运行时tomcat启动时带有HOT部署: Groovy script does not load dynamically from applicationContext. xml的Spring/REST应用程序

java - 创建 Liferay portlet 配置页面