I have a problem with a simple web application in JSF 2.2, my managed bean is not solved and I got this error:
/greeting.xhtml #21,66 date="#{dukesBDay.yourBD}": Target Unreachable, identifier 'dukesBDay' resolved to null
This is my managed bean:
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class DukesBDay {
#EJB
private DukesBirthDayBean dukesBirthdayBean;
This is my greeting.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:fc="http://java.sun.com/jsf/composite/components">
<h:head>
<title>First Cup Greeting Page</title>
</h:head>
<h:form>
<h2>
<h:outputText value="#{bundle.Welcome}"/>
</h2>
<h:outputText value="#{bundle.DukeIs} "/>
<h:outputText value="#{dukesBDay.age} #{bundle.YearsOldToday}"/>
<p/>
<h:outputText value="#{bundle.Instructions}"/>
<p/>
<h:outputText value="#{bundle.YourBD} "/>
<fc:inputDate id="userBirthday" date="#{dukesBDay.yourBD}" />
<h:outputText value=" #{bundle.Pattern}"/>
<p/>
<h:commandButton value="#{bundle.Submit}"
action="#{dukesBDay.processBirthday}"/>
<p/>
<h:message for="userBirthday" style="color:red"/>
</h:form>
and this is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>greeting.xhtml</welcome-file>
</welcome-file-list>
</web-app>
I tried adding the name property to #ManagedBean:
package firstcup.web;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name="dukesBDay")
#SessionScoped
public class DukesBDay {
but it doesn't work, still have the same error.
I tried mapping the managed bean in my faces-config:
<managed-bean>
<managed-bean-name>dukesBDay</managed-bean-name>
<managed-bean-class>firstcup.web.DukesBDay</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
but I get this error:
Unable to create managed bean dukesBDay. The following problems were found: - Bean or property class firstcup.web.DukesBDay for managed bean dukesBDay cannot be found
Why my DukesBDay is not solved? What's wrong?
Thanks
I solved restarting NetBeans and GlassFish Server.
I didn't add the name property to #ManagedBean:
package firstcup.web;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class DukesBDay {
and everything works, so it isn't necessary (according to naming convention, you can refer to the bean using the uncapitalized short name of the Class)
when you write #ManagedBean, you should also add the name property to that.
#ManagedBean(name="dukesBDay")
#sessionscoped
the other way to declare your managed bean is, using faceconfig.xml file.
<managed-bean>
<managed-bean-name>dukesBDay</managed-bean-name>
<managed-bean-class>package.DukesBDay</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
If you don't specify dukesBDay along with #ManagedBean, it will be resolved to null.
Related
Environment
Weblogic 12.1.3.0.0
Java 1.7
JSF 2.2 (provide libraries from app because of issues with Weblogic Server)
Ant-based build tool (huge company means old tools)
Files
Portal.java
#ManagedBean(name="portal")
#ViewScoped
public class Portal {
public String foo ="foo";
#PostConstruct
public void init() {
System.out.println("instantiated");
}
}
portal.xhtml
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<ui:composition template="/WEB-INF/templates/wide_template.xhtml">
<ui:define name="mainContent">
<!-- other markup removed -->
<h:outputText value="hello:#{portal.foo}"></h:outputText>
</ui:define>
</ui:composition>
</html>
faces-config.xml
<?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>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0">
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>portal.xhtml</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>/skinned/*</url-pattern>
<url-pattern>*.faces</url-pattern>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
Problem
My page renders all elements correctly. For example h:selectOneMenu and so on. But it seems that my Managed Bean is never instantiated as i don't see output on the console nor does the page render the content of foo.
I've read some other questions on SO:
Bean not instantiated - I don't beans.xml at all, I think.
JSF 2 managed bean does not get instantiated - I only have one ManagedBean which is referenced.
JSF Managed bean in jar not instantiated - My structure should be fine.
Does anyone know about issues with SessionScope maybe?
Is there something I'm missing? Or do I have to provide more, like the app-structure after deployment, more code, screenshots?
As it is #ViewScoped bean you have to implement Serializable as the implementation may decide to serialize session or view scoped beans whenever there is a need for that.
#ManagedBean(name="portal")
#ViewScoped
public class Portal implements Serializable{
In theory the NonSerializableException should occur only during the serialization itself, but maybe your environment set-up prevents the creation itself.
Anyway all your #SessionScoped / #ViewScoped should be marked like that by definition anyway.
Try using #javax.inject.Named instead of #ManagedBean.
I'm trying to start a small JSF application with no success. Tried it on Glassfish but I had the following error (Trying to use CDI on Glassfish 4 results in javax.el.PropertyNotFoundException: Target Unreachable, identifier 'indexMB' resolved to null).
So I tried to migrate the application to the TomEE 1.5.2, but although display no error in the console, the page does not load the JSF components, as you can see in the image below:
Any help will be very useful.
Follow my setup and my files:
TomEE 1.5.2
Primefaces 3.5
commons-fileupload 1.3
commons-io 2.4
** index.xhtml
<html ...>
<f:loadBundle basename="i18n" var="bundle" />
<h:head>
<title>#{bundle['index_title']}</title>
</h:head>
<h:body>
#{bundle['index_appname']}
<br />
<h:form id="frmIndex">
<p:panelGrid columns="2">
<p:outputLabel for="user" value="#{bundle['lblUser']}" />
<p:inputText id="user" value="#{indexMB.user}" />
<p:outputLabel for="password" value="#{bundle['lblPassword']}" />
<p:password id="password" value="#{indexMB.password}" />
</p:panelGrid>
<p:commandButton action="#{indexMB.loginTest}" value="#{bundle['btn_login']}" />
</h:form>
</h:body>
** IndexMB.java
#ManagedBean ("indexMB")
#RequestScoped
public class IndexMB {
private String password;
private String user;
public IndexMB() {
}
public String loginTest(){
...
}
// getters and setters
}
** web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.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-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
** faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
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">
<application>
<locale-config>
<default-locale>pt_BR</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
</application>
** 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>
the page does not load the JSF components
The page is loading the components, otherwise there won't be a <input type="text" /> that comes from the <p:inputText id="user" value="#{indexMB.user}" /> in your Facelets code (along with other components). The problem seems to be in your #{bundle['<whatever>']} configuration. Note that this problem is not related to GlassFish nor TomEE at all, just about the internationalization configuration.
For JSF 2.x I followed the explanation from this Q/A: https://stackoverflow.com/a/4830669/1065197 and the link that provides more info: Internationalization in JSF with UTF-8 encoded properties files. Based in that tutorial I made a test application. In short:
Configure the bundle in your faces-config.xml
<application>
<locale-config>
<default-locale>pt_BR</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<!--
Here should be the full name of the package and the
name of the properties files with the i18n text.
Example (from a personal project):
edu.home.store.view.idioma.tienda
Where the properties files are:
edu.home.store.view.idioma.tienda_es.properties
edu.home.store.view.idioma.tienda_en.properties
-->
<!-- <base-name>edu.home.store.view.idioma.tienda</base-name> -->
<!-- assumming your file is directly posted in class folder -->
<base-name>i18n</base-name>
<!--
Name of the variable to use in Facelets files.
-->
<var>bundle</var>
</resource-bundle>
</application>
Create a #SessionScoped bean that will handle the locale to use in your pages.
#ManagedBean
#SessionScoped
public class LocaleBean implements Serializable {
private static final long serialVersionUID = 89794215646544L;
private Locale locale;
public LocaleBean() {
}
#PostConstruct
public void init() {
//give the default value here
locale = new Locale("pt_BR");
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
public Locale getLocale() {
return locale;
}
public String getLenguaje() {
return locale.getLanguage();
}
public void setLenguaje(String lenguaje) {
locale = new Locale(lenguaje);
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
}
Wrap all the content of your view using <f:view> and define the language to use. Preferably, this should go in the main template. Example:
<f:view locale="#{localeBean.locale}">
<!-- Your Facelets code goes here... -->
</f:view>
Apart from this problem, I highly recommend you to change the obviously autogenerated Faces Servlet mapping configuration from /faces/* to *.xhtml:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
More info:
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?
I've developed an application using JSF with PrimeFaces. I've a problem in the Facelets page where <p:xxx> tags are not displayed in the page. Here is my Facelets page to display label, textbox and button. Only label is displayed in the page:
<!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://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:i="http://primefaces.prime.com.tr/touch"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<body>
<ui:composition template="/templates/template.xhtml">
<ui:define name="content">
<h:form>
<h:outputLabel for="curator" value="Name:" style="font-weight:bold"/>
<p:inputText id="curator" value="#{user.curator}" />
<p:commandButton value="Submit" update="display"/>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
my web.xml file
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Cation</display-name>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>faces/login.xhtml</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>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
</web-app>
How is this caused and how can I solve it?
It seems the label is displaying because it is an h-tag whereas the other tags are p, so first off check whether your primefaces jar is implemented by checking the Libraries on your project (if using Netbeans ) or Java Resources\Libraries if on eclipse. The xmlns:p="http://primefaces.org/ui" is not sufficient to make primefaces work, it only specifies the namespace.
Note : you're tagging primefaces-extensions where I think primefaces tag is more relevant in your case.
I wrote a simple jsf web app on eclipse. My web app works fine when I run it on tomcat 7 server in eclipse. Now I am in the process of deploying it to a server host name = cheetah.arvixe.com.
to do this I have exported my web app as a war file and have unzipped this war file in the public_html folder of the arvixe server.
When I go to my website on my browswer, i get a blank page. When I look at the page source I see my index.xhtml with the jsf tags. I want to know why my page is not showing. Any ideas?
Here is my index.html code:
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/WEB-INF/templates/BasicTemplate.xhtml">
<ui:define name="content">
<h:form>
<h:panelGrid columns="2">
<h:outputText value="Name"></h:outputText>
<h:inputText id="userid" value="#{user2.userId }"></h:inputText>
<h:outputText value="Password"></h:outputText>
<h:inputSecret id="password" value="#{user2.pincode}"
required="true" requiredMessage= "#{vm['loginBean.password']}">
<h:messages for="password" style="color:red" />
</h:inputSecret>
</h:panelGrid>
<h:commandButton value="Register" action="query_results"></h:commandButton>
</h:form>
</ui:define>
</ui:composition>
</html>
and here is my web.xml code:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Redlancelogin</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.xhtml</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>*.xhtml</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>
in my web-ing/lib i have the jsf-api.jar and jsf-imp.jar
please tell me why I keep getting a blank page when i deploy to the host server. Thank you in advance!!!!
I tried to create a simple web app using ICEfaces and encountered a problem.
Here is the web page:
<!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: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=UTF-8" />
<title>test</title>
</h:head>
<h:body>
<h:form>
<h:commandButton value="Action" action="#{test1.action}" />
</h:form>
</h:body>
</html>
Here is the backing bean:
#ManagedBean(name = "test1")
#SessionScoped
public class Test1 {
public Test1() {
}
public void action() {
System.out.println("action invoked");
}
}
And here is web.xml:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>regconf</display-name>
<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>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<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>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>
The problem is that the size of the ViewState is large (checked with Firebug) and it grows each time I click "Action" button. After first request it is 32 KB, after second 61 KB, then 94 KB, 128 KB etc. (seems to me like it always saves the state of the previous request).
The behaviour changes when I set the value of javax.faces.STATE_SAVING_METHOD param to server - in this case all works fine and the ViewState remains small after each request (checked it on the server side using StateManager).
I'm using Mojarra 2.1.12 (also tried MyFaces - same result), ICEfaces 3.1.0 and Tomcat 7.0.29.
Thanks in advance.
ICEfaces requires server side state saving, so you should set the following:
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>