【问题标题】:jsf passing bean as parameter to included xhtml [duplicate]jsf将bean作为参数传递给包含的xhtml [重复]
【发布时间】:2016-09-10 14:20:48
【问题描述】:

在 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>  

bean 尽可能简单
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)? 我原以为这会起作用,但事实并非如此。 'This is from included file' 文本被打印,但不是 BeanA 属性。谁能告诉我为什么?

最终我想将 bean 名称和我想要调用的方法名称都传递给包含的文件。但目前,我什至无法让这个更简单的示例按预期运行。

Mojarra 2.2.0, Apache Tomcat/7.0.55

【问题讨论】:

  • 为什么您认为&lt;ui:param name="myBean" value="beanA" /&gt; 可以工作,而您引用的链接在参数值(#{beanA})中明确使用 EL?你确定任何地方都没有错误吗? (例如,如果您在某处包含 h:messages 或在开发模式下运行应用程序)?
  • Kukeltje:我试过 ,但没有奏效。 没有输出。 tomcat错误日志也没有错误输出。
  • 你在哪里定义了变量#{greeting}?如果你没有这样的,它只是属性名称,那么你应该使用#{myBean.greeting}
  • BalusC:BeanA 有一个方法 getGreeting()。使用 #{myBean.greeting} 会导致 HTTP 500 错误:javax.servlet.ServletException: /included.xhtml: Property 'greeting' not found on type java.lang.String
  • 当您错误地将 bean 名称作为字符串传递(如在当前的 sn-p 中)时,确实会发生这种情况。您需要将 bean 名称指定为 EL 变量,如您找到的链接和 Kukeltje 的评论中所示。

标签: jsf jsf-2


【解决方案1】:

感谢您的帮助 Kukeltje & 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

【讨论】:

    猜你喜欢
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多