如何使用db中的JSTL在下拉列表中设置值

时间:2014-10-28 07:49:34

标签: jsp servlets jstl

我已经使用JSTL在下拉列表中填充了ArrayList()数据。当我尝试使用getter方法'指定'(提交的db值)从bean设置值。发生异常。 请你的建议。 在此先感谢... !!

Servlet属性

        request.setAttribute("RESULT2", lDesignation);

JSTL代码

        <jsp:useBean id="userprofile" class= "com.package.dao.UserProfile" scope="request"/> 
        <jsp:setProperty name="userprofile" property="*" />

        <strong>Designation</strong>: 

        <select id="designate" name="designate">
                <option value="desigType">Select Designation</option>
        <c:forEach var="desig" items="${RESULT2}">
              <option value="${desig.key}" ${desig.key == ${userprofile.designate ? 'selected="selected"' : ''}>${desig.value}</option>
        </c:forEach>

        </select>  <br> 

例外

    javax.el.PropertyNotFoundException: Property 'key' not found on type java.lang.String
        at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:266)
        at javax.el.BeanELResolver$BeanProperties.access$300(BeanELResolver.java:243)
        at javax.el.BeanELResolver.property(BeanELResolver.java:353)
        at javax.el.BeanELResolver.getValue(BeanELResolver.java:97)
        at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
        at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
        at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
        atorg.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:967)
        at org.apache.jsp.home_jsp._jspx_meth_c_005fforEach_005f0(home_jsp.java:510)
        at org.apache.jsp.home_jsp._jspService(home_jsp.java:279)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
        atorg.apache.jasper.JasperException: An exception occurred processing JSP page /home.jsp at line 158

    155:            <strong>Designation</strong>: <select id="designate" name="designate">
    156:                    <option value="desigType">Select Designation</option>
    157:                <c:forEach var="desig" items="${RESULT2}">
    158:                    <option value="${desig.key}" ${desig.key == ${userprofile.designate ? 'selected="selected"' : ''}>${desig.value}</option>
    159:                </c:forEach>
    160:                
    161:                </select>  <br> 

1 个答案:

答案 0 :(得分:0)

通过将bean值设置为变量并使用foreach循环中的if条件进行检查来找到答案。          

     <strong>Designation</strong>: 
            <select id="designate" name="designate">
                <option value="desigType">Select Designation</option>
                <option value="${selectedDesig}" selected>${selectedDesig}</option>
                    <c:forEach var="designate" items="${RESULT2}">
                        <c:if test="${designate != selectedDesig}">
                            <option value="${designate}">${designate}</option>
                        </c:if>
                    </c:forEach>
            </select>  <br>