How to merge two web projects? - java

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")

Related

Liferay and Jersey The ResourceConfig instance does not contain any root resource classes

I am trying to add rest api within my application so i added two servlet one for handling JSF request another for Rest API request.
web.xml
<?xml version="1.0"?>
<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"
>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>-1</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>redmond</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.infinite.npsc.webapi</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.infinite.npsc.webapi.CorsFilter</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
so, for example i want to call api as
http://localhost:5081/ApplicationFormFillingProcess-portlet/api/savePayment/2075-531345
but now i get an error of com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
if i changed jersey servlet url mapping from
<url-pattern>/api/*</url-pattern>
to
<url-pattern>/*</url-pattern>
i can call the api. but my images and other static contents are not loaded.

How does Java servlet handle a URL that ends with '/'?

Usually, these two requests:
localhost:8080/test
localhost:8080/test/
have no difference.
However, when I add a servlet-mapping config:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
then
localhost:8080/test/
Does not work, it would return 404 error, I don't understand that I had added a config like this:
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/rest-servlet.xml,
/WEB-INF/interceptor-servlet.xml,
/WEB-INF/controller-servlet.xml,
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
So, why would the request not go to this servlet, what is the magic in *.html? The others like *.txt, *.jpeg would not cause this problem.
remove
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
leave
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and let Spring distinguish between rest and html requests.

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:

What is correct path

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.

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>

Categories