JSF 2.2 Custom Component - java

I have a small JSF 2.2 webapp. Now I need a custom component, which simply displays on the page the following text:
Created by Nazar Sobchuk - 12/07/2013 (current date)
I have the following class for the component:
package com.dataart.mediaportal.components;
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
#FacesComponent(value = "components.FooterComponent", createTag = true)
public class FooterComponent extends UIComponentBase {
#Override
public String getFamily() {
return "my.custom.component";
}
#Override
public void encodeBegin(FacesContext context) throws IOException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String date = dateFormat.format(new java.util.Date());
ResponseWriter writer = context.getResponseWriter();
writer.write("Created by Nazar Sobchuk - 2013");
writer.write(date);
}
}
How am I supposed to use this component on my JSF pages? What xmlns url should be included in order to use a component?
Here are web.xml and faces-config.xml files if needed:
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>
<!--
responsible for handling multipart/form-data requests which
is required to be able to send files over HTTP.
<servlet-name> must match the exact <servlet-name>
of the FacesServlet as you've definied in web.xml.
-->
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<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>
<servlet>
<servlet-name>ImgLoaderServlet</servlet-name>
<servlet-class>com.dataart.mediaportal.servlet.ImgLoaderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImgLoaderServlet</servlet-name>
<url-pattern>/load</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>pages/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
faces-config.xml:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
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_2.xsd">
<navigation-rule>
<from-view-id>/pages/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>home</from-outcome>
<to-view-id>/pages/home.xhtml</to-view-id>
<redirect/>
</navigation-case>
<navigation-case>
<from-outcome>register</from-outcome>
<to-view-id>/pages/registration.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/pages/registration.xhtml</from-view-id>
<navigation-case>
<from-outcome>home</from-outcome>
<to-view-id>/pages/home.xhtml</to-view-id>
<redirect/>
</navigation-case>
<navigation-case>
<from-outcome>registration</from-outcome>
<to-view-id>/pages/registration.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/pages/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>go</from-outcome>
<to-view-id>/pages/login.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/pages/home.xhtml</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/pages/login.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
<application>
<message-bundle>
AppErrorMessages
</message-bundle>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>ru</supported-locale>
</locale-config>
<resource-bundle>
<base-name>i18n.MediaPortal</base-name>
<var>bundle</var>
</resource-bundle>
</application>
</faces-config>
I would be eternally thankful for the solution.
Nazar

Related

Problems with browser refresh and PrimeFaces 8

I'm quite new at PrimeFaces as I am migrating from IceFaces.
My IceFaces application shows with a single url, page content changes and when hitting F5, current view get refreshed, without reloading the page referred by the url.
I expect this behaviour in my Primefaces app too, but this doesn't happen, as I go back to the page referred by the url, the login page in my case.
Is this an expected behaviour?
Below the web.xml:
<web-app 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_4_0.xsd" version="4.0">
<display-name>oweb20-mainweb</display-name>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>none</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>false</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>com.sun.faces.enableRestoreView11Compatibility</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>facelets.DEVELOPMENT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>facelets.SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/extras/extras.xml</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>
<servlet-name>StickyCaptcha</servlet-name>
<servlet-class>nl.captcha.servlet.StickyCaptchaServlet</servlet-class>
<init-param>
<param-name>width</param-name>
<param-value>250</param-value>
</init-param>
<init-param>
<param-name>height</param-name>
<param-value>75</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>StickyCaptcha</servlet-name>
<url-pattern>/stickyImg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<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>index.html</welcome-file>
</welcome-file-list>
</web-app>
The faces-config.xml contains beans and navigation cases and rules, here's an example:
<?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">
<managed-bean>
<managed-bean-name>SessionBean</managed-bean-name>
<managed-bean-class>com.sia.mainweb.web.SessionBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>updateManager</property-name>
<value>#{UIUpdateManager}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<from-view-id>/*</from-view-id>
<navigation-case>
<from-outcome>caseHome</from-outcome>
<to-view-id>/Home.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
To navigate between pages, in actions, I use to return the case of the page I want to go, without redirecting to that page:
public String home_action() {
logger.info(BaseLogging.methodName() + getTraceEnteringUser());
String caseName;
if (userAuthenticated()) {
boolean canContinue = checkUserFingerprint();
if (canContinue) {
caseName = "caseHome";
}
} else {
caseName = "caseNotLoggedOn";
}
logger.info(BaseLogging.methodName() + getTraceExitingUser());
return this.getNavigationManager().navigateTo(caseName); //returns caseName
}

JSF 2 + Spring 3 can't access bean

I'm trying to use Spring 3 along with JSF 2, but I'm already having trouble doing the following :
<h:outputText value="#{sampleController.hello}" />
Using a static value works just fine but getting the value from Java doesn't (it doesn't display anything) e.g.
<body>
Test <h:outputText value="#{sampleController.hello}" /> <h:outputText value="hello" />
</body>
Would show up as
Test hello
SampleController.java :
#Component
#Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SampleController {
private String hello = "Hello!";
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}
I used "JSF 2 + Spring 3 Integration" from Mkyong for the project configuration (http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/)
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="eu.shishigami" />
<context:annotation-config />
</beans>
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Sample</display-name>
<!-- Add Support for Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF Mapping -->
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
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">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
Remove the servlet-mapping jsf, this is for 1.2 version. Without the servlet-mapping works!
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

convert filter configartion in web.xml to spring.xml

I have created a filter and configured in my web.xml as follows.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp" 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>Your Web App</display-name>
<filter>
<description>Test Filter</description>
<display-name>Hello world filter</display-name>
<filter-name>HelloWorldFilter</filter-name>
<filter-class>com.abc.HelloWorld</filter-class>
<init-param>
<param-name>greet</param-name>
<param-value>Good Morning</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>HelloWorldFilter</filter-name>
<servlet-name>MyServlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>NyServlet</servlet-name>
<servlet-class>com.abc.myservlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>NyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Now I am working on Springs. I want to include this in Spring.xml. I am not having any servlet but I have a normal class as bean.
Please suggest how to add this info in spring.xml
Thanks.
Have this in your web.xml
<filter>
<display-name>HelloWorldFilter</display-name>
<filter-name>MyServlet</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>MyServlet</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
and following in applicationContext.xml (or Spring.xml in your case)
<bean id="helloWorldFilter" class="com.abc.HelloWorldFilter"> </bean>

JSF 2 form action navigation not working

Im just testing out JSF 2 with Primefaces and are setting up a basic test-form.
The goal is to submit the form and then send the user to a page listing all the entries.
But all that happens if i click "Add" button is that my webapp tries to navigate to page: "//pages/index.xhtml", and there nothing is found...
Any ideas?? I have followed tutorials pretty much, but cant see where i got it wrong..
I have mapped the commandbutton to the ManagedBean-method "addUser". The addUser method returns "success", and i have set up "success" to go to success.xhtml..
I have spent 3 hours debugging this now and i'm about to go crazy, so any help would be greatly appriciated...
index.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui">
<h:head><title>Welcome to OTV_JSF_Spring_Hibernate_Project</title></h:head>
<h:body>
<h:form>
<table>
<tr>...</tr>
<tr>
<td><p:commandButton id="addUser" value="Add" action="#{userMB.addUser}" ajax="false"/></td>
<td><p:commandButton id="reset" value="Reset" action="#{userMB.reset}" ajax="false"/></td>
</tr>
</table>
</h:form>
</h:body>
</html>
userMB:
#ManagedBean(name = "userMB")
#RequestScoped
public class UserManagedBean implements Serializable {
private static final String SUCCESS = "success";
private static final String ERROR = "error";
/* .... */
public String addUser() {
return SUCCESS;
}
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_0.xsd"
version="2.0">
<!-- JSF and Spring are integrated -->
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
<!-- configuration of navigation rules -->
<navigation-rule>
<from-view-id>/pages/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/pages/success.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>/pages/error.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
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_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>OTV_JSF_PrimeFaces_Spring_Hibernate</display-name>
<!-- Spring Context Configuration' s Path definition -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. It is registered to Servlet Container -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Project Stage Level -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome Page -->
<welcome-file-list>
<welcome-file>/pages/index.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF Servlet is defined to container -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Mapping with servlet and url for the http requests. -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
I think you have to write in this way:
<?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">
<!-- JSF and Spring are integrated -->
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>
<!-- configuration of navigation rules -->
<navigation-rule>
<from-view-id>pages/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>pages/success.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>pages/error.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>

How do I configure JSF url mappings without file extensions?

Most tutorials propose a default JSF configuration similar to the following web.xml:
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</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>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
With this configuration the corresponding *.xhtml files in my webapp are only found by the Faces Servlet if the corresponding URLs ends with the file extension .jsf (e.g. http://localhost/welcome.jsf). Is it possible to configure web.xml so URLs that do not end with .jsf are also processed as JSF pages using the same *.xhtml files?
In other words I would like to have URLs that do not depend on the server side implementation.
You can use Filter to hide this extension and make your URL SEO friendly. One such implementation of Filter is PrettyFaces.
For example:
If you need http://host:port/yourapp/login to resolve with your login.xhtml then in pretty filter configure following way
<url-mapping id="login">
<pattern> /login </pattern>
<view-id> /legacy/user/login.jsf </view-id>
</url-mapping>
Have a look at two min video tutorial
you can create url mapping like this
create faces-config.xml file in WEB-INF folder
<?xml version="1.0" encoding="ISO-8859-1"?>
<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_1_2.xsd"
version="1.2">
<navigation-rule>
<from-view-id>/jsf/demoapp</from-view-id>
<navigation-case>
<from-outcome>demoapp</from-outcome>
<to-view-id>/demoapp.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
in web.xml you have to do 2 entries
<servlet>
<servlet-name>jsfServlets</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsfServlets</servlet-name>
<url-pattern>/jsf/*</url-pattern>
</servlet-mapping>

Categories