spring mvc static resources failed to load - spring

I am trying to use static resources in my sample spring mvc project, In my project I have created a theme under resources folder as shown in the picture.
Then I have added the following 3 lines of code in spring-mvc-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: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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.sudheer.example" />
<context:annotation-config />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/theme/" cache-period="31556926"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
</beans>
WhenI try to access the static resources in many differnt ways,I am not able to access it.
This is my jsp page:
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html lang="en">
<head>
<link href="/resources/themes/css/bootstrap.min.css" rel="stylesheet" >
<link rel="stylesheet" type="text/css"
href="${pageContext.request.contextPath}/themes/bootstrap.min.css">
<link href="<c:url value="/resources/themes/css/bootstrap.min.css"/>"
rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>1. Test CSS</h1>
<h2>2. Test JS</h2>
<div class="btn"></div>
</body>
</html>
And this is my web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Can any one help me to solve this issue?

You have 3 problems:
1: In your Maven project, the folder src/main/resources has a special meaning for Maven: its content will be put in the classpath after compiling.
2: when you use spring resources mapping then the part of the url that mapping that match the mapping part '**' will been added to the location part for obtaining the file. (sorry for the bad explanation) An example will make it more clear: assume you have this resource mapping
<mvc:resources mapping="/a/**" location="/b/c/" >
And you request the url http://localhost/myApp/a/d then spring will look for the file at /b/c/d! - So when you send an request for ....../resources/themes/css/bootstrap.min.css Spring would search for: /resources/theme/themes/css/bootstrap.min.css
3: in spring config and folder you use theme but in the jsp you use themes
Solution for 1 would be: either you change the location spring resource mapping to pick up the resource from classpath by using classpath: prefix in location attribute or you put your files in an other folder: source/main/webapp/resources/theme (then the location parameter /resources/theme/) would map.
example with classpath fix for 1 and one possible fix for 2:
<mvc:resources mapping="/resources/theme/**"
location="classpath:/resources/theme/" cache-period="31556926"/>
jsp - fix for 3:
<link href="<c:url value="/resources/theme/css/bootstrap.min.css"/>"
rel="stylesheet" type="text/css"/>

Probably is this:
You have this configuration
<mvc:resources mapping="/resources/**" location="/resources/theme/"
But when you call the resourcek, you use this
<c:url value="/resources/themes/css/bootstrap.min.css"/
i htink you have to use this
/resources/css/bootstrap.min.css

If a resource need visible by a frontend, you need put the resources in "webapp" folder. Example: webapp/theme/css/bootstrap.min.css

I have create a skeleton project for Spring MVC (static resources are used also) here.

Related

SpringMVC w/ Maven - not able to get Servlet to find second page

I am trying to build a demonstrator example for a Spring MVC project that uses DispatcherServlet, following a precedent: https://www.youtube.com/watch?v=g2b-NbR48Jo&t=551s
It has been straightforward, up until I tried to add a second JSP file to the project and the program is not able to access it after initiating the main page successfully. Briefly, this project takes two numbers in the "index.jsp" file and then returns a plain text message in the "display.jsp" file, in advance of building an addition ability into the Controller...
I am also using Apache 10.0.2 for this and Spring Tools Suite 4.10.0.RELEASE with Eclipse.
This is the code for making the above image in the index.jsp file:
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
The display.jsp file simply is this, generated by Eclipse, and this is the file my program can't seem to find:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3c//DD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html14/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
I'm here
</body>
</html>
The web.xml file that initiates the DispatcherServlet and sets the URL pattern is also standard issue:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>telusko</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The telusko-servlet.xml file has the expected content to facilitate annotation use in the controller:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.telusko"></ctx:component-scan>
</beans>
The AddController.java file:
package com.telusko;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//will be responsible for adding two numbers
#Controller
public class AddController {
#RequestMapping("/add")
public String add()
{
return "display.jsp";
}
}
POM file dependencies, but i don't think these connect to the issue:
Last thing, project structure:
The error I get after clicking the "Submit" button on the first page is this:
It is generally clear that Apache cannot find the page, but I cannot understand fully why. I've tried a number of resolutions to resolve the problem:
Eclipse is using the Tomcat installation in the server settings
The location is also set to Servers/Tomcat v10.0 at localhost.server
Tried changing the URL pattern to "/", "/Dispatcher/", to no avail, and left it back with just the forward slash
Deleted the "target" folder in the Maven file and let it recreate
Corrected header references in the XML file (i.e. adding a missed "/schema/")
Updated/cleaned Maven project and server, separately. It leads to a separate error :
And this error is solved with adding <load-on-startup>1</load-on-startup> under the set of tags, at which point it reverts to the original 404 error.
I am using later version of JAR files, but I'm not sure if it might be a compatibility issue with STS4 and the version in the project.
Running the "display.jsp" file on its own does make it show with the "I'm here" message.
Targeted Runtimes for the project is set to Apache Tomcat v10.0.
I am exploring other possible angles on this problem, but it has proven to be persistent. Thank you in advance for your insight!

spring mvc - how to remove the word 'resources' when calling js,css,images

I have a resources folder in my WebContent folder that contains css, js, and images... Then i declare mvc resource mapping to locate all the resources file... but when i tried to call/acccess the file in my jsp, why is that i need to include the word resources?... do you know how can i remove it but still able to access my resources?
project structure
WebContent
|_resources
|_images
|_js
|_WEB-INF
|_jsp
|_header.jsp
header.jsp
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src="<c:url value="/resources/js/jquery.min.js" />"></script>
dispatcher.xml
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
above code works perfectly, but i want to exclude the word resources in path..
without writing one by one all the inside resources folder (like sample below)
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:resources mapping="/css/**" location="/resources/css/" />
what i need is something like this... but still declaring one line mvc resource mapping
<script src="<c:url value="/js/jquery.min.js" />"></script>
<link rel="stylesheet" href="<c:url value="/css/owl.carousel.min.css" />">
<img src="<c:url value="/images/pix/world.jpg" />" alt="user">
Do you use the server is tomcat? If you use the server is tomcat, you can do the following configuration
<Url-pattern> / </ url-pattern> means to intercept all requests.
If you need to deal with static resources js, css, etc., you can add in web.xml written before dispatcherServlet
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
Default is Tomcat's default servlet!

Simple Spring Web Project Not Working

I've been trying to learn how to develop a Spring Web application and can't seem to get it working. Using the Spring Tool Suite, I created a new Spring Project using the Simple Spring Web Maven template, which basically is comprised of four files:
index.jsp:
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<c:url value="/showMessage.html" var="messageUrl" />
Click to enter
</body>
</html>
/WEB-INF/web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>spring-web-test</display-name>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<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/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
/WEB-INF/mvc-config.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- Uncomment and your base-package here:
<context:component-scan
base-package="org.springframework.samples.web"/> -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
/WEB-INF/view/showMessage.jsp:
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
This all seemed simple and basic enough, so I built it using Maven and packaged it into a WAR file. I then deployed it to a local TomEE 7 server, went to http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT, and got to the very simple index.jsp page:
However, when clicking the link, which sends me to http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage.html, I get a 404 error from Tomcat:
I suspected it was because of the .html ending - based off what I understand of the dispatcher and view resolver, it would be looking for /WEB-INF/view/showMessage.html.jsp. However, trying to access http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage returns the same error. Looking at the Tomcat logs, I see
Nov 11, 2015 9:46:53 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage.html] in DispatcherServlet with name 'dispatcherServlet'
Nov 11, 2015 9:46:55 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage] in DispatcherServlet with name 'dispatcherServlet'
What am I doing wrong? The whole thing seems pretty simple, and I haven't even modified anything from the template Spring Tool Suite provides, so I'd have assumed it would work right out of the box. Unfortunately there doesn't seem to be a whole lot of resources online on developing Spring web applications for use in servlet containers, and of the ones I've found, nothing seemed to suggest I'm doing anything wrong.
Thanks.
Change your index.jsp to something like this
<c:url value="/showMessage" var="messageUrl" />
You don't need file extensions, because you've set them up in your mvc-config.xml.
Then you need a basic controller to handle the mapping. Something like this:
#Controller
public class WebController{
#RequestMapping(value = "/")
public String showMessage(){
return "index";
}
#RequestMapping(value = "/showMessage")
public String showMessage(){
return "showMessage";
}
}

Spring MVC not initial request mapping

Does the web deploy mechanism affect Spring MVC work?
I create a web project(SpringMVCTest) contains web.xml and a pjsp pages then I create a Java project(SpringMVCTestSrc) and use the link source function to link test_src and SpringMVCTestSrc.
My Eclipse workspace
My problem is if I deploy the project in war file then it works fine
, EX: if I open
localhost:8080/SpringMVCTest/message/showMessage.pages
it will forward to
main/showMessage.jsp
but if I deploy the project with separate web and jar file it will no request mapping found like below.
[DEBUG]-[2015/07/30 15:03:39,o.s.w.s.DispatcherServlet(init):139]: Servlet 'dispatcher' configured successfully
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.DispatcherServlet(doService):861]: DispatcherServlet with name 'dispatcher' processing GET request for [/SpringMVCTest/message/showMessage.pages]
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.m.m.a.RequestMappingHandlerMapping(getHandlerInternal):294]: Looking up handler method for path /message/showMessage.pages
[DEBUG]-[2015/07/30 15:03:40,o.s.w.s.m.m.a.RequestMappingHandlerMapping(getHandlerInternal):302]: Did not find handler method for [/message/showMessage.pages]
[WARN]-[2015/07/30 15:03:40,o.s.w.s.PageNotFound(noHandlerFound):1136]: No mapping found for HTTP request with URI [/SpringMVCTest/message/showMessage.pages] in DispatcherServlet with name 'dispatcher'
The log shows the different.
If I deploy in war file. The log shows found 18 bean definitions and request mapping works fine.
Export war file from Eclipse and put it to apache-tomcat-8.0.21\webapps
Startup Tomcat and open localhost:8080/SpringMVCTest/message/showMessage.pages
The page will show Message : Hello world
o.s.c.a.ClassPathBeanDefinitionScanner(registerDefaultFilters):244]: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
o.s.c.i.s.PathMatchingResourcePatternResolver(doFindMatchingFileSystemResources):631]: Looking for matching resources in directory tree [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc]
o.s.c.i.s.PathMatchingResourcePatternResolver(doRetrieveMatchingFiles):693]: Searching directory [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc] for files matching pattern [D:/Temp/apache-tomcat-8.0.21/webapps/SpringMVCTest/WEB-INF/classes/in/hotkey/mvc/**/*.class]
o.s.c.i.s.PathMatchingResourcePatternResolver(findPathMatchingResources):424]: Resolved location pattern [classpath*:in/hotkey/mvc/**/*.class] to resources [file [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc\MainForm.class]]
o.s.c.a.ClassPathBeanDefinitionScanner(findCandidateComponents):286]: Identified candidate component class: file [D:\Temp\apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\classes\in\hotkey\mvc\MainForm.class]
o.s.b.f.x.XmlBeanDefinitionReader(loadBeanDefinitions):224]: Loaded 18 bean definitions from location pattern [classpath:test-dispatcher-context.xml]
If I deploy with separate web and jar file. The log shows found 17 bean definitions and request mapping does not work.
Build a directory in apache-tomcat-8.0.21\webapps, EX: apache-tomcat-8.0.21\webapps\SpringMVCTest
Copy WEB-INF, META-INF from Eclipse workspace to apache-tomcat-8.0.21\webapps\SpringMVCTest
Export jar file from SpringMVCTestSrc and put it to apache-tomcat-8.0.21\webapps\SpringMVCTest\WEB-INF\lib
Startup Tomcat and open localhost:8080/SpringMVCTest/message/showMessage.pages
The page shows HTTP 404
o.s.c.a.ClassPathBeanDefinitionScanner(registerDefaultFilters):244]: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
o.s.c.i.s.PathMatchingResourcePatternResolver(findPathMatchingResources):424]: Resolved location pattern [classpath*:in/hotkey/mvc/**/*.class] to resources []
o.s.b.f.x.XmlBeanDefinitionReader(loadBeanDefinitions):224]: Loaded 17 bean definitions from location pattern [classpath:test-dispatcher-context.xml]
Does someone know why the same code but different deploy mechanism has different result?
My code as blow
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>SpringMVCTest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:test-service-context.xml</param-value>
</context-param>
<filter>
<filter-name>SetCharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:test-dispatcher-context.xml</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.pages</url-pattern>
</servlet-mapping>
</web-app>
Spring Configuration(test-service-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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="testMessageService" class="in.hotkey.service.TestMessageService" >
</bean>
</beans>
Spring MVC Config(test-dispatcher-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/beans http://www.springframework.org/schema/beans/spring-beans-4.0.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">
<mvc:annotation-driven />
<context:component-scan base-package="in.hotkey.mvc"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
</beans>
Main Controller
package in.hotkey.mvc;
import in.hotkey.service.TestMessageService;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class MainForm {
#RequestMapping(value="/message/showMessage.pages")
public ModelAndView handleShowMessage() throws Exception {
ModelAndView mv = new ModelAndView("main/showMessage");
mv.addObject("message", "Hello world!");
return mv;
}
}
JSP Content
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Test</title>
</head>
<body>
<h4>Message : ${message}</h4>
</body>
</html>
I found what the problem is....
Root cause founded from Spring MVC document.
It means annotation scan need directory entries!!
The problem is if I export whole project into war file. It contains the complete directory entries in WEB-INF/classes directory but jar file has no directory entry information.
Resolve this problem just check the "Add directory entries" option in Eclipse export function.

Picking up Tomcat's Context.xml parameters via SpEL

Deploying war to Apache Tomcat 8 the following way.
Placing myApp.xml under $CATALINA_HOME/conf/[enginename]/[hostname]/
with the following contents:
<Context>
<Parameter name="myApp_configs" value="file:/the/path/to/configs/folder"
type="java.lang.String" override="false"/>
</Context>
B.t.w. I do not place any kind of Context.xml into war.
Then copying myApp.war to $CATALINA_HOME/webapps
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>service</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And this way I try loading properties-file in beans.xml (referenced in web.xml above).
<?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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
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://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
<!-- Imported resources for cxf -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!--context:property-placeholder
location="#{contextParameters['myApp_configs']}/myApp.properties"/-->
<bean id="configurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="#{contextParameters['myApp_configs']}/myApp.properties"/>
</bean>
...other lines follow here...
</beans>
However I am getting following error upon beans loading:
Field or property 'contextParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
Could you help me understand the error and propose a fix so that I can access Context-defined parameters?
P.S. I have not put here, but I also have some <Environment>-nodes in Context, and they are successfully accessible via JNDI in other places.
So as we were not able to solve the root cause - why contextParameters bean is not available, the following workaround (using ol' syntax) took place:
<bean id="myConfigsLocation"
class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
<property name="initParamName" value="myApp_configs" />
</bean>
<context:property-placeholder
location="#{myConfigsLocation}/myApp.properties" />
And it worked successfully.

Resources