How to serve static resources using Spring's mvc:resources on WebLogic? - spring-mvc

We have a web app that works beautifully on tomcat, but fails to run on a latest weblogic cause of static resource loading issues - basically we serve all resources from /static/** and have set this up in the spring servlet xml file like this:
<mvc:resources mapping="/static/**" location="/static/" />
This works on tomcat, but on weblogic you simply see an ugly page as all CSS/JS/jpgs within the static directoty cannot be found.
I played with this, too:
<mvc:default-servlet-handler />
I placed it once at the end of the spring config and at the beginning, but no result...
How to serve our static resources?

for a start I would check 2 things. First as I am not a Spring user I am not sure what method spring uses to find and load the files and if it uses getRealPath you could try enabling this in weblogic.
In admin console you click in the domain name, web applications and down the page(last option) you will find a checkbox to enable getRealPath to work even when your app is packaged in a .war or .ear file. You will need to restart the servers for your domain for this configuration to take effect.
If this does not fix your problem you could try mapping the static files using weblogic.xml file.
You will have to use the weblogic tags like:
<wls:virtual-directory-mapping>
<wls:local-path>/var/docs/weblogic</wls:local-path>
<wls:url-pattern>/static/*</wls:url-pattern>
</wls:virtual-directory-mapping>
In the example above you would have a file under fisic disc path /var/docs/weblogic suppose it is named myfile.css to be loaded correctly by the complete url: http://seudominio.com/static/myfile.css or using the relative path /static/myfile.css
I believe one of these approaches could help you.
regards.

For my Spring MVC I have below platform and that works quite well for static resources like CSS or JS.
Platform
Spring MVC 4.2.0
Hibernate 4.2.20
Weblogic 10.3.6
Eclipse
create resources folder inside /WebContent folder & outside /WebContent/WEB-INF. So my Application structure would be like below.
In the front controller config XML file i.e. dispatcher-config.xml should be as below.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- default page to show when app starts -->
<mvc:view-controller path="/" view-name="Home"/>
<!-- essentially sets you your Spring context to allow for dispatching requests to Controllers -->
<mvc:annotation-driven />
<!-- used to load static resources like css, js etc... -->
<mvc:default-servlet-handler/>
<!-- automatically wire values into properties, methods, and constructors. -->
<context:annotation-config/>
<!-- scan for components like #Controller, #Repository, #Service, #Component etc...-->
<context:component-scan base-package="au.com.snh" />
<!-- spring view resolver bean -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- load database properties file -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- declare beans -->
<bean id="regionDao" class="au.com.snh.dao.RegionDaoImpl" />
<bean id="regionService" class="au.com.snh.service.RegionServiceImpl" />
<!-- declare datasource bean -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.pwd}" />
</bean>
<!-- hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="au.com.snh.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- resource bundles -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/propertybundle/common"/>
</bean>
In above config file notice below 2 tags are important for static content i.e css
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
I have included the CSS file in my JSP as below.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome to the World of Spring MVC</title>
<link rel="stylesheet" href="/${initParam.appRootPath}/resources/css/main.css">
</head>
<body>
<center>
<h1>Welcome to the World of Spring MVC 4.2 & Hibernate 4.2 </h1>
</center>
</body>
</html>
FYI, I have also included here 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" id="WebApp_ID" version="2.5">
<display-name>SpringMVCHibernateProject</display-name>
<!-- global variables -->
<context-param>
<param-name>appRootPath</param-name>
<param-value>SpringMVCHibernateProject</param-value>
</context-param>
<!-- front controller -->
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Hopefully my post would be useful.
Thanks - Hitesh

Related

Include CSS in JSP page of Dynamic Web Application

My JSP pages in my dynamic web application (in Eclipse) are not being styled by my CSS code. I have included a stylesheet in index.jsp as follows:
index.jsp
<html>
<head>
<title>To Do List - Home</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/stylesheet.css">
</head>
<body>
Tasks
</body>
</html>
And my project structure is as follows:
I thought that href="${pageContext.request.contextPath}/css/stylesheet.css would look for stylesheet.css in ToDoList/WebContent/css/. If I try navigating directly to the stylesheet in the browser via http://localhost:8080/ToDoList/css/stylesheet.css it returns a 404 error.
I am aware this question has been asked before but from looking at the other questions I still can't figure out what is wrong with my project structure.
Update:
So I added <mvc:resources mapping="/css/**" location="/css/" /> to my servlet config, but now when I navigate to any page other than index.jsp I get a 404 error.
todolist-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scan for JavaConfig, annotated with #Configuration -->
<context:component-scan base-package="com.petehallw.todolist.main" />
<context:annotation-config/>
<mvc:resources mapping="/css/**" location="/css/" />
<!-- Configure Spring view resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
The error I get is:
WARNING: No mapping found for HTTP request with URI [/ToDoList/tasks] in DispatcherServlet with name 'todolist'
This is upon clicking a link to "tasks" in index.jsp which was previously returning the tasks.jsp page.
For every URL in the app, you can make use of c:url tag in JSTL lib.
Tasks.
So, if you have controller method mapping #requestmapping("/tasks"), it will be invoked.
Or try as Tasks like you use at css files.
Important: You should add <mvc:annotation-driven /> in xml configuration for the support of annotation-driven MVC controllers like #RequestMapping, #Controller.

trying to add JSF to my Spring WebFlow Project and now my WebFlow will not working

I am trying to add some JSF to my Spring WebFlow project and I made some changes trying to following the details at:
http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/ch13s07.html
but now my webflow project will not work.
Here is my old flow.xml file that worked:
<?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:flow="http://www.springframework.org/schema/webflow-config"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd
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">
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<!-- Executes flows: the entry point into the Spring Web Flow system -->
<flow:flow-executor id="flowExecutor" />
<!-- The registry of executable flow definitions -->
<flow:flow-registry id="flowRegistry"
flow-builder-services="flowBuilderServices"
base-path="/WEB-INF/flows">
<flow:flow-location-pattern value="/**/*-flow.xml" />
</flow:flow-registry>
<flow:flow-builder-services id="flowBuilderServices"
view-factory-creator="mvcViewFactoryCreator"
validator="validator"
/>
<bean id="mvcViewFactoryCreator" class=
"org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="defaultViewSuffix" value=".jsp" />
</bean>
<!--Maps request paths to flows in the flowRegistry-->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry" />
</bean>
<!--
Dispatches requests mapped to flows to FlowHandler implementations
-->
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
</beans>
Now here is my new flow.xml file that does not work:
<?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:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd
http://www.springframework.org/schema/faces
http://www.springframework.org/schema/faces/spring-faces-2.2.xsd">
<!-- Executes flows: the central entry point into the Spring Web Flow system -->
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextListener"/>
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<!-- The registry of executable flow definitions -->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices" base-path="/WEB-INF">
<webflow:flow-location-pattern value="**/*-flow.xml" />
</webflow:flow-registry>
<!-- Configures the Spring Web Flow JSF integration -->
<faces:flow-builder-services id="flowBuilderServices" />
<!-- A listener maintain one FacesContext instance per Web Flow request. -->
<bean id="facesContextListener"
class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener" />
</beans>
and now for my faces-config.xml file:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config 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-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
So can someone please tell me why webflow stopped working!!
I think you are trying to load faces-config.xml as a Spring configuration file. The JSF configuration file (faces-config.xml) is where you register a JSF application's resources and it is not a Spring configuration file so do not include it with other Spring config files.
Also /WEB-INF folder is the default place to put faces-config.xml. If you have it in /WEB-INF/spring folder then you need to mention the path in the web.xml something like this:
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>
/WEB-INF/faces/faces-config.xml
</param-value>
</context-param>
Unrelated to the concrete issue you also need 'SpringBeanFacesELResolver' in your faces-config.xml to resolve any Spring beans before you inject them in any JSF managed beans.
<faces-config>
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<faces-config>

How to configure MultipartResolver?

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources/ directory -->
<resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
id="multipartResolver"> <property name="maxUploadSize" value="500000" />
</bean>
There is my context.xml file but when I include the multipartResolver configuration I get the following error:
"cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'bean'.".
Please help me, I am new to spring.
I think you should prefix bean and property with beans:
...
<beans:bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<beans:property name="maxUploadSize" value="500000" />
</beans:bean>
...
and remember to close the XML with the end tag:
...
</beans:beans>

Spring and tag mvc resources, can't reach .css, and .js files

My application use Spring 3.0.4(this is first version where tag mvc:resources work fine).
The current issue is that my app can not reach .css and .js files from the mapped resources.
Structure test.war:
/test -root
|
/static-resource
|
/css
|
/screen.css
|
/js
|
/WEB-INF
|
/index.jsp
My test-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/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
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/static-resource/"/>
<context:annotation-config/>
<context:component-scan base-package="org.web"/>
<tx:annotation-driven/>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<bean id="authenticationInterceptor" class="org.util.AuthenticationInterceptor"/>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="-1"/>
</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"/>
<property name="cookieName" value="bovalta_language"/>
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="authenticationInterceptor"/>
<ref bean="localeChangeInterceptor"/>
</list>
</property>
</bean>
</beans>
In the index.jsp I try access to the resources on two ways with spring url and with JSTL url like below
<%-- With JSTL url --%>
<link media="screen" rel="stylesheet" href="<c:url value="/resources/css/screen.css"/>" type="text/css"/>
<%-- With Spring url --%>
<spring:url value="/resources" var="resourceUrl"/>
<link media="screen" rel="stylesheet" href="${resourceUrl}/css/screen.css" type="text/css" />
When I undeploy war file in the Tomcat AS, application work fine without any exception, but Tomcat server can not find my css and js files from resources.
I try to reach css file through url http://localhost:8080/test/resources/css/screen.css
but Tomcat not found it. Any suggestion will be useful.
Thanks in advance
FYI
For all that would have same problem as I have, I resolved my issue and shared with you:
test-servlet.xml is same without modification as in my above post.
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/static-resource/"/>
in the index.jsp page will be:
<spring:url value="/resources/css/screen.css" var="resourceUrl"/>
<link media="screen" rel="stylesheet" href="${resourceUrl}" type="text/css" />
Main problem was in my web.xml because I mapped spring servlet with
<url-pattern>/*.htm</url-pattern>
instead of that you must mapping only
<url-pattern>/</url-pattern>
web.xml will look:
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Thanks for all!
I ran into the same issue and this is how i resolved the issue:
In spring configuration file
Replace
<mvc:resources mapping="/resources/**" location="/resources/"/>
With
<mvc:default-servlet-handler/>
And provide the below path in the JSP to load static contents
<link rel="stylesheet" href="resources/css/Mass.css" type="text/css"/>
Hope this will help !
test-servlet.xml
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"></mvc:resources>
<link href="<c:url value="../resources/css/style.css" />" rel="stylesheet">

Web-application context/ root application context and transaction manager setup

I had two questions,
In Spring MVC application, what is the purpose of having ContextLoaderListener?
Below are the my entries in web.xml,
All MVC beans are defined in servlet-context.xml
All database and annotation based transaction management is defined in applicationContext.xml and I'm using container managed transaction in JBoss
The trasaction manager works fine if I pass the applicationContext.xml as highlighted below as to DispatcherServlet. But I thought we should only pass the Spring MVC context info to DispatcherServlet.
If I remove the applicationContext.xml the transaction manager stops working? I'm confused what is best way of managing the context files?
Web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/applicationContext.xml
/WEB-INF/config/spring-mail.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>messages</param-value>
</context-param>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/servlet-context.xml
***/WEB-INF/config/applicationContext.xml***
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
applicationContext.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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.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-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- registers all of Spring's standard post-processors for annotation-based configuration -->
<context:annotation-config />
<jee:jndi-lookup id="dataSource" jndi-name="java:OracleDS"/>
<tx:annotation-driven/>
<tx:jta-transaction-manager/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation"
value="classpath:com/common/model/config/sqlmapconfig.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
servlet-context.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"
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.0.xsd
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">
<!-- Scans the classpath of this application for #Components to deploy as beans -->
<context:component-scan base-package="com.xxxx"/>
<!-- Configures the #Controller programming model -->
<mvc:annotation-driven/>
<!-- Configures Handler Interceptors -->
<mvc:interceptors>
<!-- Changes the locale when a 'locale' request parameter is sent; e.g. /?locale=de -->
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
<!--Register Request/Response Interceptor-->
<bean class="com.xxx.common.auditor.RequestInterceptor"/>
<!-- <bean class="com.xxx.common.interceptor.UserAuthenticationInterceptor"/>-->
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/>
<!-- Application Message Bundle -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages"/>
<property name="cacheSeconds" value="0"/>
</bean>
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/xxxx.properties</value>
</property>
</bean>
Thanks for you help! I know its quote long, but wanted to make myself understood better
As explained in the documentation, every dispatcher servlet has its own application context, where you typically define controllers, view resolvers, etc., and which inherits (and can override beans) from a root application context, which typically contains data source definitions, middle tier services, etc.
The ContextLoaderListener, as its documentation explains, is used to to start up and shut down Spring's root application context (from which the servlet contexts inherit).
It's also useful when you want to use Spring for your middle tier, but you don't want to use Spring MVC as your presentation layer. In this case, you only define a root application context using ContextLoaderListener.

Resources