loading struts-config.xml from web.xml - java

When I'm writting
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/struts-config.xml</param-value>
</init-param>
This tag in web.xml that time I was getting the error
word config notspelled correctly
I know this is a logical name, so you can use any word, but that word
config is underlined in red. Because of that, whenever I run a struts application, I mention the path in a form tag of jsp: something like /book. This book path is also in
the struts-config.xml file.
So when I run this project, I'm getting the error
requested resources not available(/book)
That means struts-config.xml is not loaded correctly. I have imported jar also.

Try this
<init-param>
<param-name>config</param-name>
<param-value>classpath:struts-config.xml</param-value>
</init-param>
Copy struts-config.xml to WEB-INF/classes folder
Also you should have all struts jar files into your WEB-INF/lib

I don't think so it is necessary to move struts-config.xml file somewhere.
Actually word "config" is for initialize parameter(struts-config.xml) with name config.
config with orange color(not red)underline is only for dictionary check nothing else.
So error occurs because
In your struts-config.xml has mess or misconfigured.if possible please add you struts-config.xml with this thread so we can check what actually a reason.

Put it inside servlet action with servlet-class ActionServlet. Worked in my case, have similar problem, but I've just wanted to change folder from classes to my default.

<init-param> <param-name>StrutsConfig</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param>

Related

How to find my servlet for web.xml?

I am using wildfly, JBOSS
And I made a Servlet...called ServletImg
My folder structure looks like this:
WebContent/WEB-INF/classes/ServletImg.java
Now i am trying to add him into the web.xml file...like this:
<servlet>
<servlet-name>ServletImg</servlet-name>
<servlet-class>ServletImg</servlet-class> <!-- Here is the problem -->
</servlet>
<servlet-mapping>
<servlet-name>ServletImg</servlet-name>
<url-pattern>/ServletImg</url-pattern>
</servlet-mapping>
Error msg:
servlet.class must be valid fully qualified class name
Any suggestions on resolving the issue?
The point is, everything you need to know is in the question:
Use a fully qualified name.
To solve your problem, all you need to do is:
Create a package to put your servlet in,
Use the name my.package.ServletImg in the web.xml.
It isn't a good practice to put Java classes in the default package, and can be confusing sometimes to servers, as you just faced it
The problem is very clearly given in the error that you've mentioned.
You need to mention the Servlet class name with the full package structure.
For example, check this!
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>examples.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
Observe that the tag < servlet-class> is a fully qualified Class name, that includes the package in which the class has been declared.
Create .java classes in src folder not in WebContent/WEB-INF/classes/
and next if you are creating java dynamic web project then select Dynamic Web module version 2.5 so that you can find web.xml file in Path /ProjectName/WebContent/WEB-INF/web.xml

Spring MVC base/root URL is the same with war name

I created a basic rest service using Spring MVC with xml config. My app name is: myservice. When i create a war from it, I receive a war named myervice-1.0.0. In conclusion, I have to access my application through http://localhost:8080/myservice-1.0.0/resource. I'd like to be just 'myservice', like project name. What can I do? Thanks a lot. I'm using tomcat + gradle.
web.xml:
<servlet>
<servlet-name>webappservice</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webappservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
You can rename the war name. If you are using maven as build tool. Then you can use below solution.
<build>
<finalName>myservice</finalName>
. . .
</build>
It will better if you can share some configuration details like pom.xml,web.xml etc.
If you double click on the server definition in STS, you will see a "modules" tab. From there, you can edit the "Path" and set it to whatever you want. When you select "Run on Server", STS has to define a context path and simply defaults it to last element of the default package. If I recall correctly, by default Tomcat uses the file name name of the zipped or exploded .war. In either case, you can over-ride it.
see more about it here

Location of ApplicationContext.xml and properties file in spring

In my application I have placed the ApplicationContext xml file in src and the project is working fine.
Can we place the ApplicationContext.xml in our WebContent or Web-Inf folder?
Also I want to know if I can place my properties file in WebContent.
Since I have placed my ApplicationContext.xml in src, I placed my properties file in src and that worked fine. Below is the code for it
<bean id="licenseSettings"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:LicenseSettings.properties" />
</bean>
I tried putting the properties file in WebContent/conf but the properties were not read succesfully.
My question is, can we put ApplicationContext.xml and properties file somewhere inside WebContent folder?
Update: I put my ApplicationContext.xml in WEB-INF/classes and it was read successfully using ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
My only question remaining unanswered is, where shall I put my properties file and where is it appropriate to be put in?
There are couple of ways you can do this:
In Spring MVC, You mention dispatcher-servlet in web.xml as follows
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Now this by default will look for a file named mvc-dispatcher-servlet.xml. That is, the servlet name appended by -servlet.xml. And it will look for this file in class path.
Alternatively which fits your case, if you already have xml file and don't want to rename it, add the following entry in web.xml in addition to the servlet entry above.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/ApplicationContext.xml</param-value>
</context-param
Here you can choose any location inside WEB-INF. Put the properties file in classes folder so that it could be found in classpath. Since you are using Eclipse embedded Tomcat, put the following as your bean configuration for property placeholder.
<property name="location" value="classpath:../../conf/LicenseSettings.properties" />
Yes you can.
If you put the files directly inside WEB-INF that should work.
If you want to put them in a folder called WEB-INF/conf you would need to change the properties location to refer to conf/LicenseSettings.properties

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 template MVC project fail to run out of box, STS2.8.1 on linux 64bit

Unable to run spring template project on STS. Where to check it?
There is a spring template with STS. But, it failed on an old STS, and failed on a freshly installed STS-2.8.1, nothing changed but give a project name and top package to start the project.
No mapping found for HTTP request with URI [/you/home.htm] in DispatcherServlet with name 'appServlet'
And it seems all configured in web.xml, and servlet-context.xml :
<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>
Can we believe the contextConfigLocation setting must be ok? Why it can not get the handler?
The correct url for the Home controller is <server>/<applicationName>/ (not `.../home.html").
So for example it the Projects name is "test" and you use a Tomcat, then the urls is: http://localhost:8080/test/
And I fond a second problem, but I can not reproduce it
I have tryed the "Spring MVC Template" my own and get stucked with exaxtly the same output.
I was reading the code and configuration again and again, and did not find any mistake, because there is no.
After I modified the HomeController
public HomeController() {
logger.info("init home");
}
It starts suddenly working! -- So I think it was a Eclipse referesh Problem. (Just try to clean and republish the project)
You may try this:
First stop the server if it is running.
Expand src Tree and
Select src/main/webapp
Right click it
From Popup Menu select Build Path
Select 'Use As Source Folder' option
Now try to run the project again

Categories

Resources