How to use Spring Webflow popup="true" with Primefaces? - spring

Until now we were using Spring Webflow 2.3, which brought the "sf" namespace and Spring.js, which could be used to display view-states in a popup.
Webflow 2.4 deprecated Spring.js and removed the taglib with the following statement:
Previous releases of Spring Web Flow shipped with a component library which provided Ajax and client-side validation capabilities for JSF 1.2 environments. Applications using these components will need to switch to a 3rd party JSF component library such as PrimeFaces or RichFaces.
See: http://docs.spring.io/spring-webflow/docs/current/reference/htmlsingle/#spring-faces-upgrade-from-swf23-components
Now i have added a dependency to Primefaces 5.2 to my pom.xml and everything seems to work fine, except for SWF Popups, which simply redirect to the new view-state.
First page:
<ui:composition template="#{templatePath}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:define name="content">
<h:form id="formDashboard">
<p:commandButton id="addWidget" value="Hinzufügen" action="add-widget"/>
</h:form>
</ui:define>
</ui:composition>
Second page (should be rendered in a popup):
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:fragment id="popupFragment">
abc
</ui:fragment>
</ui:composition>
Flow definition:
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow.xsd">
<view-state id="start" view="dashboard.xhtml">
<transition on="add-widget" to="popup-addWidget"/>
</view-state>
<view-state id="popup-addWidget" view="popup-addWidget.xhtml" popup="true">
<on-render>
<render fragments="popupFragment"/>
</on-render>
</view-state>
</flow>
Spring configuration:
<bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry"/>
</bean>
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextListener"/>
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices"/>
<!-- Populates the flowRegistry on startup. Removing this and manually adding all flows changes nothing. -->
<bean class="config.FlowRegisteringBeanPostProcessor"/>
<faces:flow-builder-services id="flowBuilderServices"/>
<faces:resources/>
<bean id="facesContextListener" class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener"/>
I'm not including the web.xml, since there is nothing special.
Using Mojarra 2.2, Primefaces 5.2, Spring 4.2 and Webflow 2.4.1.
Using Firebug i can see that the server POST response issues to redirect, which JSF/Primefaces honors and does a full redirect to the new view-state instead of showing a popup:
<?xml version='1.0' encoding='UTF-8'?>
<partial-response id="j_id1"><redirect url="/Demo/spr/dashboard?execution=e1s2"></redirect></partial-response>
If anybody managed to get popup="true" working with these versions, with or without Spring.js, i'd be happy to hear about it.

We ended up using Primefaces' p:dialog and p:confirmDialog, instead of Webflow popups.
This doesn't really answer my question, but i can't see an answer to this seeing that Webflow sends the same response no matter if popup is set to true or false.

Related

Spring Security for Web Flow: How to access flow scoped variables in the login page

When I use Spring Security for Spring Web Flow - how can I access flow scoped variables in the login page? The situation:
A view state in the web flow is secured by a <secured/> tag. When an unauthorized user enters this state, the filter chain calls the login page. But this login page is outside the flow. In the Spring MVC controller of this login page I've tried to access the flow variable like in this answer. But I get null for requestContext.
There should be no problem with using flow scoped variables on unsecured pages. I have the following which works ok:
<flow xmlns="http://www.springframework.org/schema/webflow" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<view-state id="start" model="userForm" view="user/login">
<on-entry>
<set name="flowScope.titleCode" value="'login'" />
<set name="flowScope.login_error" value="requestParameters.login_error" />
<set name="flowScope.showRecaptcha" value="recaptchaService.showRecaptcha()" />
</on-entry>
</view-state>
Then my login.jsp looks like:
<h2>
<spring:message code="title_${titleCode}" />
</h2>
The relevant tag header for the jsp is:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>

Can Spring help to prevent caching of html pages on the browser?

I have a Java/Spring 3.x webapp that uses ExtJS and I use the Sencha Architect to create the front end which results in an automatically generated app.html file that loads in the JS and CSS resources that looks like this:
<!DOCTYPE html>
<!-- Auto Generated with Sencha Architect -->
<!-- Modifications to this file will be overwritten. -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ui</title>
<script src="ext/ext-all.js"></script>
<script src="ext/ext-theme-neptune.js"></script>
<link rel="stylesheet" href="ext/resources/ext-theme-neptune/ext-theme-neptune-all.css">
<link rel="stylesheet" href="css/custom.css">
<script type="text/javascript" src="app.js"></script>
</head>
<body></body>
</html>
I want to protect this html file with Spring security and this seems to work except that it is often cached in the browser so that it appears to reload even when the user is not logged in. Here is my Spring XML that configures security for my webapp:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/ui/app.html" access="hasRole('ROLE_USER')" />
<intercept-url pattern="/ui/**" access="permitAll" />
<form-login
login-page="/login"
default-target-url="/ui/app.html"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<csrf/> <!-- enable csrf protection -->
</http>
<authentication-manager>
<authentication-provider >
<user-service>
<user name="test" password="test" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
As you can see I have configured it to protect the ui/app.html resource as well as redirect to that page after log in. This works fine until the browser caches the page and causes confusion when the user is logged out and tries to access the same URL.
I was wondering if Spring MVC could be used to load the page via a controller, perhaps modifying the HTTP headers to force the page to expire, but as this is a page that is normally delivered directly by the servlet container and not MVC I'm not sure how I would configure that.
I'd also like to be able to leave my app.html file in-situ as it uses resources that are relative to it, and it's also easier to leave it there when working with Sencha Architect.
This will prevent browser caching:
<http>
<!-- ... -->
<headers>
<cache-control />
</headers>
</http>
It adds Cache-Control, Pragma and Expires headers for every response. More information can be found in reference documentation, section Security HTTP Response Headers.
Update: This answer was written for version 3.2 of Spring Security. As of version 4, these headers are included by default.
You can choose one of the below which depends on how your application is going to serve the resource requests.
Using Spring Security
http://docs.spring.io/spring-security/site/docs/current/reference/html/headers.html#headers-cache-control
Using Extjs
http://www.sencha.com/forum/showthread.php?257086-Is-there-a-simple-way-to-disable-caching-for-an-entire-ExtJS-4-application
Using HTML
http://www.htmlgoodies.com/beyond/reference/article.php/3472881

spring framework mvc localization

I am trying to add Internationalization and Localization support to our Spring MVC application.
I made encoding like this in *-servlet.xml
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
But I found wrong character like below
I cannot figure out what problem I should fix it. If possible, please let me know.
I've already added in jsp page like this:
<%# page contentType="text/html;charset=UTF-8" language="java" %>
But it doesn't work.
The defaultEncoding property of ReloadableResourceBundleMessageSource is used to
Set the default charset to use for parsing properties files. Used if
no file-specific charset is specified for a file.
It has no bearing on how the client is reading the response. If you are generating your response with a jsp, you can give it this line at the start
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
so that the client knows that you are providing data encoded with the UTF-8 charset.
If you are not using a jsp, there are other ways to set the content-type or content-encoding, directly from HttpServletResponse or from a returned ResponseEntity object.
update your default encoding with:
<property name="defaultEncoding" value="ISO-8859-1" />
and this should work for rendering characters with accents.
At least it works on my spring projects (french and european languages/users)
If this is not an option for you (bigger audience of targetted users) try to add this in your jsp:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
...
</head>

Java Web: How use JSP with JSF and Ajax?

i'm working in a project with jsp, i need to start to use ajax too.
i'm enjoying jsf , is possible work with these together (JSP, JSF and AJAX) ?
I'm asking this cause i could run jsf with ajax
<h:commandButton id="bt_save" value="Save" title="Save" action="#personMBean.clickSave()}">
<f:ajax execute="#form" render="lblMessage" />
</h:commandButton>
<br/>
<h:outputLabel id="lblMessage" value="#{personMBean.message}" />
But when i try input this code in my jsp page it doesn't work, even if i insert on my jsp page:
<%#page contentType="text/html"%>
<%#page pageEncoding="UTF-8"%>
<%#taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%#taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
Any ideas ?
Ensure that you're using JSF 2.x (the <f:ajax> doesn't exist in JSF 1.x) and that you've a <h:head> instead of <head> in the master template (so that JSF can auto-include necessary Ajax JavaScripts).
Said that, you should really consider Facelets as replacement of JSP. JSP is an outdated view technology and ill suited for templating and has been replaced by Facelets as per JSF 2.0 / Java EE 6.
Yes - try richfaces or primefaces
I don't see why you couldn't do this but certainly not on the same page.
To do this you need to map the *.jsp extension to the JSP servlet while mapping the *.jsf or *.xhtml to FacesServlet.

Refresh view with Spring WebFlow transition

I'm trying to add localisation to my web app which is built with Spring WebFlow and facelets. I want to add support for english and french. I've created my two messages_fr.properties and messages_en.properties files.
My template which I use for all my jsf pages has the following code to define the messages bundle and two links to switch between french and english.
<f:loadBundle basename="messages" var="msg" />
...
<h:commandLink id="changeLocaleFr" action="changeLocale"
class="flag fr">
<f:param name="ln" value="fr" />
</h:commandLink>
<h:commandLink id="changeLocaleEn" action="changeLocale"
class="flag en">
<f:param name="ln" value="en" />
</h:commandLink>
I've set up a session local resolver
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
and a local change interceptor
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="ln" />
</bean>
which I added to my flow handler mapping
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry" />
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor"></ref>
</list>
</property>
<property name="order" value="0" />
</bean>
In my flow I have a global transition for changeLocale
<global-transitions>
<transition on="changeLocale" />
</global-transitions>
All this is almost working. When I click on one of the links, the locale is changed. But I don't see the changes immediatly, I have to manually refresh or navigate to another view to rerender the page with the new langage being used. How can I make the changes appear immediatly after clicking on the link ?
My guess would be that this has something to do with the JSF view root not having changed at all when processing the changeLocale action.
Try to add some code like this when processing the locale change:
FacesContext context = FacesContext.getCurrentInstance();
context.setViewRoot(context.getApplication().getViewHandler().createView(context, context.getViewRoot().getViewId()));
That way JSF is forced to refresh the state of all components.

Resources