Struts 2 + Spring and contextConfigLocation - spring

I am trying to integrate and configure Struts 2 + Spring and register ContextLoaderListener listener class as parameter contextConfigLocation I am trying to write SpringBeans.xml instead of the default applicationContext.xml. But the problem is that this SpringBeans.xml located right at the root src directory.. and I am not shure how to write param-value.. /src/SpringBeans.xml ...help please..

You wouldn't write src/SpringBeans.xml, because your source directory isn't a deployable artifact.
You should put the config file either:
On the classpath (IMO generally preferable)
In WEB-INF (to prevent direct client access)
If it's on the classpath, how to get it deployed depends on your build/packaging system.
For example, in Eclipse, you can just leave it at the root of src. If you're using Maven, it should go at the root of src/main/resources. If it's not at the root, modify the below accordingly.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:SpringBeans.xml</param-value>
</context-param>
Otherwise provide the app-relative path, e.g.,
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/META-INF/SpringBeans.xml</param-value>
</context-param>
(Or in WEB-INF, or wherever you're putting it.)

Try below code,
<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>Example</display-name>
<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>
/WEB-INF/classes/SpringBeans.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
if you are using placing your SpringBeans.xml in src and you are using ANt build then it will be placed to WEB-INF/classes folder. But you are placing in another folder say /config then you have write you build.xml to place *.xml files to WEB-INF/classes folder when deploying.

Related

java.io.FileNotFoundException: class path resource [conf/admin/applicationContext.xml] cannot be opened because it does not exist

I'm using Spring Security, but for some reason my web.xml isn't finding my applicationContext.xml
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/admin/applicationContext.xml
classpath:conf/admin/applicationContext-security.xml
</param-value>
</context-param>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
My applicationContext.xml is in myProject/conf/admin/applicationContext.xml, in the same place as my web.xml but it always throws the exception:
14:18:07,793 ERROR [ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [conf/admin/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [conf/admin/applicationContext.xml] cannot be opened because it does not exist
I've tried putting inside the WEB-INF folder ( like every Spring Security tutorial does ) which is in myProject/dist/web/WEB-INF, but when I clean my project to refresh and rebuild it gets deleted.
So what am I doing wrong? Putting the wrong path in contextConfigLocation or applicationContext.xml in the wrong place?
Assuming that you are following the Standard Maven Directory Structure i.e. your XML config files are under src/main/webapp/WEB-INF/conf/admin then try this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/admin/applicationContext.xml
/WEB-INF/conf/admin/applicationContext-security.xml
</param-value>
</context-param>
Another approach is to go with the defaults:
Just place you applicationContext.xml file under src/main/webapp/WEB-INF and it will be picked up by spring by default.
You can then add this <import resource="applicationContext-security.xml" /> line in your applicationContext.xml to import Security config.
Check this Repository using default config approach.

How to set Spring root context path

I am a Spring newbie and am putting together a Spring web-app (not Spring-boot - how much difference does this make?). Deployment is on a Tomcat 7 server.
The app is up and running. My problem is that is only accessible via the standard URL:
http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/index.html
The following do not work:
http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT
http://mycompany.com:8081/cwing-0.0.3-SNAPSHOT/
even though my web.xml lists index.html as the welcome page.
An additional symptom of the problem is that all sorts of links within the application need to be specified as relative, rather than with a prepended '/'.
But even these urls are not what I really want.
I would rather specify
http://mycompany.com:8081/cwing
and have it bring up the index.html.
Following the lead of Add context path to Spring Boot application
I created an application.xml file in src/main/resources with the following content:
server.contextPath=/cwing
server.port=8081
This doesn't work. http://mycompany.com:8081/cwing brings up a blank page, with a 404 error on the server, as does http://mycompany.com:8081/cwing/index.html.
I must be missing something. What else is needed to get this to work as I want it to work?
UPDATE: as asked, here is the web.xml (irrelevant details removed).
<?xml version="1.0"?>
<web-app 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"
version="3.0">
<display-name>cwing</display-name>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/context.xml
/WEB-INF/spring/datasource.xml
/WEB-INF/spring/security.xml
</param-value>
</context-param>
<servlet>
<servlet-name>d</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/context.xml
/WEB-INF/spring/datasource.xml
/WEB-INF/spring/security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>d</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- Disables Servlet Container welcome file handling. Needed for compatibility
with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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>
This is not related to Spring MVC but to specific server, by default Tomcat gets your WAR name as context root. If you want to change this, you can always rename your WAR file or I recommends to change your pom.xml to build your WAR file with final name, here is how to do this:
<build>
<finalName>finalName</finalName>
. . .
</build>

How to config path to log4j.properties file in Spring and JSF environment?

I have maven-compatible eclipse dynamic project on windows 7.I put log4j.properties under src/main/resources/welcome
In web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>`
<param-value>file/C:/projectpath/src/main/resources/welcome/log4j.properties </param-value>
</context-param>
I could not get it working when I put log4j.properties file under src directory.
What s the proper way for the config above?
Is there another way rather than this?
thanks
Copy your log4j.properties file in src.
Change web.xml:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
Your configuration is wrong, because you gave the absolute path.
It should be something like:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Please also note that above xml snippet is not necessarily required, because /WEB-INF/classes/log4j.properties should be called automatically.

Issue with name appServlet-servlet.xml

Hi guys I'm working with Spring 3.2.3, hibernate 4.2.2 and org.springframework.security 3.0.5. Before I start working with security in spring, my context file was called servlet-context.xml and everything worked fine. Since I start using org.springframework.security 3.0.5 when I try to run my application I get the error:
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/appServlet-servlet.xml]
My web.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- Spring MVC -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/servlet-context.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>
Notice that I'm specifying my servlet-context.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/servlet-context.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
If I leave my web.xml just like this and copy/paste my servlet-context.xml content in a new file called appServlet-servlet.xml, everything works fine. This is confusing for me since I told web.xml that my context file name was servlet-context.xml. Am I forced to call my context file as appServlet-servlet.xml??. Of course, if I delete servlet-context.xml and leave appServlet-servlet.xml making the specficiation in web.xml, works fine.
I just wanna know if is obligatory to call my context file as appServlet-servlet.xml if I want to use spring security in my app.
This is not strange. Spring works this way. Since your DispatchServlet name is appServlet, Spring automatically tries to find the servlet context that has the same name, here "appServlet-servlet.xml".
Here is a page that may help : http://syntx.io/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/

Jboss6.1 ClassNotFound in WEB/lib for listener

I use jboss6.1. When I try to deploy a web app, it say class not found when try to load spring.
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
the error is
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationListener
I put the spring in WEB/lib. Is that wrong? Or should be something else?
Thanks for the kind replies. What confuses me is I put the spring jars in my webapp's WEB/lib folder. Isn't that enough?
Following are the web.xml and the jars. ( I put all I grabbed in the dist folder of spring3.1)
But I wonder maybe is because I use jboss and there's something special in it? I google some post class loader but don't understand.
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:jsp="http://java.sun.com/xml/ns/javaee/jsp"
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_3_0.xsd"
id="CMDB_WS_HOST" version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>99999999</session-timeout>
</session-config>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-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>
<listener>
<listener-class>com.inspur.cmdb.system.StartupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
spring jars in web/lib
org.springframework.aop-3.1.0.M2.jar
org.springframework.asm-3.1.0.M2.jar
org.springframework.aspects-3.1.0.M2.jar
org.springframework.beans-3.1.0.M2.jar
org.springframework.context.support-3.1.0.M2.jar
org.springframework.context-3.1.0.M2.jar
org.springframework.core-3.1.0.M2.jar
org.springframework.expression-3.1.0.M2.jar
org.springframework.instrument.tomcat-3.1.0.M2.jar
org.springframework.instrument-3.1.0.M2.jar
org.springframework.jdbc-3.1.0.M2.jar
org.springframework.jms-3.1.0.M2.jar
org.springframework.orm-3.1.0.M2.jar
org.springframework.oxm-3.1.0.M2.jar
org.springframework.test-3.1.0.M2.jar
org.springframework.transaction-3.1.0.M2.jar
org.springframework.web.portlet-3.1.0.M2.jar
org.springframework.web.servlet-3.1.0.M2.jar
org.springframework.web.struts-3.1.0.M2.jar
org.springframework.web-3.1.0.M2.jar
Make sure your server classpath has included the Spring jar library. In Spring 3 its, spring-web.jar
check whether you have these jars in WEB/lib
commons-logging-1.1.1.jar
jstl-1.2.jar
spring-asm-3.0.3.RELEASE.jar
spring-beans-3.0.3.RELEASE.jar
spring-context-3.0.3.RELEASE.jar
spring-core-3.0.3.RELEASE.jar
spring-expression-3.0.3.RELEASE.jar
spring-web-3.0.3.RELEASE.jar
spring-webmvc-3.0.3.RELEASE.jar
It is ok with JBoss 7.It is classloading effect.
Config : /WEB-INF/jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC
"-//JBoss//DTD Web Application 4.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">
<jboss-web>
<class-loading java2ClassLoadingCompliance="false">
<loader-repository>
myapp:loader=anyUniqueName
<loader-repository-config>java2ParentDelegation=false</loader-repository-config>
</loader-repository>
</class-loading>
</jboss-web>
Otherwise
I reference here.
I have not tried with JBoss 6 yet. But, I hope, the following configuration will be helpful for JBoss 6.
NOTE:
On JBoss 6.0, to avoid the container loading the classes before the application actually starts, one needs to add a WEB-INF/jboss-scanning.xml file to the application archive - with the following content:
Config : /WEB-INF/jboss-scanning.xml
<scanning xmlns="urn:jboss:scanning:1.0"/>

Resources