What I read from the documents, @Model is equivalent to @ManagedBean with RequestScoped.
So if I try to fix the same, I can do something like:
ui:param
<ui:param name="bean" value="familyBean" />
In crud_template.xhtml:
<p:commandButton value="Save" actionListener="#{requestscope[bean].save}" />
Try this out, this should work.
Edit:
I did try to replicate the same, and was able to work it out.
I am posting the sample code here for your reference.
Also would like to share one thing: If the bean is not inject once inside the xhtml, you will not be able to get the reference out of it.
Template page:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j">
<h:head></h:head>
<h:form>
<ui:insert name="create_form">
<!-- Default -->
</ui:insert>
<br />
<br />
<h:commandLink id="link" value="Name Converted">
<f:ajax event="click" listener="#{requestScope[beantt].convertNameToUpperCase2}" execute="dateOfExpense" render="dateOfExpense" />
</h:commandLink>
</h:form>
</ui:composition>
Sample test page:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j">
<f:view>
<h:body>
<ui:composition template="testTemplate.xhtml">
<ui:param name="beantt" value="helloWorld" />
<ui:define name="create_form">
<h:panelGrid columns="3">
<h:outputLabel value="Name*:" for="itemName" />
<h:inputText size="20" value="#{helloWorld.name}" id="dateOfExpense"/>
<h:outputText value="#{requestScope}" />
</h:panelGrid>
</ui:define>
</ui:composition>
</h:body>
</f:view>
</ui:composition>
Backend bean class: (Not working with archetype)
@Named("helloWorld") //@Model was not available in my project
public class HelloWorldBean {
private String name;
//Getters and Setters
Logger LOG = LoggerFactory.getLogger(HelloWorldBean.class);
public void convertNameToUpperCase2(AjaxBehaviorEvent event) {
LOG.debug("Inside Bean");
name = name.toUpperCase();
}
}
UPDATED BEAN CLASS
@javax.faces.bean.ManagedBean(name="helloWorld")
@javax.enterprise.context.RequestScoped
public class HelloWorldBean {
private String name;
// Getters and Setters
// Logger LOG = LoggerFactory.getLogger(HelloWorldBean.class);
public void convertNameToUpperCase2(AjaxBehaviorEvent event) {
// LOG.debug("Inside Bean");
name = name.toUpperCase();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I have also displayed all the available variables in request scope, this will help in finding out whether the key being looked for was actually available or not.
Update This is my observation so far. If you try to wire @Named, @Model (it doesn't work well with UI, means cannot resolve in xhtmls).
CDI is more like, injecting services into the controllers or here ManagedBean.
e.g. Sample demonstrating the CDI:
@Named("testCon")
public class TestConsumer {
public String convertNameToUpperCase2(String name) {
// LOG.debug("Inside Bean");
return name.toUpperCase();
}
}
@javax.faces.bean.ManagedBean(name="helloWorld") @javax.enterprise.context.RequestScoped public class HelloWorldBean {
@Inject
@Named("testCon")
private TestConsumer test;
private String name;
// Getters and Setters
// Logger LOG = LoggerFactory.getLogger(HelloWorldBean.class);
public void convertNameToUpperCase2(AjaxBehaviorEvent event) {
name = test.convertNameToUpperCase2(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}