jsf 2 - JSF sample project managed beans error, unable to print managed beans message to html -
i started implement basic jsf application unable print jsf managed beans message html..
here code :
helloworld.java :
package com.project.managedbeans; import javax.faces.bean.managedbean; import javax.faces.bean.requestscoped; @managedbean(name="helloworld", eager=true) @requestscoped public class helloworld { private string message; public helloworld(){ system.out.println("hello world managed bean created"); } public string getmessage(){ return "hello world ! "; } public void setmessage(string message){ this.message = message; }
index.html :
<!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"> <body> #{helloworld.message} </body> </html>
faces-config.xml
<?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_1.xsd" version="2.1"> </faces-config>
and 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>jsfsampleproject</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>*.jsf</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>
and when run application on apache server, page see under
localhost:8080/jsfsampleproject/ :
#{helloworld.message}
i have jsf 2.1 mojorra under libraries tab. think problem ?
thanks.
the problem welcome file list accepts file names physically present in web app, , index.xhtml
. note none of welcome files match condition.
next, file not handled facesservlet
. is, requested url not correspond servlet url mapping in web.xml. note mapping *.jsf
doesn't match requested files well.
all in all, following excerpt web.xml solve problem:
<welcome-file-list> <welcome-file>index.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>*.xhtml</url-pattern> </servlet-mapping>
of course, you'll need file index.xhtml
@ root of web application.
Comments
Post a Comment