Unable to get FlowHandlerMapping working in Spring Webflow - spring-mvc

I'm using SWF 2.1 with Spring 2.5.6. and am trying to get the default mapping strategy for 2.1 to work. I had previously been using the mapping strategy that was the 1.0 default (whereby the flow id was retrieved from the request parameters)
In spring MVC configuration, I'm using the simpleUrlMapping strategy (order = 1) and have declared the SimpleControllerHandlerAdapter bean.
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
SWF configuration:
<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean><!--
Launches new flow executions and resumes existing executions. -->
<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
<webflow:flow-execution-repository max-executions="5" max-execution-snapshots="80"/>
</webflow:flow-executor>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<!--
Creates the registry of flow definitions for this application -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF/flows">
<webflow:flow-location-pattern value="/**/*-flow.xml"/>
</webflow:flow-registry>
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="viewFactoryCreator" development="true"/>
<bean id="viewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers">
<list>
<ref local="beanNameViewResolver"/>
<ref local="internalResourceViewResolver"/>
</list>
</property>
</bean>
<!-- Maps request paths to flows in the flowRegistry;
e.g. a path of /hotels/booking looks for a flow with id "hotels/booking" -->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping" p:order="0">
<property name="flowRegistry" ref="flowRegistry"/>
</bean>
Here is the directory structure in the web folder:
/WEB-INF/flows/flow1-name/flow1-flow.xml
/WEB-INF/flows/flow2-name/flow2-flow.xml
/WEB-INF/flows/flow3-name/flow3-flow.xml
I have been trying to access the flows by going to
http://www.appdomain.com/flow1-name
the mapping does not work and I get a 404.
Also, how should I name subflows and where should I put them? I would like to be able to access the subflows from more than one top-level flow.

I don't know what your servlet mapping in web.xml looks like but http://www.appdomain.com/flow1-name doesn't seem to include the name of your webapp and possibly the name of your servlet mapping. Check if the DispatcherServlet is getting reached at all. You can do that by enabling the org.springframework.web and org.springframework.webflow logging categories and looking for the output.

Related

Will my application work if i don't give handlerMapping in configuration file?

This is my spring-servlet.xml file. I am new to spring MVC. Do we need to define bean for HelloWorld.Controller. If i don't define will it work?
<bean id="viewResolver" class=" org.springframework.web.servlet.view. InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >
<property name="message" value="Hello World!" />
</bean>
</beans>
If i don't give this bean definition
<bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >
<property name="message" value="Hello World!" />
</bean>
MyApplication should work or not? I am new to spring MVC. In few tutorial this code is there and in few its not there. Please explain.
Yes, you will need to define the bean (way to create instance of class) for all the Controller/Service/Dao/Components class you want to use and set relevant properties.
I would recommend you to use Annotation based configuration (spring boot) as it eases all this process. You can find lot of tutorials on getting stated with spring boot. Here is one such good tutorial https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/

Upgrade of Spring webflow 1.0 to 2.4.2 - Setting default flow id?

I'm very much new to spring webflow framework and I have been working on upgrading the existing project on spring webflow 1.0 to use spring webflow 2.4.2 latest framework. Currently on our project we are using the defaultFlowId in dispatcher-servlet.xml and there is a corresponding setter method at FlowController.java of spring webflow 1.0 but it is not available with the current webflow framework.
I would like to understand if there is any alternative for the same ?
(dispatcher-servlet.xml)
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/**/index.htm=loginController
</value>
</property>
</bean>
<bean name="loginController" class="com.example.ui.controllers.LoginController">
<property name="defaultFlowId" value="init-flow" />
<property name="flowExecutor" ref="flowExecutor" />
</bean>
Now it says, Caused By: org.springframework.beans.NotWritablePropertyException: Invalid property 'defaultFlowId' of bean class [com.example.ui.controllers.LoginController]: Bean property 'defaultFlowId' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Since there is no setDefaultFlowId() in latest FlowController.java of org.springframework.webflow.mvc.servlet.FlowController
Anyone who worked/used the above method ? or can i know whether we can define the same kind of defaultId in other ways ?
Any help would be greatly appreciated !
We have upgraded. Here is the flow definition that we are required to use in order that the flow is maintained and default flow is used:
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/**/index.htm=yourController
</value>
</property>
</bean>
And the rest:
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
<webflow:flow-location id="index" path="/WEB-INF/initialflow.xml" />
</webflow:flow-registry>

Alfresco: Why is there no Scripts Extension folder, when there is a Web Script Extension folder?

When I learnt about web scripts, the tutorials said to leave the Web Scripts folder alone and instead to add my web script to the Web Scripts Extensions folder. My understanding is that this is so that I do not pollute Alfresco's out-of-the-box web scripts, and upgrading will be easier.
Why is there no equivalent extensions folder for the Scripts folder?
Or is my understanding of the reason for the Web Scripts vs Web Scripts Extensions folders wrong?
Extension folders provide a way to override default behavior. You can put your code there if you like, but doing so makes it more difficult to override it. I will use the repository as an example. Similiar logic applies to share.
Have a look at alfresco/WEB-INF/classes/alfresco/web-scripts-application-context.xml. It reads:
<bean name="webscripts.store.repo.extension" parent="webscripts.repostore">
<property name="store"><value>workspace://SpacesStore</value></property>
<property name="path"><value>/${spaces.company_home.childname}/${spaces.dictionary.childname}/cm:extensionwebscripts</value></property>
</bean>
<bean name="webscripts.store.repo" parent="webscripts.repostore">
<property name="mustExist"><value>true</value></property>
<property name="store"><value>workspace://SpacesStore</value></property>
<property name="path"><value>/${spaces.company_home.childname}/${spaces.dictionary.childname}/cm:webscripts</value></property>
</bean>
<bean id="webscripts.repoclasspathstore" class="org.alfresco.repo.web.scripts.RepoClassPathStore" abstract="true" />
<bean id="webscripts.store" parent="webscripts.repoclasspathstore">
<property name="mustExist"><value>false</value></property>
<property name="classPath"><value>webscripts</value></property>
</bean>
<bean id="webscripts.store.alfresco" parent="webscripts.repoclasspathstore">
<property name="mustExist"><value>false</value></property>
<property name="classPath"><value>alfresco/webscripts</value></property>
</bean>
<bean id="webscripts.store.client" parent="webscripts.repoclasspathstore">
<property name="mustExist"><value>true</value></property>
<property name="classPath"><value>alfresco/templates/webscripts</value></property>
</bean>
<bean id="webscripts.store.client.extension" parent="webscripts.classpathstore">
<property name="classPath"><value>alfresco/extension/templates/webscripts</value></property>
</bean>
<bean id="webscripts.searchpath" class="org.springframework.extensions.webscripts.SearchPath">
<property name="searchPath">
<list>
<ref bean="webscripts.store.repo.extension" />
<ref bean="webscripts.store.repo" />
<ref bean="webscripts.store.client.extension" />
<ref bean="webscripts.store.client" />
<ref bean="webscripts.store.alfresco" />
<ref bean="webscripts.store" />
</list>
</property>
</bean>
The searchPath property of webscripts.searchpath shows the search order. Search terminates as soon as it has a match. As you can see, it is no problem to put custom code in non extension locations as long as their path is unique.
I personally only put override webscripts in extension locations.
You can create a folder under Scripts called Extension and put all your script in that. Or just create different folders under Scripts for each function or project, it doesn't matter. They get picked up by Alfresco.

How to Load lables via Properties file in FreeMarker

I am using Spring MVC + FreeMarker integration. As I am new to FreeMarker i am not able to find out a way to configure FreeMarker labels from properties file.
Kindly help me out on this.
Thanks.
You can use, 'ResourceBundleMessageSource', message source for this.
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
<property name="basename" value="classpath:messages/messages" />
<property name="defaultEncoding" value="UTF-8"/>
/>
Define the locale resolver,
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
Define localeChangeInterceptor which detects the language parameter from the user session and call locale resolve. And register the interceptor in handler Mappings.
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Property files need to be defined like,
messages_en.properties
messages_ar.properties
inside the messages folder path.
'ReloadableResourceBundleMessageSource' can be used if the language property files frequently changes. That means you don't have to restart the application in each language file change.
You need to import spring macro,
<#import "/spring.ftl" as spring/>
and
messages can be accessed inside a .ftl as follows.
<#spring.message "customMessageKey"/>

Configuring FreeMarker In Spring Application

I am new to freemarker,but i want to fetch the data from Spring Application to my view which has extension .ftl.I am using ${message} but it displays as usual.It should display the data which is availble in message from Spring Application.
I am configured dispatcherServlet as below
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".ftl"/>
</bean>
You don't use InternalResourceViewResolver with Freemarker, you use FreemarkerViewResolver. Replace that, you should be fine.
Also, I suggest not putting your freemarker templates in /WEB-INF/jsp. They're not JSPs, and should never be treated as such.

Resources