What is correct path - java

What is correct path to my hhconfig.xml from web xml?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/hhconfig.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>hhconfig</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hhconfig</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is my screen from project tree
http://zapodaj.net/22e3adc3a776f.png.html

Your hhconfig.xml must be located directly in src/main/resources - currently it is under src/main/resources/META-INF. All files from src/main/resources will be added to <war>/WEB-INF/classes, thus they will be available on your web app's classpath.
You also need to change classpath:/hhconfig.xml to classpath:hhconfig.xml. Check Spring documentation for examples of resource strings.

Related

web.xml can't find dispatcherServlet's servletMapping

Building a Spring Web Maven Project in STS; I can't seem to get the web.xml editor to find the servletMapping for the dispatcherServlet, despite it being right there. It keeps telling me that the Servlet Name - "The value is not among the possible selections." The dropdown list flutters like it's looking, but there are no options. My web.xml:
<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>
And here's the error I'm getting:

How to merge two web projects?

I have 2 different war project. And I would like merge this project to one war. Each is Spring project.
Spring project have java based configuration. Each project have similar controllers and bean.
I would like make sturcture like this:
localhost:9080/common/project1
localhost:9080/common/project2
How web.xml should be?
my web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>bla.bla.web.WebConfig</param-value>
</context-param>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/project1/*</url-pattern>
</servlet-mapping>
As Cuzz stated, just simply deploy both wars in tomcat. Each of them will work independently since they have different servlet-mappings.
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/project1/*</url-pattern>
</servlet-mapping>
and for second:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/project2/*</url-pattern>
</servlet-mapping>
But i would recommend you to move contents of both together. Then for instance if your servlet-mapping is:
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/common/*</url-pattern>
</servlet-mapping>
In your controllers you will have:
#Controller
#RequestMapping(value = "/project1")
and for second:
#Controller
#RequestMapping(value = "/project2")

Error due to listener in web.xml

when I am removing listener part from web.xml my project is running fine. Please let me know why?
<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_2_5.xsd"
version="2.5">
<display-name>migration</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
This is a spring web application in eclipse.
ContextLoaderListener is optional. If all you're beans are in the (child) web context (DispatcherServlet) you can remove it.
How it works basically, Spring load a root WebApplicationContext via Spring’s ContextLoaderListener and a child WebApplicationContext via Spring’s DispatcherServlet. This results in a parent-child context hierarchy.
If you don't need anything in the root hierarchie then just keep the DispatcherServlet.
Further reading :
Role/Purpose of ContextLoaderListener in Spring?
ContextLoaderListener or not?
and of course http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/
Only if you have two config xml files. Say, SerivesContext.xml, ContextDAO.xml
If you have configured everything in one spring config file you don't need the
ContextLoaderListener, just the dispatcher servlet is sufficient.
If you use different ones , you can configure it as ,
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,WEB-INF/spring-security.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
In Spring Web Applications, there are two types of container, each of which is configured and initialized differently. One is the Application Context and the other is the WebApplicationContext.
Purpose of ContextLoaderListener :
ContextLoaderListener is used to initialize Application Context. In this case, you will tell Spring to load all *-.context.xml files from the classpath. There is only one application context per application.
So you can configure this in web.xml as
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*-context.xml</param-value>
</context-param>
Purpose of contextConfigLocation :
On the other hand, you have WebApplicationContext. WebApplicationContext is the child context of the Application Context. Each DispatcherServlet defined in a Spring web application will have an associated WebApplicationContext. You can initialize the WebApplicationContext as
<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:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
NOTE : Here name of the XML must be like <servlet name>-servlet.xml. In our case name of servlet is dispatcher therefore, name of XML file is dispatcher-servlet.xml.
I hope this clears you when to use what & why.

Spring import runs hibernate persistence twice

I have 2 spring configurations :
spring-servlet.xml
spring-security.xml
needed to add this line to security:
<beans:import resource="spring-servlet.xml"/>
Now hibernate is ran twice, this is log screenshot :
my web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</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/spring-security.xml
</param-value>
</context-param>
remove <beans:import resource="spring-servlet.xml"/>,
and set
<param-value>
/WEB-INF/spring-security.xml;/WEB-INF/spring-servlet.xml
</param-value>
and maybe you twice define sessionFactory bean. Remove one of them.
EDIT:
Ok, two context is normal. One - application context, loaded by ContextLoaderListener, should contains definition of sessionFactory, dao, services, etc. usually its name is applicationContext.xml
DispatcherServlet should contains only beans for MVC. You can define conext name in parameter:
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servlet.xml</param-value>
</init-param>

Adding ContextLoaderListener to web.xml in Spring MVC

I am new to Spring MVC. I have a web application. I have the following configuration:
<welcome-file-list>
<welcome-file>list.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Do I need to add the following line to the web.xml file?
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Yes you need to add ContextLoaderListener in web.xml,
only if you want to load other Spring context xml files as well while loading the app
and you can specify them as
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
Only if you have two config xml files. One with Services / DAOs and another with Controller. If you have configured everything in one spring config file you don't need the ContextLoaderListener, just the dispatcher servlet is sufficient.
It is recommended to split the config into two and use the ContextLoaderListener to create the root application context and the dispatcher servlet to create the web layer application context.
It is optional, you don't really need it just for Spring MVC (DispatcherServlet will do). But adding Spring security to your Spring MVC must be done with
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Just one remark, if using ContextLoaderListener you will have to add DelegatingFilterProxy:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>/admin</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
in your web.xml as well. Sorry for being four years too late. Cheers
This maybe a little advanced, in my application which is an enterprise app, they construct their own listener class and put in the web.xml. On start up, this customized listener will scan the app to gather all info including resources, external connections, server info ip, jars, etc. The info is accessible in a web page.

Categories