How to get a different error page for different PROJECT_STAGE in JSF - jsf

I want to show detail exception along with stack trace on error page if the project stage is development
web.xml entry -
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
else if Project stage is Production then I want to show a custom message to user.
web.xml entry-
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
Is there any way to achieve this?

The JSF project stage is available by Application#getProjectStage(). The JSF application is in turn available by FacesContext#getApplication(). The JSF context is in EL available by #{facesContext}.
So, this should do in the error page:
<c:choose>
<c:when test="#{facesContext.application.projectStage eq 'Development'}">
<!-- Print stack trace here -->
</c:when>
<c:otherwise>
<!-- Print custom message here -->
</c:otherwise>
</c:choose>

Related

fileUpload doesn't fire event in PrimeFaces 3.5 with JSF 2.2

I can't make fileUpload component on PrimeFaces 3.5 to fire the event. I have read many posts about that topic and followed advise there but still it doesn't work. I tried all of the modes (simple, auto, advanced) with no success.
If I use standard inputFile tag from the JSF specification it works properly.
This is my web.xml file:
<?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>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>redmond</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>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/home.xhtml</welcome-file>
</welcome-file-list>
</web-app>
And this is part of my view page with the fileUpload tag:
<h:form enctype="multipart/form-data">
<p:dialog id="basicDialog" header="Add pictures" widgetVar="dlg1" >
<p:fileUpload fileUploadListener="#{galleryManagedBean.addPicturesToGallery}" multiple="true"/>
</p:dialog>
</h:form>
The extract from managed bean with the method that is called from the tag:
#Named(value = "galleryManagedBean")
#RequestScoped
public class GalleryManagedBean {
public void addPicturesToGallery(FileUploadEvent event)
{
System.out.println("Triggered upload");
}
}
Also I would like to add that the Http POST request is fired properly after I checked it using the debugger tool in Chrome.
I have added necessary libraries to the classpath i.e. :
commons-fileupload-1.3.jar
commons-io-2.4.jar
This is caused by a change in FacesServlet of JSF 2.2. Since that version, FacesServlet natively supports file uploads (specifically: multipart/form-data HTTP requests) thanks to the presence of the new Servlet 3.0 specific #MultipartConfig annotation. Also, a new <h:inputFile> component was been introduced to offer a file upload component in the standard JSF component set.
This all conflicts with PrimeFaces file upload facility in older PrimeFaces 3.x versions which didn't take this new JSF 2.2 feature into account at all. The PrimeFaces 3.x file upload filter parsed and consumed the entire request while it should leave this job up to the FacesServlet. This caused the FacesServlet to be unable to properly decode the HTTP request (determining the submitted values and actions).
PrimeFaces 4.0, which is designed specifically for JSF 2.2, has taken this all into account. In this changeset of the PrimeFaces file upload filter you can see the changes done to recognize JSF 2.2 and bypass the parsing in the filter. Theoretically, it should also suffice to entirely remove the file upload filter registration from web.xml so that this isn't used anymore.
It should work fine if you upgrade to PrimeFaces 4.0. It has coincidentally been officially released just 2 days ago, so you're pretty on time for that. 

Composite component required="true" not respected

In the composite:interface I have defined an attribute like this:
<composite:attribute name="myAttribute" required="true"/>
Now when I use my composite component like this, without defining any attributes:
<myTag:myCC/>
I would expect an error to occur. It doesn't. What could I possibly be missing?
It will only occur if your JSF project stage is set to Development as follows in web.xml:
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
It defaults to Production. Don't be surprised if you start to see several other errors/warnings related to "development mistakes" after setting the above context parameter.
In your specific case you should get an exception during opening the page something like this when you omit the required attribute:
javax.faces.view.facelets.TagException: /test.xhtml #22,19 <my:composite> The following attribute(s) are required, but no values have been supplied for them: foo.
at com.sun.faces.facelets.tag.composite.InterfaceHandler.validateComponent(InterfaceHandler.java:232)
at com.sun.faces.facelets.tag.composite.InterfaceHandler.apply(InterfaceHandler.java:125)
at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:95)
...

Basic JSF with Session doesn't work in glassfish

I wanted to test Session replication of session and application scoped beans of JSF 2 in a clustered environment, currently trying with glassfish. To do so I created a simple application that I deployed to the server first to verify it's working (it doesn't).
Here is what I did so far:
Setup a Ubuntu Virtual Machine in Oracle VirtualBox
download glassfish as zip-file and unzip it in the home directory
start glassfish
deploy the JSF-app as war-file
go to the page of the app: localhost:8080/jsftest/index.jsf
try to set a variable saved in session/application scope
Depending on javax.faces.STATE_SAVING_METHOD in web.xml the result:
server: viewExpiredException is thrown
client: value is not stored and default value is shown
To create the app I did the following:
create new dynamic web project in eclipse
unzip all files from myfaces core in WEB-INF/lib
set the classes-folder to WEB-INF/classes
write beans and a .xhtml as below
The session scoped bean (application scoped is similiar):
#ManagedBean(name = "sessbean")
#SessionScoped
public class MySessionScopeBean implements Serializable{
private static final long serialVersionUID = -4733271400646173098L;
private String value = "default session data";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
The index.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:outputText value="Session id: #{sessionScope['id']}" />
<h:form>
<h:panelGroup>
<h:outputText value="Session Variable:"/>
<br/>
<h:inputText value="#{sessbean.value}"/>
</h:panelGroup>
<h:commandButton value="submit"/>
</h:form>
<h:form>
<h:panelGroup>
<h:outputText value="Application Variable:"/>
<br/>
<h:inputText value="#{appbean.value}"/>
</h:panelGroup>
<h:commandButton value="submit"/>
</h:form>
</h:body>
</html>
So this very basic example doesn't work. Any suggestions?
Also the Session-ID is not shown by #{sessionScope['id']}, don't know if that is the way to do it.
Got it working by removing parts of the (generated) web.xml file. The working file looks like this:
<?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" version="3.0">
<display-name>jsftest</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>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
What I stripped out (don't know which part caused the failure):
<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>
<context-param>
<description>
This parameter tells MyFaces if javascript code should be allowed in
the rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default is 'true'</description>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
If true, rendered HTML code will be formatted, so that it is 'human-readable'
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default is 'true'</description>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<description>
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default is 'false'
</description>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

tomcat7+El2.2+jsf1.2 not working

expression language is not working in tomcat7 with jsf1.2 .#{message.name_prompt} is ouputed as #{message.name_prompt}.
i tried to replace the el-api.jar in the tomcat lib folder with el-api-2.2.jar and put the el-impl-2.2.jar in the WEB-INF/lib folder, adding
<context-param>
<param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
to the web.xml. stilll the same.
my tomcat exact version is *7.0.21*this is the web.xml with the default tomcat setting:
<?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"
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>BasicExamples</display-name>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
update:
now i find that the expression language is not working on the form page but the message expression language is working as #{message.result_text}=>You entered the following information : ,but still managed bean not working.
and El is working fine with jsf2.0
You don't need to install/add anything to get EL 2.2 to work in Tomcat 7. It already ships with EL 2.2. Remove those JARs and the context param.
Given your symptoms
#{message.name_prompt} is ouputed as #{message.name_prompt}
I have the impression that you're talking about using EL in template text something like:
<p>This is EL in template text #{message.name_prompt}</p>
This is not an EL 2.2 feature. This is a Facelets feature. Facelets is the successor of JSP. You need to replace JSP by Facelets in order to be able to use EL in template text like that. For JSF 1.2, you can use Facelets 1.1.
Otherwise, when you want to stick to JSP, you really need to use <h:outputText>:
<p>This is EL in template text <h:outputText value="#{message.name_prompt}" /></p>
The only new EL 2.2 feature is the ability to invoke action methods with arguments, e.g.:
<h:dataTable value="#{bean.list}" var="item">
<h:column>
<h:commandButton value="Edit" action="#{bean.edit(item)}" />
</h:column>
</h:dataTable>
See also:
Difference between JSP EL, JSF EL and Unified EL
Migrating from JSF 1.2 to JSF 2.0

Lost RichFaces skin when rendering through rich:panel and a4j:include

I have an h:selectOneMenu and an a4j:commandButton, the latter of which reRenders a component called content which looks like this:
<rich:panel id="content">
<a4j:include viewId="#{MyBacking.viewId}" />
</rich:panel>
When the response is rendered and the component loads the content of the new JSP page, the tabs contained in that page use a skin that is different from the rest of the app (I think default blue).
I've noticed that after the included code is loaded, if I hit refresh, although this causes the bean to reissue the page contents, the skin is properly assigned.
My web xml says:
<context-param>
<param-name>org.richfaces.SKIN</param-name>
<param-value>glassX</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.CONTROL_SKINNING</param-name>
<param-value>enable</param-value>
</context-param>
<context-param>
<param-name>org.richfaces.CONTROL_SKINNING_CLASSES</param-name>
<param-value>enable</param-value>
</context-param>
Is this effect because RichFaces is in some way not in control of the rendering of the tabs at this point?
How can I ensure the tabs conform to the skin? All the documentation is relating to overriding a skin, and I'd rather not have to override and skin with the skin that it should already have.
Thanks
I am not sure if it will solve your problem, however you can try to force Richfaces to avoid using the default skin by setting a specific web.xml parameter:
<context-param>
<param-name>org.richfaces.LoadStyleStrategy</param-name>
<param-value>ALL</param-value>
</context-param>
You can find more details about this property here.
edited, to set the correct param-value, as stated in the comments.
I've just noticed I'd included a styleClass parameter - and because the jsp is now included didn't have access to the css facet reference.

Resources