1

In a jsf page, I'm using 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>

and I want to pass a Bean name so the included content can call a method on the 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>  

the bean is as simple as possible
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";
  }
}

looking at a previous answer on 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)? I was expecting this to work, but it doesn't. The 'This is from included file' text gets printed, but not the BeanA property. Could anyone tell me why?

Eventually I'm wanting to pass to the included file both a bean name and the method name which I want to invoke. For the moment though, I can't even get this simpler example to behave as I'd expect.

Mojarra 2.2.0, Apache Tomcat/7.0.55

Community
  • 1
  • 1
twisted
  • 742
  • 2
  • 11
  • 19
  • 1
    why do you think `` sould work while the link you refer to explicitly uses EL in the param value (`#{beanA`})? And are you sure there are not any errors anywhere? (e.g. if you include an h:messages somewhere or run the app in development mode)? – Kukeltje May 14 '16 at 11:29
  • Kukeltje: I tried , and that didn't work. gives no output. There is no error output to the tomcat error log either. – twisted May 14 '16 at 12:04
  • Where did you define variable `#{greeting}`? If you have no such and it's just the property name, then you should be using `#{myBean.greeting}`. – BalusC May 14 '16 at 12:08
  • BalusC: BeanA has a method getGreeting(). Using #{myBean.greeting} results in an HTTP 500 error: javax.servlet.ServletException: /included.xhtml: Property 'greeting' not found on type java.lang.String – twisted May 14 '16 at 12:18
  • That will indeed happen when you incorrectly pass the bean name as a string (as in your current snippet). You need to specify the bean name as an EL variable as shown in the link you found and in Kukeltje's comment. – BalusC May 14 '16 at 15:03

1 Answers1

2

Thanks for your help Kukeltje & BalusC.

I've got a working example now that demonstrates both passing in a bean and a method name as parameters. For anyone with the same problem, here it is:

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>  

Output:
Passing in beanA as a parameter:
hello from BeanA

Passing in beanB as a parameter:
hello from BeanB

Passing in beanA and method 'hello' as parameters:
hello from BeanA

Passing in beanA and method 'goodbye' as parameters:
goodbye from BeanA

Passing in beanB and method 'hello' as parameters:
hello from BeanB

Passing in beanB and method 'goodbye' as parameters:
goodbye from BeanB

twisted
  • 742
  • 2
  • 11
  • 19