如何将数据从servlet填充到下拉列表

时间:2014-02-19 14:15:25

标签: jsp servlets httpsession

我有一个servlet,我已经返回了一个列表:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("inside doPost of Role Management ---");
        this.context = request.getServletContext();
        UserProperties userProp;
        try{
            userProp= new UserProperties();
            //just a place holder to see if the property file can be read...
            userGroup_DS_Proxy = userProp.ReadProperties("UserGroup_DS_Proxy").toString();
            System.out.println("Properties file location was found in th tomcat folder");
        }catch(Exception ex){
            userProp= new UserProperties(context);
            System.out.println("Fail Safe Mode Activated");
        }
        userGroup_DS_Proxy = userProp.ReadProperties("UserGroup_DS_Proxy").toString();
        roleManagementDebug = Boolean.valueOf(userProp.ReadProperties("RoleManagementDebug").toString());
        RoleManagementDAO roleManagementDAO= new RoleManagementDAO(roleManagementDebug,userGroup_DS_Proxy);

        if(roleManagementDebug){
            System.out.println("new post for Role Management");
        }
        List<String> roles = roleManagementDAO.getRoles();
        HttpSession session = request.getSession(true);
        session.setAttribute("RoleList", roles);
    }

现在我通过在HttpSession中设置它来传递列表: session.setAttribute(“RoleList”,角色);

现在我想在jsp中获取此会话值并使用此会话变量填充下拉列表。 由于我对JSP方面知之甚少,因此我很难解决这个问题。我怎样才能做到这一点?期待您的回答。 提前致谢

1 个答案:

答案 0 :(得分:1)

您可以使用jstl标记并显示列表,而不是尝试使用scriptlet(&lt; %%&gt;)。在你的jsp标题中,

<%@ taglib prefix="c" 
       uri="http://java.sun.com/jsp/jstl/core" %>

您可以查看here了解相关信息。

<c:forEach var="listVar" items="${listName}">  //In your case RoleList 
     <option value ="${listVar.attribute1}">
           <c:out value="${listVar.attribute2}"/>
    </option>
</c:forEach>

希望这有帮助。