繁体   English   中英

运行简单的JSF应用程序时出错

[英]Getting error while running a simple JSF application

我在尝试运行基本的JSF应用程序时遇到异常。 我使用JSF1.1,JDK 6和tomcat 6.0.18相同。

例外

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

exception 

javax.servlet.ServletException: Error testing property 'lastname' in bean of type null
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:209)


root cause 

javax.faces.el.PropertyNotFoundException: Error testing property 'lastname' in bean of type null
    com.sun.faces.el.PropertyResolverImpl.getType(PropertyResolverImpl.java:342)
    com.sun.faces.el.impl.ArraySuffix.getType(ArraySuffix.java:240)
    com.sun.faces.el.impl.ComplexValue.getType(ComplexValue.java:208)
    com.sun.faces.el.ValueBindingImpl.getType(ValueBindingImpl.java:345)
    com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:111)
    javax.faces.component.UIInput.getConvertedValue(UIInput.java:713)
    javax.faces.component.UIInput.validate(UIInput.java:638)
    javax.faces.component.UIInput.executeValidate(UIInput.java:849)
    javax.faces.component.UIInput.processValidators(UIInput.java:412)
    javax.faces.component.UIForm.processValidators(UIForm.java:170)
    javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:912)
    javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:342)
    com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:78)
    com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:90)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:197)

以下是我为同一文件创建的文件:

//jsfmbean.jsp

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">

    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<f:view>
<html>
<body bgcolor="cyan">
<h:form id="form1" >
<h:inputText id="text1" value="#{text2}" />
<h:inputText id="text2" value="#{player.lastname}" />
 <h:commandButton action="success" value="success" />
 <h:commandButton action="#{player.changeName}" value="failure" />
 </h:form>
</body>
</html>
</f:view>

//faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">



<faces-config>

  <application>

  <locale-config>

  <default-locale>en</default-locale>  

  </locale-config>

  </application>

  <navigation-rule> 


  <from-view-id> 
   /jsfevent.jsp 
  </from-view-id> 

  <navigation-case>
  <from-outcome>success</from-outcome> 
  <to-view-id>/mathew.jsp</to-view-id> 
   </navigation-case> 

   <navigation-case> 
   <from-outcome>failure</from-outcome> 
   <to-view-id>/amy.jsp</to-view-id> 
   </navigation-case> 
  </navigation-rule> 



  <navigation-rule> 
<from-view-id>/jsfmbean.jsp</from-view-id> 
<navigation-case> 
<from-outcome>success</from-outcome> 
<to-view-id>/mathew.jsp</to-view-id> 
</navigation-case> 
</navigation-rule> 

<managed-bean> 
<managed-bean-name>player</managed-bean-name> 
<managed-bean-class> 
ourdemo.player 
</managed-bean-class> 

<managed-bean-scope> 
session 
</managed-bean-scope> 

<managed-property> 
<property-name>firstname</property-name> 
<property-type>java.lang.String</property-type>
<value>Mahatma</value> 
</managed-property> 

<managed-property> 
<property-name>lastname</property-name> 
<property-type>java.lang.String</property-type>
<value>Gandhiji</value> 
</managed-property> 

</managed-bean>

</faces-config>

//Player.java

package ourdemo; 
import javax.faces.context.*; 
import javax.faces.component.*; 
import javax.faces.validator.*; 
public class Player 
{ 
String firstname ;  
String lastname ; 


public void setFirstname(String a) 
{ 
firstname=a; 
} 
public String getFirstname() 
{ 
return firstname; 
} 
//---------- 
public void setLastname(String b) 
{ 
lastname=b; 
} 
public String  getLastname() 
{ 
return lastname; 
} 
//-------------------
public void changeName() 
{ 
lastname= firstname+" "+lastname; 
} 

}

请帮忙 ...

#{player}指向的Bean尚未正确初始化,因此#{player.lastname}引发NullPointerException。

Java区分大小写。 您已经创建了具有完全合格的类名ourdemo.Player的托管bean,但是您在faces-config.xml中将bean声明为ourdemo.player (请注意,小写字母p代替大写字母P )。 这样,JSF无法找到并创建bean类,从而导致#{player}null ,从而导致此异常。

相应地修复它:

<managed-bean>
    <managed-bean-name>player</managed-bean-name> 
    <managed-bean-class>ourdemo.Player</managed-bean-class> 
    <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

具体问题无关 :我完全同意Arjan Tijms的问题评论:尽可能使用JSF 2.0或更高版本。 JSF 1.1已过时5年,并且有许多缺点。 如果由于业务限制而使用JSF 1.1,请说服您的经理。 或者,如果您正在使用JSF 1.1,因为您正在阅读过时的书籍/教程,则将它们扔掉并阅读正确的书籍/教程。

您的faces-config.xml错误。

类名声明必须与

<managed-bean-class> 
ourdemo.Player 
</managed-bean-class> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM