JSF2.0 @ManagedProperty - Target Unreachable返回null

时间:2013-03-29 14:10:43

标签: java jsf jsf-2

我在JSF 2.0中使用@ManagedProperty,但我遇到了以下问题。

实体类

@Entity(name="UserDetail")
@Table(name="in_user")
@SessionScoped
public class UserDetail implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    private Integer no;
    private String userName;
    private String password;


    public Integer getNo() {
        return no;
    }
    public void setNo(Integer no) {
        this.no = no;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}

控制器

@ManagedBean(name="authenticator")
@RequestScoped
public class Authenticator implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManagedProperty(value = "#{userDetail}")
    private UserDetail userDetail;

        // Some Code......
}

.XHTML

<p:panel header="Application Login" toggleable="true" toggleOrientation="horizontal" style="width:400px">
                <p:panelGrid columns="2" columnClasses="0">
                    <h:outputLabel value="User Name " />
                    <h:inputText value="#{authenticator.userDetail.userName}" />

                    <h:outputLabel value="Password " />
                    <h:inputSecret value="#{authenticator.userDetail.password}" />

                    <p:commandButton value="Login" action="#{authenticator.authenticate}"></p:commandButton>
                    <p:commandButton value="Log Out" action="#{authenticator.logOut}" />
                </p:panelGrid>
            </p:panel>

当我执行应用程序时,我面临以下问题..

Mar 29, 2013 7:26:56 PM com.sun.faces.lifecycle.ProcessValidationsPhase execute
WARNING: /login.xhtml @21,66 value="#{authenticator.userDetail.userName}": Target Unreachable, 'userDetail' returned null
javax.el.PropertyNotFoundException: /login.xhtml @21,66 value="#{authenticator.userDetail.userName}": Target Unreachable, 'userDetail' returned null
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1030)
    at javax.faces.component.UIInput.validate(UIInput.java:960)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
    at javax.faces.component.UIInput.processValidators(UIInput.java:698)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at org.primefaces.component.panel.Panel.processValidators(Panel.java:297)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
    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:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我不知道问题出在哪里。

如果我更换。

@ManagedProperty(value = "#{userDetail}")
    private UserDetail userDetail;

private UserDetail userDetail = new UserDetail();
然后它工作正常。但我想使用@ManagedProperty。请帮帮我。

2 个答案:

答案 0 :(得分:0)

如果不添加@ManagedBean,

@SessionScoped无效。它也没有意义,因为JSF将创建一个空的UserDetails bean。

好像你需要在那里添加一个从UserDetails Entity中提取详细信息的服务。

@RequestScoped
public class Authenticator implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManagedProperty(value = "#{userService}")
    private UserService userService;

    public UserDetail getUser(long id){
        return  userService.getUser(id);
    }
}

如果您没有使用Spring,那么您可以将服务应用程序作为范围,

@ManagedBean
@ApplicationScoped
public  class UserService {

          public UserDetail getUser(long id){
                    return  em.createQuery("Select UserDetail .... where id ...");
          }    
}

如果您使用Spring ,

,您的服务可能如下所示
     @Service
     public class UserService {            
          public UserDetail getUser(long id){
                    return  em.createQuery("Select UserDetail .... where id ...");
          }    
}

如果您使用的是弹簧,则需要添加Spring EL解析器来解析弹簧服务

<faces-config>
  <application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
  </application>
</faces-config>

答案 1 :(得分:0)

因为我只能根据我的个人经验回答,如果你在尝试了这篇文章中的所有答案之后仍然没有解决你的问题,我建议你去项目---&gt;干净如果你是使用Eclipse(我不知道其他IDE)。实际上,我有完全相同的问题,只能通过这个解决方案解决它,但仍然不知道它发生的原因。