0

我在 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。请帮我。

4

2 回答 2

0

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

于 2013-03-29T20:39:57.757 回答
0

@SessionScoped不添加 @ManagedBean 无效。这也没有意义,因为 JSF 将创建一个空的 UserDetails bean。

似乎您需要在那里添加一个从 UserDetails 实体中提取详细信息的服务。

@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,那么您需要添加 Spring EL 解析器来解析您的 spring 服务

<faces-config>
  <application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
  </application>
</faces-config>
于 2013-03-29T17:57:46.403 回答