---
title: "Spring/Struts从Tomcat到WPS/WAS移植的问题"
author: "chhff"
date: "2010-11-29T17:36:05Z"
tags: []
categories: ["企业架构"]
slug: "spring-struts-tomcat-wps-was"
draft: false
lastmod: "2010-11-29T17:37:12Z"
id: 828492
url: "https://chhff.iteye.com/blog/828492"
---

问题：
javax.servlet.ServletException: Unable to instantiate Action, menuLoginAction,  defined for 'menuLogin' in namespace '/sys'Error creating bean with name 'menuLoginAction': 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. - action - file:/C:/W7/profiles/AppSrv01/installedApps/localhostNode01Cell/CmccSzyd.ear/CmccSzydWeb.war/WEB-INF/classes/conf/struts/sys.xml:7:67

问题的本质：
在于struts和Spring对HTTP request的执行顺序要求，先是Spring（会写一些theadlocal的信息），然后才能是Struts. 
用户的struts和Spring的定义：
	
```xml
<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
```


在WAS的webcontainer对于listener和filter执行的顺序有些奇怪。
如果是servlet，那么就是先执行listener然后是filter，那么就能满足Spring/Struts的要求。
如果不是servlet，那么会先执行filter。
用户的struts定义没有servlet，那么就根据struts的filter的url pattern, 去先执行了struts(没有Spring去处理HTTP request), 那么就拿不到相应的信息，出现的错误。

有两种解决方案：
1）Spring不使用listener，改成使用filter的方式，并把Spring的filter放在Struts前面。
	
```xml
<filter>
		<filter-name>SpringRequestContextFiler</filter-name>
		<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>SpringRequestContextFiler</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
```

2. 作个假的servlet，servlet什么也不处理，并设置servlet mapping为"/*"，这样的话，就是先执行Spring的listener.