Deploying a spring-mvc application failed - java

I am working with spring-mvc with hibernate, and I have deployed my application on go daddy server, when I open any html file it works well, But on opening a jsp file, gives me a 404 file not found error. Please any one can help me with the problem...?
I m using Spring 3.0,
Jdk 1.7.0_04,
apache-tomcat 6.0.32
The web.xml I'm using is
web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>seekersworkroom</display-name>
<welcome-file-list>
<welcome-file>/view/Index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>imageupload</servlet-name>
<servlet-class>com.seekersworkroom.controller.imageuploadController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageupload</servlet-name>
<url-pattern>/imageupload.htm</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Userimageupload</servlet-name>
<servlet-class>com.seekersworkroom.controller.UserimageuploadController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Userimageupload</servlet-name>
<url-pattern>/Userimageupload.htm</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>seekersworkroom</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>seekersworkroom</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>seekersworkroom-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/seekersworkroom-servlet.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
</web-app>
The welcome doesnot dispaly index.jsp file but if the same is replaced by index.html it shows this file. And by opening any url eith .jsp extension it shows 404 error.
seekersworkroom-servlet:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="/login/*.htm" id="AdminprofileController" class="com.seekersworkroom.controller.AdminprofileController">
</bean>
<bean name="/Index/*.htm" id="IndexController" class="com.seekersworkroom.controller.IndexController">
<property name="indexDAO" ref="IndexDAO" />
<property name="skillsaddedDAO" ref="SkillsaddedDAO" />
<property name="skillsDAO" ref="SkillsDAO"></property>
<property name="userDAO" ref="UserDAO" />
<property name="acountryDAO" ref="acountryDAO" />
<property name="bregDAO" ref="BregistrationDAO" />
<property name="cregistrationDAO" ref="CregistrationDAO" />
<property name="ccontactDAO" ref="CcontactDAO" />
<property name="caddressDAO" ref="CaddressDAO" />
<property name="asubcategoryDAO" ref="AsubcategoryDAO" />
<property name="cportfolioDAO" ref="CportfolioDAO" />
<property name="keywordDAO" ref="KeywordDAO" />
<property name="bcreatejobDAO" ref="BcreatejobDAO" />
<property name="messageDAO" ref="messageDAO"></property>
<property name="userimageDAO" ref="UserimageDAO" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/view/" p:suffix=".jsp" />
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/skw"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<value>com/seekersworkroom/VO/User.hbm.xml</value>
<value>com/seekersworkroom/VO/buyerregistration.hbm.xml</value>
<value>com/seekersworkroom/VO/contractorregistration.hbm.xml</value>
<value>com/seekersworkroom/VO/Contractorcontact.hbm.xml</value>
<value>com/seekersworkroom/VO/Contractoraddress.hbm.xml</value>
<value>com/seekersworkroom/VO/Contractorkeyword.hbm.xml</value>
<value>com/seekersworkroom/VO/Userimage.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="acountryDAO" class="com.seekersworkroom.DAO.AcountryDAO" >
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<bean id="AcategoriesDAO" class="com.seekersworkroom.DAO.AcategoriesDAO" >
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
</beans>
This is the servlet file in which the annotaions are used to cal to particular controllers.
This servlet contains the beans through which the request is redirected to particular controllers.
But the main problem is jsp file is not working only...
What can I do in web.xml so that it allows me to access the jsp file on server side??
Is that the problem with Spring framework or the web.xml file is missing something???

I assume the jsp you are accessing is outside of your WEB-INF folder. In such cases, use <mvc:resources mapping="/static/**" location="/" /> in your seekersworkroom-servlet.xml; and access the jsp using url http://yourdomain/static/yourjspname.jsp.
Also, you may not need to protect your static resources; So, use
<http pattern="/static/**" security="none" xmlns="http://www.springframework.org/schema/security"/> in your spring security configuration file

Related

Spring application AJAX call after login returns 404 not found

I ahve a spring application where an ajax call after login dosen't work.
I made the ajax function as below in CHrome dev tools snippets.
var today = new Date();
var month=today.getMonth();
var name="testuser";
$(document).ready(function(){
$.ajax({
type: "GET",
url: "ajax/getStmt",
data: "name="+name+"&month="+month,
success: function(msg){
alert( "Data Saved: " + msg );
},
error: function(msg){
alert("there was an error \n"+msg);
}
});
});
controller:
package com.cardholder.controller.ajax;
#Controller
public class StatementAjax {
#Autowired
public UserDao userDao;
//initialise logger, dao
#RequestMapping(value = "/ajax/getMonths", method = RequestMethod.GET)
public String getMonths(){
//get user from session
//get user token from session
/*UserStatement availableStmts = new UserStatement();
availableStmts.setAvailableStatements(userDao.getUserStatementMonths(user.getUser_token()));*/
return "months";
}
#RequestMapping(value = "/ajax/getStmt", method = RequestMethod.GET)
public String getStatement(#RequestParam(required = true) String name, #RequestParam(required = true) String month){
System.out.println(name+", "+month);
//get user from session
//get user token from session
/*UserStatement availableStmts = new UserStatement();
availableStmts.setAvailableStatements(userDao.getUserStatementMonths(user.getUser_token()));
ArrayList<UserStatement> s = (ArrayList<UserStatement>) userDao.getUserSelectedStatement(user.getUser_token(), 07, 2015);*/
return "statements";
}
}
Spring xml.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.cardholder,com.cardholder.orm,com.cardholder.controller.ajax" />
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<bean id="secureServiceClient" class="com.cardholder.service.SecureServiceClient">
<property name="serviceUrl" value="http://xxxx:8080/xApi/api/"></property>
<property name="serviceUserName" value="xxxx"></property>
<property name="servicePassword" value="xxxx"></property>
</bean>
<bean id="httpHeders" class="org.springframework.http.HttpHeaders">
</bean>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="errorHandler">
<bean class= "com.cardholder.service.error.ServiceClientErrorHandler" />
</property>
</bean>
<bean id="resourceLoader" class="org.springframework.core.io.DefaultResourceLoader"/>
<bean id="pdfRenderer" class="com.cardholder.controller.menunav.util.Util"/>
<bean id="serviceClientInterceptor" class="com.cardholder.service.interceptors.ServiceClientInterceptor">
<property name="serviceUserName" value="xxx"></property>
<property name="servicePassword" value="xxx"></property>
</bean>
<bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<constructor-arg name="strength" value="16" />
</bean>
<bean id="idGenerator" class="org.apache.catalina.util.SessionIdGenerator"></bean>
<bean id="sessionValidator" class="com.cardholder.session.SessionValidator"></bean>
<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.cardholder"></property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://xxxx:3306/portal" />
<property name="username" value="xxxx" />
<property name="password" value="xxxx" />
</bean>
Below is the link to a screenshot from devtools.
http://i.stack.imgur.com/EXry7.png
As far as i know the request dosent even enter the application context even if the URL its trying to access is right.
i have a debug point in the dispatcher servlet's do dispatch method which should have been the first place to be reached before any interceptors, controllers etc. i dont see any thread pausing there.
EDITS:
adding Web.xml
<?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"
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"
metadata-complete="true">
<display-name>NCHP</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.*</url-pattern>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>land.htm</welcome-file>
</welcome-file-list>
</web-app>
Thanks for posting your web.xml.
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.*</url-pattern>
<url-pattern>*.htm</url-pattern>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Based on the above config Spring's dispatcher will be consulted, only if the incoming URL has some path info. e.g. login.htm, something.xx. However, if the url has no path info, like /getStmt, the dispatcher will not receive the request.
You can either change your ajax end point to some thing like /getStmt.ajax. Or add /as a url pattern in the web xml (this could have other implications, based on how other resources are served in your project).
If you are currently serving pages through xxxx.htm convention, I would recommend using xxxx.ajax convention for ajax requests.
You might also want to look at this answer : No mapping found for HTTP request with URI Spring MVC
Hope this helps.

Spring request scope in HttpRequestHandlerServlet does not work

I can't understand why the configuration does not work.
I make spring web applcation without mvc and use HttpRequestHandlerServlet class. I need that all beans use one Connection in one request. And I set request scope in Connection bean but when I run it:
IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
My web.app is:
<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">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>monitoring</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>monitoring</servlet-name>
<url-pattern>/monitoring</url-pattern>
</servlet-mapping>
My app-context is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost/nts" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="maxPoolSize" value="20" />
<property name="minPoolSize" value="5" />
<property name="maxStatements" value="100" />
<property name="testConnectionOnCheckout" value="true" />
</bean>
<bean id="conn" class="java.sql.Connection" factory-bean="dataSource" factory-method="getConnection" scope="request"/>
<bean id="monitoring" class="com.sotas.terminal.server.servlet.MonitoringServlet">
<property name="monitoringService">
<bean class="com.sotas.terminal.server.service.MonitoringService">
<property name="conn" ref="conn"/>
<property name="providerDAO">
<bean class="com.sotas.terminal.server.dao.ProviderDAOImpl">
<property name="conn" ref="conn"/>
</bean>
</property>
</bean>
</property>
</bean>
The error message is pretty helpful. Since you aren't using the Dispatcher Servlet in your web.xml (as for a standard spring mvc app):
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
You need to find another way to give spring access to the request. Adding the RequestContextListener to your web.xml should do the trick:
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
I thing I understand. HttpRequestHandlerServlet is singleton. RequestHandler as field of HttpRequestHandlerServlet must be proxy to refresh all "request" scope beans inside
and RequestContextListener must be in web.xml:
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
it is work app-context.xml:
<bean id="monitoring" class="com.sotas.terminal.server.servlet.MonitoringServlet" scope="request">
<property name="conn" ref="conn"/>
<aop:scoped-proxy/>
<property name="monitoringService">
<bean class="com.sotas.terminal.server.service.MonitoringService" scope="request">
<property name="conn" ref="conn"/>
<property name="providerDAO">
<bean class="com.sotas.terminal.server.dao.ProviderDAOImpl" scope="request">
<property name="conn" ref="conn"/>
</bean>
</property>
</bean>
</property>
</bean>

Spring does not connect to mysql database using JPA datasource

I have a spring project and i want my tables created by hibernate but i think my app can't connect to database. I changed my datasource db url to be wrong and there is no exception at all. When i change packagesToScan property to a value which does not exist, still there is no exceptions. So the problem is that spring does not try to connect to the db.
Here is my root-context.xml.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:property-placeholder location="classpath*:application.properties" />
<context:component-scan base-package="com.istiletisim.test" />
<context:annotation-config />
<tx:annotation-driven/>
<task:annotation-driven />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>test2</value>
</property>
<property name="password">
<value>1234</value>
</property>
<property name="poolPreparedStatements">
<value>true</value>
</property>
<property name="maxIdle">
<value>1000</value>
</property>
<property name="testWhileIdle">
<value>true</value>
</property>
<property name="testOnBorrow">
<value>true</value>
</property>
<property name="validationQuery">
<value>SELECT 1</value>
</property>
<property name="maxActive">
<value>50</value>
</property>
<property name="maxWait">
<value>1000</value>
</property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.istiletisim.test.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
<prop key="hibernate.id.new_generator_mappings">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
And web.xml file here :
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml,
/WEB-INF/spring/security.xml
</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Thanks.

Deploying Web App With Spring MVC

I have never come across this problem before. Actaully, I have a web application running through a war file. I have configured Spring to work with it and it works perfectly.
The problem is that I am trying to configure Activiti through Spring. Basically, there is a set of .bpmn20.xml files inside the WEB-INF/processes folder. The Activiti team has mentioned that they do not know how to configure this inside a web application. As a standlone application I can deploy resources .bpmn20.xml files automatically as the processes folder is on the classpath. I have having trouble configuring in a web application structure.
Please see below:
I am created a Spring MVC application and created an Activiti database by running the DbSchemaCreate.main(). Actaully my processes don't seem to deploy on the war file. When Tomcat starts the ProcessEngine is started through Spring and works. I can access the RuntimeService. The code can be seen below:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web- app_2_5.xsd"
version="2.5">
<display-name>WebApp</display-name>
<context-param>
<!-- Specifies the list of Spring Configuration files in comma separated format.-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/activiti.xml
</param-value>
</context-param>
<listener>
<!-- Loads your Configuration Files-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
activiti.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="databaseSchemaUpdate" value="true"/>
<property name="jobExecutorActivate" value="false"/>
<property name="transactionManager" ref="transactionManager"></property>
<!-- <propety name="beans">
<map>
<entry key="printer" value-ref="printer"/>
</map>
</property>-->
<property name="deploymentResources" value="classpath*:/processes.*.bpmn20.xml"/>
</bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/activiti_example"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>
The automatic resosource deployment is not being deployed. I have also tried through the code to deploy but it throws an Exception:
repositoryService.createDeployment().addClasspathResource("ProcessExample.bpmn20.xml").deploy();
runtimeService.startProcessInstanceByKey("processExample", mapOfProcessVariables);
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.activiti.engine.ActivitiException: resource 'ProcessExample.bpmn20.xml' not found
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
org.activiti.engine.ActivitiException: resource 'ProcessExample.bpmn20.xml' not found
org.activiti.engine.impl.repository.DeploymentBuilderImpl.addClasspathResource (DeploymentBuilderImpl.java:59)
com.webchannel.web.EmailController.sendE(EController.java:46)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
How can this resource be located inside the webapp?
I have tried putting it in WEB-INF/processes/ProcessExample.bpmn20.xml.
I have also tried:
<property name="deploymentResources" value="/WEB-INF/processes.*.bpmn20.xml"/>
EDIT
This website may help, but I am stuck.
Is WEB-INF in the CLASSPATH?
Maybe try adding the folder where the process is defined (your bpmn20.xml file) to the build path.
The error tells you, that the Activiti-Engine cannot find your file, that's why you need to tell it where to find it.
Try This :
<web-app 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"
version="3.1">
<!-- for specifies the Web application display name -->
<display-name>APMC</display-name>
<!-- For Authentication processing mechanisms -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- For mapping request resource and combine its results with the matching JSP -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- ContextLoaderListener provides access to the ServletContext -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-config.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Above file is web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.apmc.dao" />
<context:component-scan base-package="com.apmc.services" />
<task:annotation-driven executor="taskExecutor" scheduler="taskScheduler" />
<task:executor id="taskExecutor" pool-size="1" />
<task:scheduler id="taskScheduler" pool-size="1" />
<context:component-scan base-package="com.apmc.controller" />
<context:component-scan base-package="com.apmc.rest" />
<context:property-placeholder location="classpath:database.properties" />
<context:property-placeholder location="classpath:log4j.properties" />
<mvc:resources mapping="/resources/**" location="/resources/mytheme/" />
<mvc:annotation-driven />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.apmc.domain.Vehicle</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
Above code is spring-config.xml
Add database.properties file in resources folder of src folder. So write code in database.properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/apmc_db
database.user=root
database.password=
hibernate.show_sql=true

OpenSessionInViewFilter doesnt work in a JSF+Spring+Hibernate application

I want to use OpenSessionInViewFilter in my JSF 2.1+Spring 3.1+Hibernate 3.6.6 project
in order to get rid of the .LazyInitializationException.
Although i declare the filter in web.xml i keep getting ;
org.hibernate.LazyInitializationException: failed to lazily initialize
a collection of role: pts.entity.Invention.inventors, no
session or session was closed
My web.xml
<?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_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<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>*.tt</url-pattern>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>excite-bike</param-value>
</context-param>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
My applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="classpath:applicationContext-security.xml" />
<context:annotation-config />
<context:component-scan base-package="pts.component" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>pts.entity</value>
</list>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/PTS" />
<property name="username" value="root" /> <property name="password"
value="" />
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"
name="template">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManager" />
</bean>
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="view">
<bean class="pts.scope.ViewScope" />
</entry>
</map>
</property>
</bean>
</beans>
I use spring managed(HibernateTemplate) hibernate daos for db operations.Thanks for the help.
You just have to be sure that you are not redirecting between JSF view for example:
In this view your Open Session in vew Filter will work:
<navigation-rule>
<from-view-id>start.xhtml</from-view-id>
<navigation-case>
<from-outcome>page1</from-outcome>
<to-view-id>page1.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
and if you modify this example this way:
<navigation-rule>
<from-view-id>start.xhtml</from-view-id>
<navigation-case>
<from-outcome>page1</from-outcome>
<to-view-id>page1.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
Your Filter will bnot help you because you are redirecting and this is completely new request and your original Hibernate session is already closed.
Inspired by this post:
OpenSessionInViewFilter +Redirect in JSF
When does it fail? I can see that you use Spring Security and have springSecurityFilterChain defined in you web.xml. And would assume that filter loads user from database and user has lazy collection that is accessed later, when Hibernate session created during authentication is propably closed, before OpenSessionInView filter starts executing.
Remember that filter-mapping order makes sense, filter executed in the order of their filter mappings are declared. I would try reorder them first.

Categories

Resources