Spring import runs hibernate persistence twice - java

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>

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

MyFaces app doesn't work on Weblogic 12c

I've got an application which is working perfectly fine on Weblogic 11. But when trying to run it on weblogic 12c, I get this exception:
...
Caused by: java.lang.IllegalStateException: No Factories configured for this Application. This happens if the faces-initialization does not work at all - make sure that you properly include all configuration settings necessary for a basic faces application and that all the necessary libs are included. Also check the logging output of your web application and your container for any exceptions!
If you did that and find nothing, the mistake might be due to the fact that you use some special web-containers which do not support registering context-listeners via TLD files and a context listener is not setup in your web.xml.
A typical config looks like this;
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
at javax.faces.FactoryFinder._getFactory(FactoryFinder.java:286)
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:206)
...
I've got this listener in my web.xml. I've gone through the first two pages on google search on this topic, but didn't find any solution.
I use:
Apache MyFaces 2.1.10
Servlet API 2.5
Spring 3.2.4
Requested: web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!--
- ####################
- Spring configuration
- ####################
//-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!--
- Declaration of the SPRING MVC dispatcher servlet.
//-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--load-on-startup>1</load-on-startup-->
</servlet>
<servlet>
<servlet-name>pibEmailServlet</servlet-name>
<servlet-class>com.teamead.fwf.web.PibEmailServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>overviewForward</servlet-name>
<servlet-class>com.teamead.fwf.web.OverviewForwardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>overviewForward</servlet-name>
<url-pattern>/showMenu/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>pibEmailServlet</servlet-name>
<url-pattern>/sendPibEmail</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>aipUpdateServlet</servlet-name>
<servlet-class>com.teamead.fwf.web.AipUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>aipUpdateServlet</servlet-name>
<url-pattern>/aip/update</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>aipUpdateServlet</servlet-name>
<url-pattern>/aip/reset</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>validationServlet</servlet-name>
<servlet-class>com.teamead.fwf.web.servlet.ValidationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validationServlet</servlet-name>
<url-pattern>/validationServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/pib.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/pib.pdf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/pib.xml</url-pattern>
</servlet-mapping>
<!--
- Specifies the context location for the root application context of this web app.
- The value mentioned here is the default of the ContextLoaderListener.
//-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/properties-config.xml
/WEB-INF/spring/menu/menu-config.xml
/WEB-INF/spring/captcha-service.xml
/WEB-INF/spring/custom-message-service.xml
/WEB-INF/spring/data-access.xml
/WEB-INF/spring/data-source.xml
/WEB-INF/spring/service-definition.xml
/WEB-INF/spring/domain-service.xml
/WEB-INF/spring/spring-security-config.xml
/WEB-INF/spring/cms-config.xml
/WEB-INF/spring/predefined-pib.xml
/WEB-INF/spring/fwf-services-manager.xml
</param-value>
</context-param>
<!-- Theme settings for FLAVOUR -->
<!--context-param>
<param-name>primefaces.THEME</param-name>
<param-value>#{siteService.siteTag}-THEME</param-value>
</context-param-->
<!--If this is not set, the XML comments in pages are rendered as components(i.e. in panelGrid)-->
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<!--<listener>
<listener-class>com.bea.p13n.http.SessionMonitor</listener-class>
</listener>-->
<!--
- Listener, to allow Jetty serving MyFaces apps.
//-->
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<!--
- Loads the specified root application context of this web app at startup,
- by default from "/WEB-INF/services.xml".
//-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- ##################################
- Tomahawk MyFaces JSF configuration
- ##################################
//-->
<context-param>
<param-name>org.apache.myfaces.JSF_JS_MODE</param-name>
<param-value>minimal-modern</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.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.COMPRESS_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION</param-name>
<param-value>30</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jspx</param-value>
</context-param>
<context-param>
<param-name>facelets.VIEW_MAPPINGS</param-name>
<param-value>/public/*;/restricted/*;/ajaxlogin/*;/mobile/*</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.CHECK_ID_PRODUCTION_MODE</param-name>
<param-value>false</param-value>
</context-param>
<!-- ONLY FOR WEBLOGIC DEPLOYMENT -->
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>
<context-param>
<param-name>primefaces.PRIVATE_CAPTCHA_KEY</param-name>
<param-value>6Ldx8eYSAAAAAI7A7Ln5GjOvQ88YD5HwwXV-KyUR</param-value>
</context-param>
<context-param>
<param-name>primefaces.PUBLIC_CAPTCHA_KEY</param-name>
<param-value>6Ldx8eYSAAAAAIyXv7tZAlOm5IGPYkIO1wLKHKMK</param-value>
</context-param>
<context-param>
<description>
This parameter tells MyFaces if javascript code should be allowed in the
rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default: "true"
</description>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.CHECK_EXTENSIONS_FILTER</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
If true, rendered HTML code will be formatted, so that it is "human readable".
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default: "true"
</description>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>
/WEB-INF/taglib/messages.taglib.xml;/WEB-INF/taglib/springsecurity.taglib.xml
</param-value>
</context-param>
<!-- if you want to disable the behaviour completely -->
<context-param>
<param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
<param-value>false</param-value>
</context-param>
<!-- if you are using myfaces + facelets don't forget to do this -->
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>false</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>
<servlet-name>Redirect Servlet</servlet-name>
<servlet-class>com.teamead.fwf.web.RedirectServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Redirect Servlet</servlet-name>
<url-pattern>/redirect</url-pattern>
</servlet-mapping>
<!--
- Faces Servlet Mapping.
//-->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<filter>
<filter-name>Character Encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--
- Spring Security filter chain.
//-->
<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>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<!--
- The corresponding filter mapping
//-->
<filter-mapping>
<filter-name>Character Encoding</filter-name>
<url-pattern>*.faces</url-pattern>
</filter-mapping>
<!--
- Common data source.
//-->
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/CS_PU</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!--
- Welcome Files.
//-->
<welcome-file-list>
<welcome-file>index.jspx</welcome-file>
</welcome-file-list>
</web-app>

Mule 3 and Spring MVC 3 integration

I am creating a webapp with Mule 3 and Spring mvc 3, the problem is I am not able to get mule beans in spring controllers. My application context load with mule context and is not available in spring context, when I load application context in spring context then mule and spring both have different reference to a single bean. How can I get mule context in spring mvc controller so that I can refer to the same bean object. For ref my web.xml is defined below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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"
id="return-label-service" version="2.5">
<display-name>return</display-name>
<context-param>
<param-name>org.mule.config</param-name>
<param-value>mule-return-label-flow.xml,applicationContext.xml </param-value>
</context-param>
<context-param>
<param-name>mule.serverId</param-name>
<param-value>return-label-service</param-value>
</context-param>
<listener>
<listener-class>
org.mule.config.builders.MuleXmlBuilderContextListener
</listener-class>
</listener>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath://applicationContext-web.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
<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-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
This is a possible duplicate of this question. Moreover, I think there is good documentation available here and here

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