How to access Map in JSF

隐身守侯 提交于 2019-12-08 03:43:53

问题


I have try to access the map in jsf using C:foreach.But i can't access that in jsf by using"#" symbol.But i can access it using "$" symbol.But i need to access to displayed using jsf component h:outputtext.My sample code is

h:form binding="#{MapInJsf.initForm}">
                <c:forEach items="#{MapInJsf.nameMap}" var="nameMap">
                    <%--<li>${nameMap.key}</li>--%> I cann access it
                    <%--<h:outputText value="#{nameMap}"/>--%>
                    <h:outputText value="Name2 : #{nameMap.key}"/>
                   <h:outputText value="Last Name1 : #{nameMap.value}"/>
                </c:forEach>
            </h:form>

what i do wrong and how can i access that?My Ref link is Dynamic value binding of JSF component Please help me.


回答1:


This will only work when you're using JSP 2.1 or newer (Servlet 2.5 or newer). On JSP 2.0 or older (Servlet 2.4 or older), it is not possible to reference the c:forEach variable by deferred EL #{}. The deferred EL #{} was namely initially part of JSF 1.0 and was only later integrated in JSP 2.1, around the time JSF 1.2 was released. You should however be able to use standard EL ${}.

<c:forEach items="${MapInJsf.nameMap}" var="nameMap">
    <h:outputText value="Name2 : ${nameMap.key}"/>
    <h:outputText value="Last Name1 : ${nameMap.value}"/>
</c:forEach>

But you won't be able to bind it to an UIInput component like <h:inputText> since the ${} notation only calls bean getter, not setter.

If you're running a servlet 2.5 compatible container (like Tomcat 6.0 and up), then you need to ensure that your web.xml is declared as per Servlet 2.5. I.e. the root declaration must be as follows:

<web-app 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="YourWebAppID"
    version="2.5">

Only then you will be able to use deferred EL #{} on JSP tags like JSTL.

See also:

  • Java/JSP article on The Unified Expression Language


来源:https://stackoverflow.com/questions/4551132/how-to-access-map-in-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!