java.io.FileNotFoundException:for DispatcherServlet in WebApp - java

I have a program I'm running in a Spring project which always fails because of a java.io.FileNotFoundException, when it comes to locating the DispatcherServlet.
The DispatcherServlet lives in the \WEB-INF folder and is accessible to the rest of the project without incident.
So at the moment I'm forced to hardcode the path to the DispatcherServlet as follows:
File config = new File("C:\\project\\build\\web\\WEB-INF\\project-servlet.xml");
boolean exists = Misc.checkFileExists(config.getAbsolutePath());
if (exists) {
System.out.println("File: " + config.getAbsolutePath() + " found.");
}
ConfigurableApplicationContext context = new FileSystemXmlApplicationContext(config.getAbsolutePath());`
Which is not the best way at all.
But if I try to place the DispatcherServlet in a folder under \WEB-INF, e.g. \WEB-INF\resources to satisfy the CLASSPATH, the file is still not found. Because of this I can't use ClassPathXmlApplicationContext.
I have resolved this by setting my web.xml file as follows:
<servlet>
<servlet-name>project</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/project-servlet.xml</param-value>
</init-param>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/project-servlet.xml</param-value>
</context-param>
The application works as does the test program with:
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("project-servlet.xml");
I should add that I have a single project-servlet.xml file which configures everything.

The code makes no sense. ClassPathXmlApplicationContext, as its name indicates, is used to load an XML file from a classpath resource. But you're passing it the path of a file. Read the javadoc:
Standalone XML application context, taking the context definition files from the class path, interpreting plain paths as class path resource names that include the package path (e.g. "mypackage/myresource.txt")
What constitutes the classpath of a web application is the directory WEB-INF/classes, and all the jar files inside WEB-INF/lib. So, your XML file is not in the CLASSPATH, and can thus not be loaded using a ClassPathXmlApplicationContext.
If you want to load a context file from WEB-INF, use an XmlWebApplicationContext.
You should not have to do that manually anyway, since a Spring web application is typically configured using the web.xml file, or using JavaConfig, as explained in the documentation.

Related

How to specify a file path in web.xml file

i am developing an application in java and i am new to this java ee platform.I have a dataset file called "51Degrees-EnterpriseV3_2.dat" in my resource folder. how do i specify the path of this in my web.xml file and how do i retrieve the file in code?
In web.xml , you can add
<context-param>
<param-name>you can mention here localtion variable name</param-name>
<param-value>location here </param-value>
</context-param>
The other answer shows the correct way to add the parameter to your web.xml. Which for the 51Degrees V3.2 data file would look like this:
<context-param>
<description>The name of the device database.</description>
<param-name>BINARY_FILE_PATH</param-name>
<param-value>51Degrees-EnterpriseV3_2.dat</param-value>
</context-param>
As far as I know, there is not a way to get a resource which is located in the resources path (it's an XML file so the .getResource(name) method cannot be used). However, as you have a web.xml file, I assume this is a web project (i.e. .war rather than .jar)? If this is the case, then the WEB-INF directory is what you need. Files in here are also packaged up like resources, but can be more easily consumed by a .war package.
If you put your data file in src/main/webapp/WEB-INF/, which is used as the root by the web project. So the above XML example will work if the path to your data file is:
src/main/webapp/WEB-INF/51Degrees-EnterpriseV3_2.dat
As a sidenote, there is more documentation on configuring 51Degrees in a Java web app here

Load log4j2.xml file outside project with enviroment variable

I am developing a java web-app (v3.0) for a TomCat 7.0 server and I am having troubles loading the log4j2.xml file.
I have defined the log4j2.xml file outside my project and defined the path for the file in my web.xml file.
If i hardcode the path my log4j2.xml file loads as it should.
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///C:/my/path/log4j2.xml</param-value>
</context-param>
On the other hand I want to use an enviroment variable to define the path.
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///${ENVIROMENT_VARIABLE}/log4j2.xml</param-value>
</context-param>
When I start TomCat I have this error:
ERROR SatusLogger Unable to access file:///$%7BENVIROMENT_VARIABLE%7D/log4j2.xml
It looks like it isn't 'translating' the variable.
Any help will be very aprecciated.
PD: Sorry for my english.
Log4j interpolates the value it finds for log4jConfiguration in web.xml. However, you have to use standard Log4j Lookup syntax. To get an environment variable you would specify:
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///${env:ENVIROMENT_VARIABLE}/log4j2.xml</param-value>
</context-param>

applicationContext.xml is being copied to /WEB-INF/classes from src/main/resources

however tomcat is throwing the error :
IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml];
Which suggests that tomcat expects the applicationContext.xml to be one directory above where it is copied to. It is being copied to the /WEB-INF/classes directory, not just the plain old /WEB-INF
I am using maven.
If you have put it under src/main/resources, you should refer to the resource as classpath:/applicationContext.xml
In your web.xml you can state where your applicationContext.xml is located. If it is in /WEB-INF/classes then you must state /WEB-INF/classes/applicationContext.xml (and not just /WEB-INF/applicationContext.xml).
Try this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
To include more than one context files you can use the import function. To do so, write into your applicationContext.xml one line per file like this:
<import resource="myOtherSpringContext.xml"/>
Use the following syntax in your web.xml ( http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/context/ContextLoader.html ):
<!-- list of the configuration files to load -->
<context-param>
<!-- Spring -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
It will load it from the classes/* directory.
BTW, if you want to copy the applicationContext.xml directly in your WEB-INF directory, move it in /src/main/webapp
BTW, here is the way recommanded by the spring documentation: http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#context-create
HIH
This is standard Maven behaviour, and is what a lot of people use with no objection. Why do you prefer it in WEB-INF?
Anyway, if you want it there, you can just put it in src/main/webapp/WEB-INF.
the standard way is to create new source dir src/main/webapp. All files from this dir will be places in the same folder as WEB-INF in resulting war. So, inside this new source dir you can create WEB-INF and place you applicationcontext.xml in it.
so you structure would look much like
+src
+---main
+---+---java
+---+---webapp
+---+------WEB-INF
check maven-war-plugin documentation

Spring - Use/Load context file from jar file

I would like to load a context file from a jar file. For example, I want to create a data access jar file - holds entity and dao objects and it would have a spring context file: dataaccess-context.xml.
Now in my Web application I want to be able to access this jar using the context file.
I have placed the following in my web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/vsg/dataccess/framework/context/dataaccess-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
When I do this I get a file not found exception. The jar file is placed into the WEB-INF/lib directory.
Now running from with in Eclipse using JBoss I placed the spring jar files into the class path this did not solve the issue.
Any direction, I have read several links and so forth here and google and it all seems to be possible so not sure what I am doing incorrect here.
It is possible and it should be working for you.
Maybe is just some error in your configuration file. Try this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/com/vsg/dataccess/framework/context/dataaccess-context.xml
</param-value>
</context-param>
See that the path starts with a slash.

spring beans configuration

I'm using Spring's dependency injection but I'm running into difficulty loading a resource in my Spring config file.
The resource is an XML file and is in a JAR file on my classpath. I try to access it as follows:
<import resource="classpath:com/config/resources.xml" />
however I keep getting encountering the following error:
Failed to import bean definitions from URL location [classpath:com/config/resources.xml]
The JAR file is on the classpath of a Java project, which is in turn used by my web app. Should I really be doing my Spring configuration in the web project as opposed the Java project, or does that matter?
If it needs to be in the classpath of your webapp, then you should stick the JAR containing the config file into your WEB-INF/lib directory.
If you're using a webapp, then the common convention is use a ContextLoaderListener to ensure a WebApplicationContext is inserted into a standard place in the ServletContext:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/com/config/resources.xml</param-value>
</context-param>
Then use WebApplicationContextUtils to fish the application context out of the servlet context using:
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
I ran into a similar issue with a red5 plugin. I resolved it like so:
try {
subContext = new FileSystemXmlApplicationContext(new String[] { "classpath*:/myconfig.xml" }, true, context);
} catch (Exception fnfe) {
subContext = new FileSystemXmlApplicationContext(new String[] { "plugins/myconfig.xml" }, true, context);
}
This will look anywhere on the classpath first, including within the jar that contains my code. If an exception occurs the plugin directory is checked. It may not be the best solution but it works.
I don't really recall why this matters, but try putting an asterisk () in front of the colon (:) classpath:/ If this doesn't work, try the asterisk after the colon (classpath:*), although I think it was before the colon.
I've only used the <import> directive in J2SE and it works without the classpath: prefix, simply as <import resource="config/resources.xml" />. But in J2EE if all your files are inside WEB-INF, it should be similar, just import resource="bla.xml" and it should find it, although in J2EE you don't need to do this because in web.xml you can define several files in the contextConfigLocation context parameter inside web.xml, just separate them with spaces or newlines.

Categories

Resources