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

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>

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/

using c3p0 with java MVC (JDK 1.6)

I'm trying to create global connections for multiple WAR files (apps) to use.
Each project use entity manager architecture and maven. I'm using spring 3.5.1, tomcat 6.0.39, JDK 1.6.0_45, C3P0 0.9.1.2, hibernate-c3p0 4.2.3.Final, com.mchange c3p0 0.9.2
I saw examples all over but none works for me, I'm not sure why.
I tried almost everything, but I have no clue what's wrong.
I've written into server.xml the following:
<Resource auth="Container"
description="DB Connection"
driverClass="com.mysql.jdbc.Driver"
maxPoolSize="20"
minPoolSize="12"
acquireIncrement="1"
name="jdbc/testdb1"
user="root"
password=""
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/test_db1?autoReconnect=true" />
and into context.xml the following:
<ResourceLink name="jdbc/testdb1"
global="jdbc/testdb1"
type="javax.sql.DataSource" />
and into web.xml the following:
<resource-ref>
<res-ref-name>jdbc/testdb1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
and my project (app) specific configuration:
PART 1 (JPA XML FILE):
<bean id="transactionManagerMysql" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emfMysql"/>
</bean>
<bean id="emfMysql" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="jdbc/testdb1" />
<property name="persistenceXmlLocation" value="classpath:persistence-infrastructure.xml" />
<property name="persistenceUnitName" value="puMysql" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<jpa:repositories base-package="domain.data.repository"
entity-manager-factory-ref="emfMysql"
transaction-manager-ref="transactionManagerMysql"/>
PART 2 (PERSISTENCE XML FILE):
<persistence-unit name="puMysql" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>domain.Mysqltable1</class>
<properties>
<property name="hibernate.connection.datasource" value="jdbc/testdb1"/>
</properties>
</persistence-unit>
I get an exception:
Error creating bean with name 'emfMysql' defined in class path resource [datasource-tx-jpa-infrastructure.xml]: Cannot resolve reference to bean 'jdbc/testdb1' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'jdbc/testdb1' is defined
I hope someone can help me :)
Thanks ahead
It said that the datasource resource you defined in tomcat server.xml is not a bean in spring. You should define a datasource bean like this:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/DatabaseName"/>
</bean>
And use this bean instead of jndi name for datasource property of emfMysql.
Please refer this answer

Spring MVC and Shiro Configuration using ini files

I'm trying to set up an environment with Spring MVC and Apache Shiro. I'm following articles mentioned in shiro.apache.org.
I'm using Spring's DelegatingFilterProxy as Shiro Filter in web.xml.
The current filtering is done using :
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login"/>
<property name="successUrl" value="/dashboard"/>
<property name="unauthorizedUrl" value="/unauthorized"/>
<property name="filterChainDefinitions">
<value>
/** = authc, user, admin
/admin/** = authc, admin
/login = anon
</value>
</property>
</bean>
Question is, how do I use shiro.ini file defining security settings?
You don't need to use shiro.ini. All of the rest of your configuration can (and should, since you're using ShiroFilterFactoryBean) be done in Spring.
For example, adding a securityManager and ehCache based cache manager to your shiroFilter:
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm"/>
<property name="sessionMode" value="native"/>
<property name="sessionManager" ref="sessionManager"/>
<property name="cacheManager" ref="cacheManager"/>
</bean>
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="ehCacheManager"/>
</bean>
<bean id="ehCacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/>
<bean id="sessionDAO"
class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"/>
<bean id="sessionManager"
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionDAO" ref="sessionDAO"/>
</bean>
<bean id="myRealm" class="com.foo.MyRealm"/>
You can check shiro documentation here http://shiro.apache.org/reference.html, it contains everything, in spring, as Les said, usually define different beans instead of using the shiro.ini file, but also you can use this file for authentication, use IniRealm like:
<bean id="myRealm" class="org.apache.shiro.realm.text.IniRealm">
<property name="resourcePath" value="classpath:/shiro.ini" />
</bean>
more detail refers to here

Unable to get FlowHandlerMapping working in Spring Webflow

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.

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