JSF访问支持地图对象 [英] JSF accessing backing map object

查看:147
本文介绍了JSF访问支持地图对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsp子视图页面,我已经传递一个参数,然后我要将该参数传递给存储在会话bean中的地图的get()方法。

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.

例如:

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

在上面的示例中,MySessionBean实现了Map界面,我有自己的定制get方法,将创建一个对象,并将其放在地图中(如果不存在该键[params.id])。当我在调试模式下运行代码时,MySessionBean的get方法从未被调用,我的面板始终呈现。我没有正确传递参数吗?或访问传递给子视图相关参数的参数?

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>

我试图这样做的原因是,我可以在一个单个页面,使每个实例将不具有相同的后台bean对象。因此,在会话中使用一个地图并传递一个id来获取对每个实例的支持bean的访问。

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.

此外,我受限于JSF 1.2,JSTL 1.1,JBoss 4.0.4。所以我不能使用RichFaces或JSF 2的答案。

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.

编辑:11/22/11 11:23

我用静态字符串值替换了[param.id]。

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

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

一切都奏效。它触发了我的地图获取方法,并访问了会话bean和所有内容。所以显然不喜欢整个使用[params.id]传递给地图对象。不确定从这里做什么。

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.

推荐答案

在JSF2中,正确和容易的解决方案是使用复合组件。由于您遇到JSF 1.2和jsp,您可以使用标签文件而不是这些就像常规的jsps,但扩展名为标签 tagx 并放在 WEB-INF /标签。我在下面的示例中使用xml语法,文件名为 example.tagx

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>

在jspx中,您必须声明命名空间,如 xmlns:myTags = urn:jsptagdir:/ WEB-INF / tags /,在jsp中的语法是:

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" %>

自定义标签可以在页面上多次使用,正确的备份bean可以作为一个这样的属性:

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}" />

编辑:您可能还需要一个文件 WEB-INF / tags / implicit.tld 指定版本:

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访问支持地图对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆