2013-05-08 43 views
1

我在嘗試將託管bean作爲param傳遞給facelet模板時遇到了很多麻煩。 我想在facelet模板中創建一個變量,並通過ui:param傳遞它的值,但我經常將「Target Unreachable,identifier'bean'解析爲null」錯誤。 我已經tryed來設置它像:如何將託管bean作爲param傳遞給facelet模板

<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 

回答

0

我從文件中讀出什麼@Model與RequestScoped相當於@ManagedBean。

所以,如果我嘗試修復一樣,我可以這樣做:

UI:PARAM

<ui:param name="bean" value="familyBean" /> 

crud_template.xhtml

<p:commandButton value="Save" actionListener="#{requestscope[bean].save}" /> 

嘗試這一點,這應該工作。

編輯:

我曾嘗試複製相同,並能夠做得出來。

我在這裏發佈示例代碼供您參考。

還想分享一件事: 如果bean不在xhtml中注入一次,那麼您將無法從中獲取該引用。

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> 

樣品測試頁:

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

後端bean類:(與原型不工作)

@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(); 
    } 
} 

修訂bean類

@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; 
} 
} 

我還在請求範圍中顯示了所有可用的變量,這將有助於確定被查找的密鑰是否實際可用。

更新 這是我目前爲止的觀察結果。如果嘗試連線@Named,@Model(它不適用於UI,意味着無法在xhtmls中解析)。

CDI更喜歡將注入服務注入控制器或ManagedBean。

例如樣品展示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 公共類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; 
} 
} 
+0

那離開我後續的錯誤:'的ActionListener = 「#{requestScope [豆] .save}」 :目標無法訪問,'familyBean'返回空' – 2013-05-08 23:32:50

+0

我已經更新了一個示例代碼看看那個。 – 2013-05-09 05:10:15

+0

我試着執行你傳遞給我的確切代碼,但它又給了我同樣的錯誤:'/WEB-INF/templates/testTemplate.xhtml @ 26,54 listener =「#{requestScope [beantt] .convertNameToUpperCase2}」: Target Unreachable,'helloWorld'returned null:javax.el.PropertyNotFoundException:/WEB-INF/templates/testTemplate.xhtml @ 26,54 listener =「#{requestScope [beantt] .convertNameToUpperCase2}」:目標無法訪問,'helloWorld'返回null',那麼代碼如何在那裏運行得很漂亮,這裏不要......可能pom.xml導入的是你的不同的依賴關係嗎? – 2013-05-10 00:42:47

1

我嘗試了所有上述建議,但沒有運氣。 終於爲我工作的唯一事情是在調用方法時使用括號'()'。我想該屬性沒有發現異常應該是一個線索,它混淆了財產與方法。我認爲這是facelets的一個bug,因爲它與傳統的JSF actionListener和動作方法調用不一致。

上面的例子應該一起工作:

<p:commandButton value="Save" actionListener="#{bean.save()}" />

其中

<ui:param name="bean" value="#{familyBean}" />

相關問題