0

JSFページでこのコードを使用します

<h:outputText value="#{writeBean.t}" />

(そして私はWEB-INFフォルダーにbeans.xmlを持っています)

これは、後者のStringBeanが注入されるWriteBeanです。

package huhu.main.managebean;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
import javax.inject.Named;


@Default
@Named
@SessionScoped
public class WriteBean implements Serializable {
   /**
 * 
 */
private static final long serialVersionUID = 1L;

String t;

@Inject StringBean stringbean;


public WriteBean() {
  setT(stringbean.getS());
}

public String getT() {
  return t;
}

public void setT(String t) {
  this.t = t;
}
}

これは、JSFページに表示する文字列が設定されているStringBeanです。

package huhu.main.managebean;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Default;
import javax.inject.Named;

@Default
@Named
@SessionScoped
public class StringBean implements Serializable {

   /**
* 
*/
   private static final long serialVersionUID = 1L;

   String s = new String("WORKS!!");

   public StringBean(){

   }

   public String getS() {
     return s;
   }

   public void setS(String s) {
  this.s = s;
  }
}

これは例外です:

    HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.RuntimeException: java.lang.NullPointerException
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
    org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)
root cause

com.google.common.collect.ComputationException: java.lang.RuntimeException: java.lang.NullPointerException
    com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:397)
    org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:102)
    org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:115)
    org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:96)
    org.jboss.as.weld.webtier.jsf.ForwardingELResolver.getValue(ForwardingELResolver.java:46)
    javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
    com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72)
    org.apache.el.parser.AstValue.getValue(AstValue.java:147)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
    javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)
root cause

java.lang.RuntimeException: java.lang.NullPointerException
    org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:186)
    org.jboss.weld.util.reflection.SecureReflections.newInstance(SecureReflections.java:340)
    org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:240)
    org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:87)
    org.jboss.weld.bean.proxy.ClientProxyProvider.access$000(ClientProxyProvider.java:43)
    org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:53)
    org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:46)
    com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:355)
    com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:184)
    com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153)
    com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69)
    com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:393)
    org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:102)
    org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:115)
    org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:96)
    org.jboss.as.weld.webtier.jsf.ForwardingELResolver.getValue(ForwardingELResolver.java:46)
    javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
    com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72)
    org.apache.el.parser.AstValue.getValue(AstValue.java:147)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
    javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)
root cause

java.lang.NullPointerException
    huhu.main.managebean.WriteBean.<init>(WriteBean.java:25)
    huhu.main.managebean.WriteBean$Proxy$_$$_WeldClientProxy.<init>(WriteBean$Proxy$_$$_WeldClientProxy.java)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance0(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    org.jboss.weld.util.reflection.SecureReflections$16.work(SecureReflections.java:343)
    org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)
    org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:173)
    org.jboss.weld.util.reflection.SecureReflections.newInstance(SecureReflections.java:340)
    org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:240)
    org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:87)
    org.jboss.weld.bean.proxy.ClientProxyProvider.access$000(ClientProxyProvider.java:43)
    org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:53)
    org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:46)
    com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:355)
    com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:184)
    com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153)
    com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69)
    com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:393)
    org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:102)
    org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:115)
    org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:96)
    org.jboss.as.weld.webtier.jsf.ForwardingELResolver.getValue(ForwardingELResolver.java:46)
    javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
    com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72)
    org.apache.el.parser.AstValue.getValue(AstValue.java:147)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    javax.faces.component.UIOutput.getValue(UIOutput.java:169)
    com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355)
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
    javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)
note The full stack trace of the root cause is available in the JBoss Web/7.0.13.Final logs.

JBoss Web/7.0.13.Final
4

2 に答える 2

3

そのメソッドを作成する必要があります(コンストラクターを置き換えます):

@PostConstruct
public void init() {
    setT(stringbean.getS());
}

からjavax.annotation.PostConstruct、CDIがBeanを作成した直後に呼び出されます。

于 2012-11-13T15:50:24.517 に答える
1

私のせいは単純だと思います。コンストラクターで挿入されたプロパティを呼び出すことはできません。これは、まだ設定されていないプロパティにアクセスしようとすることを意味するためです。ここで、WriteBeanコンストラクターを空にして、新しいメソッドwriter()でコード行を使用しました。

public String writer(){
    setT(stringbean.getS());
    return t;
}

私がjsfページで呼び出したもの:

<h:outputText value="#{writeBean.writer()}" />
于 2012-10-13T09:48:30.373 に答える