Target Unreachable, identifier resolved to null JS

2019-04-12 14:19发布

问题:

I looked already those:

Target Unreachable, identifier resolved to null in JSF 2.2

"Target Unreachable, identifier 'authenticator' resolved to null" in SEAM

Java EE 6: Target Unreachable, identifier 'helloBean' resolved to null

Target Unreachable, identifier "Bean Name" resolved to null

Target Unreachable, identifier "Bean Name" resolved to null

JSF: /index.xhtml @12,80 value="#{LoginBean.username}": Target Unreachable, identifier 'LoginBean' resolved to null

http://www.coderanch.com/t/598907/JSF/java/Target-unreachable-identifier-resolved-null

but none of them worked for me.

Login.xhtml:

<?xml version="1.0" encoding="UTF-8" ?>
<!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:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login Page</title>
</head>
<body>
    <h:form prependId="false">

        <h3>WELLCOME</h3>
        <br />
        <h3>Enter</h3>
        <table>
            <tr>
                <td><h3>Name</h3></td>
                <td><h:inputText value="#{user.name}" id="name" /></td>
            </tr>
            <tr>
                <td><h3>password</h3></td>
                <td><h:inputSecret value="#{user.password}" id="password" /></td>
            </tr>
        </table>

        <h:commandButton value="Login">
            <f:ajax execute="name password" render="out" />
        </h:commandButton>
        <h3>
            <h:outputText id="out" value="#{user.greeting}" />
        </h3>

    </h:form>
</body>
</html>

User.java:

import java.io.Serializable;

import javax.annotation.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class User implements Serializable {

    private String name;
    private String password;
    private String greeting;

    public String getGreeting() {
        if (name.length() == 0)
            return "";
        else
            return "Welcome to JSF2 + Ajax, " + name + "!";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

}

I did not config those web.xml, because jsf2.2 does itself as i know.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Ajax</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

Config face:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

</faces-config>

Error is when I click button:

        HTTP Status 500 - /Login.xhtml @17,78 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null

    --------------------------------------------------------------------------------

    type Exception report

    message /Login.xhtml @17,78 value="#{user.name}": Target Unreachable, identifier 'user' resolved to null

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

    exception 

javax.el.PropertyNotFoundException: Target Unreachable, identifier 'user' resolved to null
    at org.apache.el.parser.AstValue.getTarget(AstValue.java:98)
    at org.apache.el.parser.AstValue.getType(AstValue.java:82)
    at org.apache.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:172)
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95)
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
    at javax.faces.component.UIInput.validate(UIInput.java:976)
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
    at javax.faces.component.UIInput.processValidators(UIInput.java:712)
    at javax.faces.component.UIForm.processValidators(UIForm.java:253)
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
    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:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

And this warning:

Typee value of the field User.greeting is not used  

But there 2 strings to with same get and set but no warnings for them.

I use tomcat 7.0.54.

回答1:

There's quite a bit wrong with your current code:

  1. You're using the wrong JSF annotations

  2. You're not using JSF-standard tags. Use:

    • <h:head/> instead of <head> (this is required for most ajax functionality in jsf. Without it, the necessary javascript to support ajax cannot be included on the page
    • <h:body> instead of <body>

An HTTP-500 is indicative of an actual bug in your code.



回答2:

Don't be afraid of 500 error code. just read stack trace carefully. there were some error in your code like Null Pointer Exception. please check this code ans tell the result.

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class User implements Serializable {

    private String name;
    private String password;
    private String greeting;

    /**
     * @return
     */
    public String getGreeting() {
        if (name == null ||name.length() == 0)
            return "";
        else
            return "Welcome to JSF2 + Ajax, " + name + "!";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

}

also please be careful about @kolossus answer and use standard jsf tags and annotations



回答3:

Try to use different annotation

import javax.faces.bean.ManagedBean;

instead of Java EE 6's

import javax.annotation.ManagedBean;