JSF accessing backing map object

2019-08-01 16:18发布

问题:

I have a jsp subview page that I have passed a parameter to and I want to then pass that parameter to a map's get() method that is stored in a session bean.

Ex:

<h:panelGrid id="panelGrid1" rendered="#{MySessionBean[param.id].showPanelGrid1}">
...
</h:panelGrid>

In the above example MySessionBean implements the Map interface and I have my own custom get method that will create an object and put it in the map if none exists for the key [params.id]. When I run the code in debug mode my get method for MySessionBean never gets called and my panel is always rendered. Am I not passing parameters correctly? Or accessing the parameter passed to the subview correclty?

Here is how I passed the parameter to this subview:

<f:subview id="subview1">
  <jsp:include page="/MyTemplatePage.jsp">
    <jsp:param name="id" value="staticUniqueId1"/>
  </jsp:include>
</f:subview>

The reason I'm trying to do this is so I can include this template subview multiple times in a single page so that each instance won't have the same backing bean objects. Thus using a map in the session and passing it an id to gain access to the backing beans for each instance.

Also, I am limited JSF 1.2, JSTL 1.1, JBoss 4.0.4. So I can't use answers that use RichFaces or JSF 2.

EDIT: 11/22/11 11:23

I Replaced the [param.id] with a static string value.

<h:panelGrid id="panelGrid1" rendered="#{MySessionBean.MY_TEMP_VAL.showPanelGrid1}">
  ...
</h:panelGrid>

And everything worked. It triggered my map get method and accessed the session beans and everything. So it is clearly not liking the whole using [params.id] passing to the map object. Not sure what to do from here.

回答1:

In JSF2 the proper and easy solution would be to use composite components. Since you are stuck with JSF 1.2 and jsp you could use tag files instead. These are like regular jsps but with the extension tag or tagx and placed under WEB-INF/tags. I'm using the xml syntax in the example below, in a file name example.tagx:

<jsp:root version="2.1"
          xmlns:jsp="http://java.sun.com/JSP/Page"
          xmlns:h="http://java.sun.com/jsf/html">
    <jsp:directive.attribute name="myBean"
                             required="true"
                             rtexprvalue="false"
                             deferredValue="true"
                             deferredValueType="com.example.MyBean"/>
    <h:panelGrid id="panelGrid1" rendered="#{myBean.showPanelGrid1}">
    ...
    </h:panelGrid>
</jsp:root>

In a jspx you then have to declare the namespace like xmlns:myTags="urn:jsptagdir:/WEB-INF/tags/", in a jsp the syntax would be:

<%@taglib tagdir="/WEB-INF/tags" prefix="myTags" %>

The custom tag can then be used multiple times on a page and the right backing bean can be passed as an attribute like this:

<myTags:example myBean="#{myBeanInstance1}" />

Edit: You might also need a file WEB-INF/tags/implicit.tld to specify the version:

<?xml version = '1.0' encoding = 'UTF-8'?>
<taglib 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-jsptaglibrary_2_1.xsd"
        version="2.1" xmlns="http://java.sun.com/xml/ns/javaee">
        <tlib-version>2.1</tlib-version>
</taglib>


标签: jsf map param