jsf将bean作为参数传递给包含的xhtml

时间:2016-05-14 11:26:42

标签: jsf jsf-2

在jsf页面中,我使用了ui:include。
的index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 
  xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"> 
<h:head>
  <title>test</title>
</h:head>
<h:body> 
  <ui:include src="included.xhtml" >
    <ui:param name="myBean" value="beanA"  />
  </ui:include>  
</h:body>
</html>

我希望传递一个Bean名称,以便包含的内容可以调用bean上的方法 的 included.xhtml

<ui:composition
  xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:ui="http://java.sun.com/jsf/facelets">
  This is from included file.<br/>
  #{myBean[greeting]}
</ui:composition>  
豆子尽可能简单 的 BeanA.java:

package com.test;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;

@ManagedBean
public class BeanA implements Serializable{
  public String getGreeting(){
    return "hello from BeanA";
  }
}

查看stackoverflow上的上一个答案: JSF 2: how to pass an action including an argument to be invoked to a Facelets sub view (using ui:include and ui:param)? 我期待这个工作,但它没有。 &#39;这是来自包含的文件&#39;文本被打印,但不是BeanA属性。谁能告诉我为什么?

最终我想要将包含的bean名称和我想要调用的方法名称传递给包含文件。但就目前而言,我甚至无法通过这个更简单的例子来表现我的期望。

Mojarra 2.2.0, Apache Tomcat / 7.0.55

1 个答案:

答案 0 :(得分:1)

感谢您的帮助Kukeltje&amp; BalusC。

我现在有一个工作示例,它演示了传入bean和方法名称作为参数。对于有同样问题的人,这里是:

<强> BeanA.java:

package com.test;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;

@ManagedBean
public class BeanA implements Serializable{
  public String getHello(){
    return "hello from BeanA";
  }
  public String getGoodbye(){
    return "goodbye from BeanA";
  }
}

<强> BeanB.java:

package com.test;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;

@ManagedBean
public class BeanB implements Serializable{
  public String getHello(){
    return "hello from BeanB";
  }
  public String getGoodbye(){
    return "goodbye from BeanB";
  }
}

<强>的index.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html 
  xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"> 
<h:head>
  <title>test</title>
</h:head>
<h:body> 

  Passing in beanA as a parameter:<br/>   
  <ui:include src="included1.xhtml" >
    <ui:param name="myBean" value="#{beanA}"  />
  </ui:include>  
  <hr/>
  Passing in beanB as a parameter:<br/>
  <ui:include src="included1.xhtml" >
    <ui:param name="myBean" value="#{beanB}"  />
  </ui:include>  
  <hr/>
  Passing in beanA and method 'hello' as parameters:<br/>
  <ui:include src="included2.xhtml" >
    <ui:param name="myBean" value="#{beanA}"  />
    <ui:param name="myMethod" value="hello"  />
  </ui:include> 
  <hr/>
  Passing in beanA and method 'goodbye' as parameters:<br/>
  <ui:include src="included2.xhtml" >
    <ui:param name="myBean" value="#{beanA}"  />
    <ui:param name="myMethod" value="goodbye"  />
  </ui:include> 
  <hr/>
  Passing in beanB and method 'hello' as parameters:<br/>
  <ui:include src="included2.xhtml" >
    <ui:param name="myBean" value="#{beanB}"  />
    <ui:param name="myMethod" value="hello"  />
  </ui:include> 
  <hr/>
  Passing in beanB and method 'goodbye' as parameters:<br/>
  <ui:include src="included2.xhtml" >
    <ui:param name="myBean" value="#{beanB}"  />
    <ui:param name="myMethod" value="goodbye"  />
  </ui:include> 
  <h:messages />

</h:body>
</html>

<强> included1.xhtml:

<ui:composition
  xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:ui="http://java.sun.com/jsf/facelets">

  #{myBean.hello}

</ui:composition>  

<强> included2.xhtml:

<ui:composition
  xmlns="http://www.w3.org/1999/xhtml"   
  xmlns:ui="http://java.sun.com/jsf/facelets">

  #{myBean[myMethod]}

</ui:composition>  

<强>输出:
传入beanA作为参数:
你好,来自BeanA

传入beanB作为参数:
你好,来自BeanB

传入beanA和方法'hello'作为参数:
你好,来自BeanA

传入beanA和方法'goodbye'作为参数:
来自BeanA的再见

传入beanB和方法'hello'作为参数:
你好,来自BeanB

传入beanB和方法'goodbye'作为参数:
来自BeanB的再见