Refresh view with Spring WebFlow transition - spring

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.

Related

Spring LDAP, Is it possible to configure LdapTemplate using authenticated use?

In my application I have fill some user fields from its information in a LDAP, I use LdapTemplate configured in the servlet-context.xml (with a properties file to set the values) and everything works perfect but I don't want to have the login of a user set in plain text in the application so I wonder if it is possible to configure the LDAPTemplate using the user logged in the application and if possible get a way to do it.
My actual configuration is this:
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="${ldap.url}" />
<property name="base" value="${ldap.base}" />
<property name="userDn" value="${ldap.userDn}" />
<property name="password" value="${ldap.password}" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
<property name="ignorePartialResultException" value="true" />
</bean>
Any help will be appreciated, thanks in advance.

How to get all application's online users in Spring Security 3.0.5?

When I make an implementation for
org.springframework.security.core.userdetails.UserDetailsService
and use the statement
sessionRegistry.registerNewSession(user.getUsername(), user);
within it after successful authentication, then the
sessionRegistry.getAllPrincipals();
list is not empty (but when I log out from application the session still remain within list) otherwise this list will be empty. how can I make the session registration (and also unregistration during user log out or session expiration) within sessionRegistry automatically? my spring config is as below:
<sec:http auto-config="true" use-expressions="true" access-denied-page="/accessDenied.jsf">
<sec:form-login login-page="/login.jsf" />
<sec:session-management session-authentication-strategy-ref="sas" />
</sec:http>
<bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
<bean id="scr"
class="org.springframework.security.web.context.HttpSessionSecurityContextRepository" />
<bean id="smf"
class="org.springframework.security.web.session.SessionManagementFilter">
<constructor-arg name="securityContextRepository"
ref="scr" />
<property name="sessionAuthenticationStrategy"
ref="sas" />
</bean>
<bean id="sas"
class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
<constructor-arg name="sessionRegistry"
ref="sessionRegistry" />
<property name="maximumSessions" value="10" />
</bean>
Most likely you have forgotten to add an HttpSessionEventPublisher to your web.xml.
Another possibility is that the principal in question has other sessions still active which haven't timed-out or been invalidated. You have a maximum session value of 10. Try setting that to "1" instead for testing.
Also, version 3.0.5 is out of date. You should use the latest version and keep up to date with patches to avoid vulnerabilities.

setting no cache for different parts of spring mvc 3 using WebContentInterceptor?

Hi there I have developed a dynamic web application that uses Ajax to fetch data from databases and keep the GUI up to date but while testing it with IE8 I am experiencing caching issues.
I used the following code in my webmvc-config.xml file to stop the browser from caching:
<mvc:annotation-driven />
<mvc:interceptors>
<bean id="webContentInterceptor"
class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="0"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="useCacheControlNoStore" value="true"/>
</bean>
</mvc:interceptors>
and it works exactly as it should, but the problem is that now the browser obviously doesn't cache anything. what I want to know is how to modify that xml code so that it applies to the Ajax parts of the web app (which are controlled using 5 Controller files); so that the icons..etc are still cached? The path to these controller files would be something like "/admin/**"
I know that the Spring WebContentInterceptor has properties such as "setCacheMappings" and "setPathMatcher" but there is nowhere online that I can find examples of these being using in the xml config file.
ANY help would be much appreciated, it's really doing my head in.. Thanks. Jake
In your <mvc:interceptors> you can restrict the URL path each interceptor should apply to, as follows:
<mvc:interceptors>
<mvc:interceptor>
<mapping path="/admin/*"/>
<bean id="webContentInterceptor" ..... />
</mvc:interceptor>
<mvc:interceptors>
It's all explained here.

Spring SimpleUrlHandlerMapping not allowing routing for any html page

I'm trying to implement a file upload in my Spring application based on the Spring documentation. However, when I add the SimpleUrlHandlerMapping reference, I can't even route to my login page.
In my web.xml, I have all .htm files mapped to my servlet:
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
then, in myapp-servlet.xml, I have this:
<bean name="/login.htm" class="com.example.web.LoginFormController">
<property name="sessionForm" value="true" />
<property name="commandName" value="loginCommand" />
<property name="commandClass" value="com.example.command.LoginCommand" />
<property name="authenticationService" ref="authenticationService" />
<property name="loginDataAccess" ref="loginDAO" />
<property name="validator" ref="loginValidator" />
<property name="formView" value="login" />
<property name="successView" value="hello.htm" />
</bean>
This allows me to go to the login page and login. However, when I add the following based on the documentation, I get this error: No mapping found for HTTP request with URI [/myapp/login.htm] in DispatcherServlet with name 'myapp'
These are the lines I added to the myapp-servlet.xml file:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/upload.form=fileUploadController</value>
</property>
</bean>
<bean id="fileUploadController" class="com.example.web.FileUploadController">
<property name="commandClass" value="com.example.domain.FileUploadBean" />
<property name="formView" value="fileuploadform" />
<property name="successView" value="hello.htm" />
</bean>
If I take out the urlMapping section, I can navigate to the upload page, but then I get an IllegalStateException. How do I have Spring route the htm files normally and allow me to upload a file? Shouldn't the urlMapping only matter when the upload.form is called?
Update
Per SKaffman's answer, I updated the myapp-servlet.xml to look like this:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/upload.form=fileUploadController
/upload.htm=fileUploadController
/login.htm=loginController
</value>
</property>
</bean>
This allows me to navigate to the upload page, but when I click on the submit button, I get a 404 for upload.form saying "(/myapp/upload.form) is not available." If I take the upload.htm out of the mapping, I can't navigate to that page. I don't know what mapping to use for both the page and the upload. The upload page has this action defined:
<form method="post" action="upload.form" enctype="multipart/form-data">
By default, Spring registers multiple HandlerMapping beans automatically, including a BeanNameUrlHandlerMapping. This handler allows you to use things like <bean name="/login.htm"> without any additional configuration.
However, as soon as you add an explicit handler mapping bean, like your SimpleUrlHandlerMapping, then the default ones are no longer registered, and your login controller will not longer be mapped.
The simplest solution for you is probably to add your login controller to the SimpleUrlMappingHandler:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>/login.htm=loginController</value>
<value>/upload.form=fileUploadController</value>
</property>
</bean>
<bean id=loginController" class="com.example.web.LoginFormController">
...
</bean>

Integrating Spring Webflow 2 and Apache Tiles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I've recently started upgrading some applications to use Spring Webflow 2, and I want to make use of the new Ajax functionality that comes with Webflow 2. Can somebody please direct me to a tutorial for integrating Tiles 2 with Spring Webflow (since that's apparently what they recommend). I've found the documentation that comes with Webflow 2 in this regard to be absolutely useless.
This isn't exactly referring to the ajax features, but it helped me get apache tiles 2 set up for regular flows:
http://jee-bpel-soa.blogspot.com/2008/12/spring-web-flows-2-and-tiles.html
A lot more details are at the link, but the core bit you need is a new view resolver:
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions" value="/WEB-INF/flows/main/main-tiles.xml" />
</bean>
This is what I did to get it working with webflow 2 and tiles 2.0
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs/templates.xml</value>
</list>
</property>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/flow/**/*.html">
flowController
</prop>
<prop key="/**/*.html">viewController</prop>
</props>
</property>
<property name="order" value="1" />
</bean>
<bean id="tilesViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
<bean id="flowController"
class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<webflow:flow-executor id="flowExecutor"
flow-registry="flowRegistry" />
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices"
base-path="/WEB-INF/flow/user">
<webflow:flow-location path="/manage-users.xml" />
</webflow:flow-registry>
<webflow:flow-builder-services id="flowBuilderServices"
view-factory-creator="viewFactoryCreator" />
<bean id="viewFactoryCreator"
class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="tilesViewResolver" />
</bean>
It's perfectly explained in the docs. So, please, stop saying it isn't.
http://static.springsource.org/spring-webflow/docs/2.0.x/reference/htmlsingle/spring-webflow-reference.html
How to use tiles in spring: 10.5 View resolution (link + #spring-mvc-config-spring-view-resolution)
How to use Ajax with tiles in spring: 11.5: Handling Ajax request (link + #spring-js-ajax)
Copy the code from those links and you will end up with something like this:
Configuration for webflow to use Tiles:
<!-- Plugs in a custom creator for Web Flow views -->
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" />
<!-- Configures Web Flow to use Tiles to create views for rendering; Tiles allows for applying consistent layouts to your views -->
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="tilesViewResolver" />
</bean>
Configuration for Tiles:
<!-- Configures the Tiles layout system -->
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/views/layouts/page.xml</value>
<value>/WEB-INF/views/layouts/table.xml</value>
<value>/WEB-INF/views/globalViews.xml</value>
<value>/WEB-INF/views/userViews.xml</value>
</list>
</property>
</bean>
Configuration for Tiles + Ajax:
<!--
- This bean configures the UrlBasedViewResolver, which resolves logical view names
- by delegating to the Tiles layout system. A view name to resolve is treated as
- the name of a tiles definition.
-->
<bean id="tilesViewResolver" class="org.springframework.js.ajax.AjaxUrlBasedViewResolver">
<property name="viewClass" value="org.springframework.webflow.mvc.view.FlowAjaxTilesView" />
</bean>

Resources