I have seen Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable
I have followed the steps mentioned there for the use case:
#Named annotation, using Java EE 6
and everything that is specified there is as it is in my project.
xhtml file; please note I have checked the syntax value="#{course.credits}"
<html xmlns="http://www.w3.org/1999/xhtml" mlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<head><title>Add Course</title></head>
<body>
<h2>Course Details</h2>
<h:form>
<table>
<tr>
<td>Name:</td>
<td><h:inputText id="course_name" value="#{course.name}"/></td>
</tr>
<tr>
<td>Credits:</td>
<td>
<h:inputText id="course_credits"
value="#{course.credits}"/>
</td>
</tr>
<tr>
<td colspan="2"><h:commandButton value="Submit" action="#{course.addCourse}"/></td>
</tr>
</table>
</h:form>
</body>
The #Named("course") bean; please note I have checked the imports:
package packt.jee.eclipse.jms.jsf_bean;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import packt.jee.eclipse.jms.dto.CourseDTO;
#Named("course")
#RequestScoped
public class CourseJSFBean implements Serializable {
public static final long serialVersionUID = 2L;
private CourseDTO courseDTO = new CourseDTO();
#Inject
private CourseManagedMsgSenderBean courseMessageSender;
public String getName() {
return this.courseDTO.getName();
}
public void setName(String name) {
this.courseDTO.setName(name);
}
public int getCredits() {
return this.courseDTO.getCredits();
}
public void setCredits(int credits) {
this.courseDTO.setCredits(credits);
}
public void addCourse() throws Exception {
courseMessageSender.addCourse(courseDTO);
}
}
The beans.xml in META-INF/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
I have restarted Eclipse, cleaned the project, restarted the server, but I am still getting this error. Just for the avoidance of doubt the error message is
javax.el.PropertyNotFoundException: Target Unreachable, identifier 'course' resolved to null
Clearly I am making a mistake but I can't see what.
I am using Glassfish 5 with eclipse 4.8 and jdk 8.
Only other thing I can think of is that there is no war file - stuff I've read about solving this error has mentioned stuff like "make sure your war file is in xxx directory" but I have no war file. I'm just adding to the server and right-clicking on the xhtml file and selecting run on server.
Any assistance is appreciated.
EDIT: My web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>CourseManagementJMSWeb</display-name>
<welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
<servlet>
<servlet-name>JSFServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSFServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
I have no faces-config.xml.
There are class files in the project directory in glassfish:
I have tried changing the annotation #RequestScoped to #ViewScoped but it did not make any difference.
EDIT 2: My book states that beans.xml goes in WebContent/META-INF:
In any case I already looked to this as a possible cause so I put an identical beans.xml in both META-INF and WEB-INF, but it made no difference:
There is nothing in my book that says anything about deploying any jar, war, or ear file. I have mentioned this already in my question. I am entirely new to this so I do not know whether this is odd or not.
EDIT 3: I have created a small basic test class and xhtml file in the same project and I am getting the same error:
test.TestJSFBean.java:
package test;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
#Named
#RequestScoped
public class TestJSFBean implements Serializable {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name=name;
}
public void add() throws Exception {
System.out.println("TestJSFBean.add");
}
}
test.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<head>
<title>TEST</title>
</head>
<body>
<h2>TEST</h2>
<h:form>
<table>
<tr>
<td>Name:</td><td><h:inputText id="name" value="#{TestJSFBean.name}"/></td>
</tr>
<tr>
<td colspan="2"><h:commandButton value="Submit" action="#{TestJSFBean.add}"/></td>
</tr>
</table>
</h:form>
</body>
</html>
Error:
/test.xhtml #16,65 value="#{TestJSFBean.name}": Target Unreachable, identifier 'TestJSFBean' resolved to null
javax.el.PropertyNotFoundException: /test.xhtml #16,65 value="#{TestJSFBean.name}": Target Unreachable, identifier 'TestJSFBean' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:106)
Edit 4: I have tried
<td>Name:</td><td><h:inputText id="name" value="#{testJSFBean.name}"/></td>
(i.e. rather than "#{TestJSFBean.name}") and that didn't work either:
javax.servlet.ServletException: /test.xhtml #10,79 value="#{testJSFBean.name}": Target Unreachable, identifier 'testJSFBean' resolved to null
Related
I'm trying to make a simple JSF hello world, with JSF 2.0, JBoss AS 7.0
Here's my xhml file :
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Page 1</title>
</h:head>
<body>
<f:view>
<h:outputLabel value="Hello Stock Manager Hello JSF again" />
<br/>
<h:outputLabel value="Tester Bean : #{testerBean.message}" />
</f:view>
</body>
</html>
and here's the managed bean class :
package prv.stockmanager.web.beans;
public class TesterBean {
private String message = "This is a message";
public TesterBean() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
And here's the faces-config (which is in the web-inf) :
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<managed-bean>
<managed-bean-name>testerBean</managed-bean-name>
<managed-bean-class>prv.stockmanager.web.beans.TesterBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property> <property-name>message</property-name>
<property-class>java.lang.String</property-class>
<value/>
</managed-property>
</managed-bean>
</faces-config>
The page works fine without exception if I remove the call to the managed bean. But when I call the managed bean I get this :
Bean or property class prv.stockmanager.web.beans.TesterBean for managed bean testerBean cannot be found.
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) [jsf-impl-2.1.3-b02-jbossorg-2.jar:2.1.3-SNAPSHOT]
Is it because JBoss AS 7.0 is using JSF 2.1 jar file or something? Should I use JSF 2.1 then? How to change that?
Problem resolved, I figured out that Eclipse is not generating the class. I disabled the automatic build and it worked fine. There should be a problem with JBoss Studio that I am using.
I'm new in developing web applications and I installed the ICEfaces plugin. I found this tutorial to learn how to work with it and the first given example dont work in my IDE.
Its about the last part of the tutorial where it shows how to use a dateTimeEntry in a webapp.
So I followed the instructions so that my index.xhtml looks like this:
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:icecore="http://www.icefaces.org/icefaces/core"
xmlns:ace="http://www.icefaces.org/icefaces/components"
xmlns:ice="http://www.icesoft.com/icefaces/component">
<h:body>
<form>
<ace:dateTimeEntry id="dateTimeEntryId"
value="#{yourBean.selectDateProperty}" timeZone="Canada/Mountain"
pattern="MMM/dd/yyyy" style="width: 729px; " renderAsPopup="true">
</ace:dateTimeEntry><br />
</form>
</h:body>
</html>
And my java bean like this:
package org.icefaces.view;
import java.io.Serializable;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.icefaces.ace.event.DateSelectEvent;
#ManagedBean(name= "yourBean")
#ViewScoped
public class YourBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5058131064162864510L;
private Date selectDateProperty = new Date(System.currentTimeMillis());
public Date getSelectDateProperty() {
return selectDateProperty;
}
public void setSelectDateProperty(Date selectDateProperty) {
this.selectDateProperty = selectDateProperty;
}
public void dateSelectListener(DateSelectEvent event) {
setSelectDateProperty(event.getDate());
}
}
The structure of the project looks like this:
The point is, that in the tutorial there is no location given where to save the java beans. So I thougt it has to saved in the java resources directory, but I'm not sure because the app dont work. I just get a blank screen. The server and the configuration of it is valid I think because I can drop some Img or buttons and see the result in the browser. So I think I did not really understand how the java beans are connected with the xhtml files. I thougt it works with ManagedBean - attribute but I'm not sure about that.
Complete list of sample codes you can find here
http://anonsvn.icesoft.org/repo/icefaces3/trunk/icefaces/samples
Well I found out, that the connection of the javabeans and the variables in the xhtml file has to created in faces-config.xml in this form:
<managed-bean>
<managed-bean-name> [bean-name-of-java-class] </managed-bean-name>
<managed-bean-class> [fullpath-to-java-file] </managed-bean-class>
<managed-bean-scope>[scope]</managed-bean-scope>
</managed-bean>
I am starting to study struts 2. I followed the example here.
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package extends="struts-default">
<action name="hello"
class="com.tutorialspoint.struts2.HelloWorldAction"
method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>HelloWorld3</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>
</web-app>
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Struts2</h1>
<form action="hello">
<label for="name">Please enter your name</label><br/>
<input type="text" name="name"/>
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>
HelloWorldAction.java
package com.tutorialspoint.struts2;
public class HelloWorldAction{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
But after running and submitting the form, 404 error occurs.
type: Status report
message: /HelloWorld3/hello
description:The requested resource (/HelloWorld3/hello) is not available.
It does not produce any error in the console. I think the action class is not getting mapped. I know this kind of error is common to all struts beginners but I swear I've been googling this since yesterday.
struts.xml is inside /src/, I also tried putting it inside WEB-INF/classes, still got no luck.
I'm using eclipse gallileo and tomcat 6 inside it.
Replies are really appreciated.
My bad, just realized the web.xml is not the same with the web.xml in the tutorial. Also I forgot to specify the package name in struts.xml. After correcting these, I then encountered an error in the console,
java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
To solve this problem I added commons-lang3-x.y in the WEB-INF/lib directory together with other jars required by the tutorial to be added.
your class could extend ActionSupport class or Action Interface then only it will become an action class. And no need call the method execute explicitly by default it will call execute method. If you were overriding it you need to mention that method name.
And it's not mandatory to use ActionSupport class. This is basically a helper class which provides many out of the box features for you, but at same time Struts2 framework do not ask to use this class, all it want is an entry method for your action class with return type as String and which can throw a general Exception
beside validation or Internationalization this class also provides many other features like Action level Errors etc.
<action name="hello"
class="com.tutorialspoint.struts2.HelloWorldAction">
package com.tutorialspoint.struts2;
public class HelloWorldAction extends ActionSupport{
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
add commons-lang3.x.y.z jar and commons-fileupload-x.y.z jar to your buildpath to avoid 'resource not available' error
http://viralpatel.net/blogs/tutorial-create-struts-2-application-eclipse-example/
You can get the Simple Struts 2 Example Demo with the jar files.
I've encoutered strange issue with annotations in bean.
index.xhtml content:
<h:outputText value="#{ejb.helloWorld}" />
Ejb.java content:
package bean;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name="ejb")
#SessionScoped
public class Ejb implements Serializable
{
public String getHelloWorld()
{
return "Hello World";
}
}
From what we see above i should be able to call bean method... but i can't, it will work only if i manage bean in faces-config.xml file with content:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>ejb</managed-bean-name>
<managed-bean-class>bean.Ejb</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
Questions:
Why i have encountered such situation ?
How to resolve this issue ?
I'm using:
JSF 2.0 (imported to lib)
JBoss Server 4.2
My wild guess is that JSF 1.2 bundled by default with JBoss 4.2 is used (JBoss might not care about JSF version). As a result annotations are ignored, faces-config.xml file is used.
Try to use configuration suggested here (thats's google cached page).
I'm using GlassFish 3.1, and trying to pass parameter to commandButton action. Following is my code:
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" />
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" />
ManagedBean class
package actionParam;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
#Named("bean")
#RequestScoped
public class ActionParam {
public ActionParam() {
super();
}
public String submit(int param) {
System.out.println("Submit using value " + param);
return null;
}
}
and finally,
View
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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">
<h:head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1" />
<title>Test Action Param</title>
</h:head>
<h:body>
<h:form id="actionForm">
<h:commandButton id="actionButton" value="Submit"
action="#{bean.submit}">
<f:param name="param" value="123"></f:param>
</h:commandButton>
</h:form>
</h:body>
</html>
When I click submit button, I get javax.el.MethodNotFoundException.
If I remove <f:param ... /> and pass parameter as follows,
.
:
<h:commandButton id="actionButton" value="Submit"
action="#{bean.submit(123)}">
</h:commandButton>
:
.
it works OK.
But I was thinking first way (using f:param) is correct one.
Which is the correct way to pass parameter?
Thanks in advance.
The <f:param> sets a HTTP request parameter, not an action method parameter. To get it, you would need to use <f:viewParam> or #ManagedProperty. In this particular case, the latter is more suitable. You only have to replace CDI annotations by JSF annotations in order to get #ManagedProperty to work:
#ManagedBean(name="bean")
#RequestScoped
public class ActionParam {
#ManagedProperty("#{param.param}")
private Integer param;
public String submit() {
System.out.println("Submit using value " + param);
return null;
}
}
When you're targeting a Servlet 3.0 container (Tomcat 7, Glassfish 3, JBoss AS 6, etc) with a web.xml whose <web-app> root declaration definies Servlet 3.0, then you should be able to just pass the parameter straight into the action method by EL as that's supported by EL 2.2 (which is part of Servlet 3.0):
<h:commandButton id="actionButton" value="Submit"
action="#{bean.submit(123)}">
</h:commandButton>
with
public String submit(Integer param) {
System.out.println("Submit using value " + param);
return null;
}
If you target an old Servlet 2.5 container, then you should still be able to do this using JBoss EL. See also this answer for installation and configuration details: Invoke direct methods or methods with arguments / variables / parameters in EL