I know configuring OSIVF is a common pain point. I've read all the pages I could find over that last couple of days, but nothing seems to get me past this issue. I have succeeded in getting myself confused, tho. I'm trying to keep this config really simple as the web app is pretty straightforward. Well, here's the error:
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:190)
Here's the web.xml:
<web-app 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>Test MVC Application</display-name>
<servlet>
<servlet-name>onepic</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>onepic</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Finally, here's some bits from onepic-servlet.xml:
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/onePic?characterEncoding=UTF-8"/>
<property name="username" value="onePic"/>
<property name="password" value="onePic"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource" />
<property name="packagesToScan" value="com.sandofamily.onePic"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
Again, I'm so sorry for re-asking such a common problem, but I must be missing some bit of knowledge that ties this all together.
You haven't configured your Context in your web.xml, so when Spring tries to find its Context you get this error.
This shows how to configure Spring using a xml file, but you can also do this using a Java configuration based. You can find something here.
Related
I got an error when running my web app 'parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]' which means that it didn't find the path I specified in springbeans.xml. So tried another approach as follows:
web.xml (The important part)
<servlet>
<servlet-name>
spring
</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>
spring
</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>C:\Users\alamo\eclipse-
workspace\coreproject\src\main\resources\SpringBeans.xml
</param-value>
</context-param>
-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
First, I tried with just the context-param and the load listener, and then I add the servlet part and I moved the Springbeans.xml to the folder WEB-INF and renamed it to spring-servlet.xml. The version of spring is 3.2.16.
spring-servlet.xml
<beans xmlns="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-2.5.xsd">
<bean class="org.springframework.beans.factory.config.
PropertyPlaceholderConfigurer">
<property name="location">
<value>C:\Users\alamo\eclipse-
workspace\coreproject\src\main\resources\application.properties
</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="myAction" class="coreproject.action.MyAction">
<property name="userProvider" ref="userProvider" />
</bean>
<bean id="userProvider" class="coreproject.provider.UserProviderImpl">
<property name="userDAO" ref="userDAO" />
</bean>
<bean id="userDAO" class="coreproject.dao.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
I'm still getting the same error. What can I do?
I have problem when I'm trying to run my spring app on tomcat server. I'm getting error 404 in browser and this in console:
2016-05-09 12:26:03,188 INFO org.hibernate.tool.hbm2ddl.SchemaExport - HHH000230: Schema export complete
2016-05-09 12:26:05,082 INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'DispatcherServlet': initialization completed in 5685 ms
2016-05-09 12:26:05,121 WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/webstore/] in DispatcherServlet with name 'DispatcherServlet'
This is my web.xml file:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter>
<filter-name>encoding-filter</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>encoding-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/webcontext/DispatcherServlet-context.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/webcontext/security-context.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Can you say me what's the problem? It's strange, as I'm not the only one who works with this code, and only I have this problem...
Oh. And my DispatcherServlet.xml looks like that:
<?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: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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<import resource="classpath:spring/database-context.xml" />
<mvc:annotation-driven enable-matrix-variables="true">
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jsonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="pl.spring.demo" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
<bean id="jsonObjectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper" />
</property>
</bean>
You need to check in your application server the following points:
Is there any deployment error message?
The application has been deployed but with a different context? (for example / instead of /webstore)
Do you have multiple copies of your application server but you are running one and trying to deploy your application in another one?
I am studying a existing Spring MVC 3 project, while looking into spring and context config files I get confused, please clear it or suggest me If something is wrong.
Upadte root-context.xml file
<beans xmlns="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.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
<property name="basenamePrefix" value="detailtheme-" />
</bean>
<bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
<property name="defaultThemeName" value="en" />
</bean>
<!-- Helper bean to load all properties files -->
<bean id="LoadPropertiesFiles" class="org.commons.utilities.LoadPropertiesFileHelper"
init-method="loadPropertiesFileMethod" lazy-init="false" />
</beans>
Here I don't understand what is lang? What I understand is it's a veriable name whose value is assigned to paramName (DEFAULT_PARAM_NAME), but I don't understand how value is assigned to lang because I don't find any single location where some value (like en,hi..etc) is set.
The most confusing thing is one more bean with same class is defined in servlet-context.xml as :
<mvc:interceptors>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="lang" />
</beans:bean>
...
</mvc:interceptors>
Why two beans of same class is defined, is this wrong? if not, then what is work of bean defined in root-context.xml and servlet-context.xml?
Below is web.xml for reference:
<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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Listener to prevent class loader leaks -->
<listener>
<listener-class>se.jiderhamn.classloader.leak.prevention.ClassLoaderLeakPreventor</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<jsp-config>
<taglib>
<taglib-uri>/tagTld</taglib-uri>
<taglib-location>/resources/tld/EnumTag.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
LocaleChangeInterceptor will intercept web requests to your web app, and look for query param with name lang (E.g. http://mywebapp.com/login?lang=en), and try to set app's locale accordingly so that you can do localization of your web app.
As far as two files root-context.xml and servlet-context.xml is concerned - first file is being used by <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> and second file is being used by org.springframework.web.servlet.DispatcherServlet.
ContextLoaderListener can be used to initialize Spring even when you are not necessarily using Spring MVC. The DispatcherServlet is specific to Spring MVC and is needed if you are making use of it.
It may be possible to get rid of root-context.xml, but it will require you to review design of your app as there may be non-SpringMVC components that depend on beans defined root-context.xml.
Indeed, this bean definition does not belong to the root context, so you can safely delete the one defined in root-context.xml and just leave the one in servlet-context.xml.
As for the paramName parameter, this is the name of the request parameter that will be used to change the locale - see reference documentation.
I tried to find some explanation for what really happens when we do not register ContextLoaderListener in our web.xml file, mainly because I have a simple Spring + Hibernate app that works fine if I DO NOT declare ContextLoaderListener. When I do, I get 404 error.
web.xml:
<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/applicationContext.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>
applicationContext.xml:
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="org.myfantasticwebsite"/>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/pizzashop"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>org.myfantasticwebsite</value>
</array>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
I also have index.jsp that just displays some stuff from database. All 3 files (web.xml, applicationContext.xml and index.jsp) are located in WEB-INF directory, and everything works fine until I add ContextLoaderListener bean to web.xml. After, I get Page not found error on address localhost:8080/pizzashop.
This is just a simple tutorial I found on internet, that's why all configuration is in one file. I was actually trying make separate config files - 2 files stemming from web.xml (dispatcher-servlet.xml with web related config and applicationContext.xml with everything else). Further more to extract hibernate specific config to hibernate.cfg.xml, and spring+hibernate config to hibernate-context.xml.
Can someone please explain why I get 404 error, I'm stuck here for a week now. Thank you.
This is the only controller - PizzaController:
#Controller
#RequestMapping("/")
public class PizzaController {
#Autowired private PizzaDAO pizzaDAO;
/**
* This handler method is invoked when
* http://localhost:8080/pizzashop is requested.
* The method returns view name "index"
* which will be resolved into /WEB-INF/index.jsp.
* See src/main/webapp/WEB-INF/applicationContext.xml
*/
#RequestMapping(method = RequestMethod.GET)
public String list(Model model) {
List<Pizza> pizzas = pizzaDAO.findAll();
model.addAttribute("pizzas", pizzas);
return "index";
}
}
UPDATE: web.xml with ContextLoaderListener:
<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/applicationContext.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>
I'm working on a project using Hibernate for persisting and Struts 2 for the view pattern.
My configuration files are:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
//......
//.....
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- The defintion of the root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Creates the spring Container shared by all servlet and filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter> <!-- Get spring to keep the session open for the whole request, so hibernate's lazy loads work -->
<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>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans >
//.......
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/DB_TEST"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"></property>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config></context:annotation-config>
</beans>
My problem is I can't keep Hibernate session open in the view pattern of Struts 2, means when I try to load some data that are not already initialized with Hibernate (like collections for example) i get the org.hibernate.LazyInitializationException, so after doing some research I found that I must add this scope in web.xml to keep the session open in the view pattern.
Scope:
<filter> <!-- Get spring to keep the session open for the whole request, so hibernate's lazy loads work -->
<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>
But even with this I still have the same problem, so can anyone tell me what i'm doing wrong.
The order of filter chain is important. In your case the session should be opened before struts is executing actions and closed after that. The last thing is done by spring via managing Hibernate session. So, reorder filters and allow struts2 dispatcher accept requests from the first filter.
<filter> <!-- Get spring to keep the session open for the whole request, so hibernate's lazy loads work -->
<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>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
I've had similar problem some time ago, and to resolve this I used hibernate.enable_lazy_load_no_trans property instead of OpenSessionInView pattern. More informations about LazyInitializationException you can find here or here