我在尝试将托管 bean 作为参数传递给 facelet 模板时遇到了很多麻烦。我正在尝试在 facelet 模板上创建一个变量并通过 ui:param 传递其值,但我经常遇到“目标无法访问,标识符 'bean' 解析为空”错误。我已经尝试将其设置为:
<p:commandButton value="Save" actionListener="#{sessionScope[bean].save}" />
它再次不起作用。
任何人都可以帮助我吗?
这是代码:
crud_template.xhtml
<ui:define name="content">
<h:form>
<ui:insert name="create_form">
<!-- Default -->
</ui:insert>
<br />
<br />
<p:separator />
<p:commandButton value="Save" actionListener="#{bean.save}" />
<p:commandButton value="Cancel" actionListener="#{bean.cancel}" />
</h:form>
</ui:define>
familyPersist.xhtml
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/WEB-INF/templates/crud_template.xhtml">
<ui:param name="bean" value="#{familyBean}" />
<ui:define name="create_form">
<h:panelGrid columns="3">
<h:outputLabel value="Name*:" for="itemName" />
<p:inputText id="itemName" required="true"
value="#{familyBean.item.nmFamily}" />
</h:panelGrid>
</ui:define>
</ui:composition>
</html>
CrudBean.java
public abstract class CrudBean<T> {
protected T item;
private String redirect() {
return "/pages/protected/" + getEntityName() + "List.jsf";
}
public String save() {
return redirect();
}
public String cancel() {
return redirect();
}
public abstract String getEntityName();
/**
* @return the item
*/
public T getItem() {
return item;
}
/**
* @param item the item to set
*/
public void setItem(T item) {
this.item = item;
}
}
FamilyBean.java
@Model
public class FamilyBean extends CrudBean<Family> {
public FamilyBean() {
item = new Family();
}
@Override
public String getEntityName() {
return "Family";
}
}
最后,错误:
22:11:50,178 GRAVE [javax.faces.event] (http-localhost-127.0.0.1-8080-4) javax.el.PropertyNotFoundException: Target Unreachable, identifier 'bean' resolved to null
at org.apache.el.parser.AstValue.getTarget(AstValue.java:98)
at org.apache.el.parser.AstValue.invoke(AstValue.java:244)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:489)
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)
at java.lang.Thread.run(Thread.java:662)
编辑:
我使用这个 Maven 命令创建我的项目:
mvn archetype:generate \
-DarchetypeArtifactId=jboss-javaee6-webapp-archetype \
-DarchetypeGroupId=org.jboss.spec.archetypes \
-DarchetypeVersion=7.1.1.CR2