在Spring mvc中加载页面时无法加载下拉列表

时间:2012-06-14 07:14:37

标签: spring spring-mvc

我在页面加载时尝试填充下拉列表。但它没有从Controller.on提交方法加载到UserPage.jsp中,也写了referencedata方法。

控制器: -

public ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {

        log.info("onSubmit handleRequest method"
                + request.getParameter("username"));
        System.out.println("onSubmit handleRequest method"
                + request.getParameter("username"));
        String username = "", password = "";
        username = request.getParameter("username");
        password = request.getParameter("password");

        UserBean ubean = null;

        System.out.println("After shownform method called");
        HttpSession session = request.getSession(true);
        try {
            ubean = userservice.chkUsername(username, password);
            System.out.println("Information" + ubean.getUsername());
        } catch (DataException ex) {
            ex.printStackTrace();

            // throw ex;
        }
        session.setAttribute("User", ubean);
        EmpPersonalBean personalBean = new EmpPersonalBean();
        return new ModelAndView("jsp/UserPage", "EmpPersonalBean", personalBean);
    }
                 protected Map referenceData(HttpServletRequest request) throws Exception {
        log.info("UserDBBoardController======================referenceData");
        Map referenceData = new HashMap();
        List deparementList = new ArrayList();
        deparementList = userservice.getDeparmentList();
        referenceData.put("deparmentList", deparementList);
        return referenceData;

    }

UserPage.jsp

<%@ page language="java" import="com.aims.bean.*,java.util.HashMap" contentType="text/html;charset=utf-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<html>
<head>
<title>AAI</title>
</head>
<body>
<form:form method="post" modelAttribute="EmpPersonalBean" action="userpage.htm">
<table>
<tr>
    <td>Welcome <%=((UserBean)session.getAttribute("User")).getUsername()%></td>
</tr>
        <tr>

        <td>Department</td>
        <td><form:select path="deparment">
                      <form:option value="NONE" label="--- Select ---" />
                      <form:options items="${deparmentList}" />
                       </form:select>
        </td>
    </tr>
</tr>
</table>
</form:form>
</body>
</html>


public class DepartmentBean {
private String deptcode,deptname;

public String getDeptcode() {
    return deptcode;
}

public void setDeptcode(String deptcode) {
    this.deptcode = deptcode;
}

public String getDeptname() {
    return deptname;
}

public void setDeptname(String deptname) {
    this.deptname = deptname;
}

}

还在userpage.sjp enter image description here

中附加显示下拉列表

请帮助我。如何解决问题。

2 个答案:

答案 0 :(得分:0)

   <td>Department</td>
    <td><form:select path="deparment">
         <form:option value="NONE" label="--- Select ---" />
          <c:forEach var="department" items="${deparmentList}">
            <form:option value="${department}" label="${department}" />
          </c:forEach>
          </form:select>
    </td>





ModelAndView mav = new ModelAndView("viewName");


mav.addObject("deparmentList", deparementList);


return mav;

返回modelAndView对象。

答案 1 :(得分:0)

您还需要在<form:options/>代码中指定 itemLabel itemValue 属性。

<强>更新

在jsp页面中替换此行。我认为它应该可以解决你的问题。

<form:options items="${deparmentList}" itemLabel="deptname" itemValue="deptcode" />

希望这会对你有所帮助。欢呼声。