Spring:Error while accessing scoped beans from jsp - spring

I learnt how to access session scoped sping beans from a jsp but got the following error:
No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? scoped beans from jsp
request.getSession().getAttribute("scopedTarget.otmSessionHolder")).getUserVO()
my web.xml:
<listener>
<description>Spring Context Listener</description>
<display-name>Spring Context Listener</display-name>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<display-name>Request Context Listener</display-name>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<servlet>
<description>This Servlet intercepts all requests for this WebApplication</description>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans/web/**/*-Beans.xml</param-value>
</init-param>
<servlet>
<description>This Servlet intercepts all requests for this WebApplication</description>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans/web/**/*-Beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>This Servlet intercepts all RESTful requests for this Web Application</description>
<servlet-name>RESTfulSpringDispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans/web/RESTful-Beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
But still I get this exception
I use sitemesh decorator for template.Have been going thru these forums,but to no avail.Kindly help me on this

Related

The requested resource is not available on project launch spring mvc

Please why I am getting the requested resource is not found on project start up even though everything seems alright
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<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>
Please assist me!!!
You need to load the Spring context with org.springframework.web.context.ContextLoaderListener, not Log4jConfigListener (or try out Spring Boot)
See Loading context in Spring using web.xml

Spring not initializing session bean with requestcontextlistener

I am trying to decouple my webapp from enunciate and I have a web.xml as follows but when Spring 3 bootstraps I get:
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire method:.... Scope 'session' is not active for the current
thread; consider defining a scoped proxy for this bean if you intend
to refer to it from a singleton; nested exception is
java.lang.IllegalStateException: No thread-bound request found: Are
you referring to request attributes outside of an actual web request,
or processing a request outside of the originally receiving thread? If
you are actually operating within a web request and still receive this
message, your code is probably running outside of
DispatcherServlet/DispatcherPortlet: In this case, use
RequestContextListener or RequestContextFilter to expose the current
request.
The thing is I have a request context listener:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>org.springframework.security.filterChainProxy</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>org.springframework.security.filterChainProxy</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<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>
</servlet>
<servlet>
<servlet-name>jersey2</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>org.mypackage.web</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Trace</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>jaxrs.providers</param-name>
<param-value>
org.mypackage.web.rest.SerializableProvider
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey2</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jersey2</servlet-name>
<url-pattern>/rest/myurl/session</url-pattern>
</servlet-mapping>
</web-app>
So why does Spring not boot properly? Could it be because its thread related and sessionbean to sessionbean?
According to error you can not autowire session scope class because your singleton classes loaded when application starts. In the beginning session does not exist.
You need to create proxy interface
ex : #Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)

Tweak of DispatcherServlet settings web.xml - need alternative BeanInjection approach

I am trying to tweak the definition of my dispatcher servlet in my web.xml.
I had everything working perfectly using the settings below...
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml,
classpath:META-INF/spring/spring-application-context.xml
</param-value>
</context-param>
But now I am trying to tweak the settings to be the following and I get errors...
<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>
/WEB-INF/mvc-dispatcher-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF/spring/spring-application-context.xml
</param-value>
</context-param>
The error I get is a null pointer exception for one of my filter classes because in that class I do the following...
#Autowired
#Qualifier("sessionRegistry")
private SessionRegistry sessionRegistry;
Where #Qualifier("sessionRegistry") is defined somewhere in classpath:META-INF/spring/spring-application-context.xml. So what I was doing was grabbing a bean defined in spring-application-context.xml using a qualifier but I can no longer do that with the new DispatcherServlet definition inside web.xml.
Can someone suggest to me an alternative method of injecting the sessionRegistry bean defined in my spring-application-context.xml that will work.
SessionRegistry is defined in spring-application-context.xml like this...
<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />
updated
I have a ContextLoaderListener defined in web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Here is a link to my full web.xml...
http://pastebin.com/fJXwGNgn
thanks

application-config.xml vs mvc-config.xml in spring

Im a newbie to Spring and trying to understand the web.xml file.
I have created a new SPring MVC Maven project using STS,
I'm little bit confused between the application-config.xml vs mvc-config.xml file...
mvc-config.xml contains the servlet mappings but what information does the application-config file contains..
<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>
Usually the mvc configuration(/WEB-INF/mvc-config.xml) contains the the beans that are needed by the controller layer (e.g. the controllers, view resolvers ...) The application configuration(classpath:spring/application-config.xml) is for the model layer (here you can define daos, services...)

Spring+multiple gwt servlets

1.From searching the web i understand that to use spring with gwt i would have to replace the default DispatcherServlet with org.spring4gwt.server.SpringGwtRemoteServiceServlet . But all the geomajas ( which uses spring + gwt ) examples i have seen use in fact the supposed-to-be-replaced DispatcherServlet. How could i do that too? .
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Geomajas application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- framework context -->
classpath:org/geomajas/spring/geomajasContext.xml
<!-- use rasterizing -->
classpath:org/geomajas/plugin/rasterizing/DefaultRasterizedPipelines.xml
<!-- application context -->
WEB-INF/applicationContext.xml
WEB-INF/layerOsm.xml
WEB-INF/mapOsm.xml
</param-value>
</context-param>
<filter>
<filter-name>CacheFilter</filter-name>
<filter-class>org.geomajas.servlet.CacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CacheFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<!-- only needed for direct GWT -->
<listener>
<listener-class>org.geomajas.servlet.PrepareScanningContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<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*:META-INF/geomajasWebContext.xml</param-value>
<description>Spring Web-MVC specific (additional) context files.</description>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/d/*</url-pattern>
<url-pattern>/${artifactId}/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
What modifications must i make to the above file so that i can add another spring managed servlet ?
Here is how I integrated Spring with GWT via |SpringGwtRemoteServiceServlet:
Inweb.xml`:
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/dispatch/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>springGwtRemoteServiceServlet</servlet-name>
<servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springGwtRemoteServiceServlet</servlet-name>
<url-pattern>/nameOfYourApp/springGwtServices/*</url-pattern>
</servlet-mapping>
Than, whenever you wish to define a Spring managed service, use springGwtServices in the RemoteServiceRelativePath:
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.google.gwt.user.client.rpc.RemoteService;
#RemoteServiceRelativePath("springGwtServices/userService")
public interface UserService extends RemoteService{
}
For an implementation example:
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
#Service("userService")
public class UserServiceImpl extends RemoteServiceServlet implements UserService{
}
I hope this is what you needed

Resources