EvaluationException JSF : Cannot get value for expression.

Status
Nicht offen für weitere Antworten.

Nicnac

Mitglied
Hallo

ich wollte ein JSF/AJAX Tutorial machen ( http://kafle.blogspot.com/2005/09/tutorial-using-ajax-in-jsf-application.html)
und bin leider schon am JSF gescheitert.

Ich bekomme eine EvaluationException: beim Ausführen der Seite country.jsp auf die gleich bei Start der Anwendung umgeleitet wird.

...Servlet.service() for servlet jsp threw exception
javax.faces.el.EvaluationException: Cannot get value for expression '#{selectedCountryRecord.selectedCountry}'...

Das verstehe ich aber nicht, denn:

- Die Klasse selectedCountryRecord ist als managed-bean konfiguriert.
- Das Attribut selectedCountry hat eine get-Methode.

So müsste man darauf zugreifen dürfen. Hier der Code:

Hier der Code der faces-config.xml und der SelectedCountryRecord.java.
faces-config.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
                              "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
                                              
<faces-config>

<application>
<locale-config>
    <default-locale> en_US </default-locale>
       <supported-locale> en </supported-locale>
       </locale-config>
</application>

<managed-bean>
 <managed-bean-name>countryBean</managed-bean-name>
 <managed-bean-class>demo.CountryBean</managed-bean-class>
 <managed-bean-scope>application</managed-bean-scope>
</managed-bean>

<managed-bean>
 <managed-bean-name>selectedCountryRecord</managed-bean-name>
 <managed-bean-class>demo.SelectedCountryRecord</managed-bean-class>
 <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

</faces-config>

SelectedCountryRecord.java
Code:
package demo;
public class SelectedCountryRecord{
	String selectedCountry;
	CountryRecord selectedCountryRecord;
	java.util.HashMap countryDetails;
	
	public SelectedCountryRecord(){
		setCountryDetails();
	}
	
	public String getSelectedCountry(){
		return selectedCountry;
	}
	public void setSelectedCountry(String selectedCountry){
		this.selectedCountry = selectedCountry;
		setSelectedCountryRecord();
	}
	
	public String getName(){
		if(selectedCountryRecord != null)
			return selectedCountryRecord.getName();
		else return null;
	}
	public String getRegion(){
		if(selectedCountryRecord != null)
			return selectedCountryRecord.getRegion();
		else return null;
	}
	public String getContinent(){
		if(selectedCountryRecord != null)
			return selectedCountryRecord.getContinent();
		else return null;
	}
	public String getCapital(){
		if(selectedCountryRecord != null)
			return selectedCountryRecord.getCapital();
		else return null;
	}
	public String getId(){
		if(selectedCountryRecord != null) {
			if (selectedCountryRecord.getId() != "") {
				String id = "http://www.cia.gov/cia/publications/factbook/geos/"
					+ selectedCountryRecord.getId().toLowerCase() + ".html";
				return id;
			}
		}
		return null;
	}
	public boolean isSelected(){
		if(selectedCountryRecord != null) return true;
		else return false;
	}
	public void setSelectedCountryRecord(){
		if(selectedCountry != null) {
			Object obj = countryDetails.get(selectedCountry);
			if((obj != null) && 
					(obj instanceof CountryRecord)) {
				selectedCountryRecord = (CountryRecord) obj;
			} 
			else selectedCountryRecord=null;
		} else selectedCountryRecord = null;
	}
	
	public void setCountryDetails(){
		javax.faces.context.FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
		javax.faces.application.Application app = context.getApplication();
		CountryBean cBean = (CountryBean) app.createValueBinding("#{CountryBean}").getValue(context);
		countryDetails = cBean.getCountryDetails();
	}
}

Hier die country.jsp:

Code:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
	<head>
		<title>Country Details</title>
	</head>
	<body>
		<f:view>
			<h:form>
				<h:outputText value="Integrating AJAX in a simple JSF Application" styleClass="title" />
				

				

				<h:outputText value="Enter Country : " />
				<h:inputText id="autoText" value="#{selectedCountryRecord.selectedCountry}" />
				<h:commandButton value="OK" action="success" />
				<hr>

				...
			</h:form>
		</f:view>

	</body>
</html>

Ich benutze MyFaces 1.1.1 und Tomcat 5.5.

Hilfe wäre toll, ich hänge seit Stunden daran und eigentlich wollte ich AJAX lernen...:-(

Vielen Dank
Nicnac
 

Nicnac

Mitglied
Typisch.
Man erreicht stundenlang nichts, dann postet man es und findet die Lösung.

Anscheinend darf der Name der ManagedBean nicht gleich dem Namen der Klasse sein...

Habe es folgendermaßen geändert:
Code:
<managed-bean>
 <managed-bean-name>country</managed-bean-name>
 <managed-bean-class>demo.CountryBean</managed-bean-class>
 <managed-bean-scope>application</managed-bean-scope>
</managed-bean>
<managed-bean>
 <managed-bean-name>selectedCountryRec</managed-bean-name>
 <managed-bean-class>demo.SelectedCountryRecord</managed-bean-class>
 <managed-bean-scope>request</managed-bean-scope>
</managed-bean>


Ist mir bisher völlig entgangen, dass das nicht sein darf.
Vor allem weil in der jsp noch steht: <h:inputText id="autoText" value="#{selectedCountryRecord.selectedCountry}" />

Weiß jemand warum das so ist? Würde mich mal sehr interessieren.
 

Nicnac

Mitglied
Vielen Dank für die konstruktive Antwort!

Damit ist einem doch immer geholfen.

Echt super klasse.

:toll:

Nicnac
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben