• Category Archives: 编程语言

CAS返回更多用户信息

1.修改deployerConfigContext.xml文件

 

2.修改casServiceValidationSuccess.jsp文件

 

 

 

 

3.最后修改client端的web.xml文件:

	<!-- ======================== 单点登录开始 ======================== -->
	<!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置 -->
	<listener>
		<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
	</listener>
	<filter>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CAS Single Sign Out Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<filter>
		<filter-name>CAS Filter</filter-name>
		<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
		<init-param>
			<param-name>casServerLoginUrl</param-name>
			<param-value>http://localhost:8080/cas</param-value>
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<param-value>http://localhost:8080</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CAS Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->
	<filter>
		<filter-name>CAS Validation Filter</filter-name>
		<filter-class>
			org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
		</filter-class>
		<init-param>
			<param-name>casServerUrlPrefix</param-name>
			<param-value>http://localhost:8080/cas</param-value>
		</init-param>
		<init-param>
			<param-name>serverName</param-name>
			<param-value>http://localhost:8080</param-value>
		</init-param>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CAS Validation Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 该过滤器负责实现HttpServletRequest请求的包裹, -->
	<!-- 比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。 -->

	<filter>
		<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
		<filter-class>
			org.jasig.cas.client.util.HttpServletRequestWrapperFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。 -->
	<!-- 比如AssertionHolder.getAssertion().getPrincipal().getName()。根据客户端获取的方式可以选择使用这两种 -->


	<!-- <filter> <filter-name>CAS Assertion Thread Local Filter</filter-name> 
		<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class> 
		</filter> <filter-mapping> <filter-name>CAS Assertion Thread Local Filter</filter-name> 
		<url-pattern>/*</url-pattern> </filter-mapping> -->

	<!-- ======================== 单点登录结束 ======================== -->

4.可以用以下jsp代码来验证是否成功获取用户的更多信息:

	<%
	out.write("request.getUserPrincipal()="+request.getUserPrincipal());
	%>
	
	<br/><br/>


<%
//HttpServletRequest request = ServletActionContext.getRequest();
/*获取单点登录服务器传递过来的用户信息*/
AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
if (principal!=null) {
	out.write("principal不为空...");
	Map<String, Object> attributes = principal.getAttributes();
	for (String key : attributes.keySet()) {
		System.out.println(key+"="+attributes.get(key));
	}
}else{
	out.write("principal为空...");
}
%>

 

取消CAS的HTTPS登陆

1、修改WEB-INF\deployerConfigContext.xml,加入

 p:requireSecure="false"

<property name="authenticationHandlers">
			<list>
				<!--
					| This is the authentication handler that authenticates services by means of callback via SSL, thereby validating
					| a server side SSL certificate.
					+-->
				<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
					p:httpClient-ref="httpClient" p:requireSecure="false"/>
				<!--
					| This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS 
					| into production.  The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials
					| where the username equals the password.  You will need to replace this with an AuthenticationHandler that implements your
					| local authentication strategy.  You might accomplish this by coding a new such handler and declaring
					| edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.
					+-->
				<bean
					class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />
			</list>
		</property>

2、修改WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml,修改p:cookieSecure=”false”

	<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
		p:cookieSecure="false"
		p:cookieMaxAge="-1"
		p:cookieName="CASTGC"
		p:cookiePath="/cas" />

3、修改修改WEB-INF\spring-configuration\warnCookieGenerator.xml,修改p:cookieSecure=”false”

	<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
		p:cookieSecure="false"
		p:cookieMaxAge="-1"
		p:cookieName="CASPRIVACY"
		p:cookiePath="/cas" />

经过以上三步,cas server端修改完毕

CAS配合server4.0返回用户更多信息

打开deployerConfigContext.xml

<bean id="attributeRepository" class="org.jasig.services.persondir.support.StubPersonAttributeDao"
            p:backingMap-ref="attrRepoBackingMap" />

将以上代码替换成以下代码:

<bean id="attributeRepository" class="org.jasig.services.persondir.support.jdbc.SingleRowJdbcPersonAttributeDao">
		<!-- 指定使用的数据源,此处dataSource是已配置好的数据源 -->
		<constructor-arg index="0" ref="dataSource" />
		<!-- 从数据库中查询信息的SQL语句,通常只需要修改表名即可 -->
		<constructor-arg index="1" value="select * fromuserinfo where {0}" />
		<property name="queryAttributeMapping">
			<map>
				<!-- 上述查询的参数,将userName替换为表中表示用户名的字段名称 -->
				<entry key="username" value="userName" />
			</map>
		</property>
		<property name="resultAttributeMapping">
			<map>
				<!-- 需要返回给Web应用的其它信息,多个信息时可继续增加entry节点 -->
				<!--key值为数据表中的字段名称,value值为Client端取值时的名称标识 -->
				<entry key="address" value="address" />
			</map>
		</property>
	</bean>

 

 

接着在这个文件最后的 bean class=”org.jasig.cas.services.RegexRegisteredService” 标签内加上 p:ignoreAttributes=”true”

 

 

jsp中Java的运行环境版本等信息显示

		<table width="100%" cellpadding="5">
			<tr>
				<th width="170">Java的运行环境版本</th>
				<td width="*"><%=System.getProperties().getProperty("java.version")%></td>
			</tr>
			<tr>
				<th>Java的运行环境供应商</th>
				<td><%=System.getProperties().getProperty("java.vendor")%></td>
			</tr>
			<tr>
				<th>Java供应商的URL</th>
				<td><%=System.getProperties().getProperty("java.vendor.url")%></td>
			</tr>
			<tr>
				<th>Java的安装路径</th>
				<td><%=System.getProperties().getProperty("java.home")%></td>
			</tr>
			<tr>
				<th>Java的虚拟机规范版本</th>
				<td><%=System.getProperties().getProperty("java.vm.specification.version")%></td>
			</tr>
			<tr>
				<th>Java的虚拟机规范供应商</th>
				<td><%=System.getProperties().getProperty("java.vm.specification.vendor")%></td>
			</tr>
			<tr>
				<th>Java的虚拟机规范名称</th>
				<td><%=System.getProperties().getProperty("java.vm.specification.name")%></td>
			</tr>
			<tr>
				<th>Java的虚拟机实现版本</th>
				<td><%=System.getProperties().getProperty("java.vm.version")%></td>
			</tr>
			<tr>
				<th>Java的虚拟机实现供应商</th>
				<td><%=System.getProperties().getProperty("java.vm.vendor")%></td>
			</tr>
			<tr>
				<th>Java的虚拟机实现名称</th>
				<td><%=System.getProperties().getProperty("java.vm.name")%></td>
			</tr>
			<tr>
				<th>Java运行时环境规范版本</th>
				<td><%=System.getProperties().getProperty("java.specification.version")%></td>
			</tr>
			<tr>
				<th>Java运行时环境规范供应商</th>
				<td><%=System.getProperties().getProperty("java.specification.vender")%></td>
			</tr>
			<tr>
				<th>Java运行时环境规范名称</th>
				<td><%=System.getProperties().getProperty("java.specification.name")%></td>
			</tr>
			<tr>
				<th>Java的类格式版本号</th>
				<td><%=System.getProperties().getProperty("java.class.version")%></td>
			</tr>
			<tr>
				<th>Java的类路径</th>
				<td><%=System.getProperties().getProperty("java.class.path")%></td>
			</tr>
			<tr>
				<th>加载库时搜索的路径列表</th>
				<td><%=System.getProperties().getProperty("java.library.path")%></td>
			</tr>
			<tr>
				<th>默认的临时文件路径</th>
				<td><%=System.getProperties().getProperty("java.io.tmpdir")%></td>
			</tr>
			<tr>
				<th>一个或多个扩展目录的路径</th>
				<td><%=System.getProperties().getProperty("java.ext.dirs")%></td>
			</tr>
			<tr>
				<th>操作系统的名称</th>
				<td><%=System.getProperties().getProperty("os.name")%></td>
			</tr>
			<tr>
				<th>操作系统的构架</th>
				<td><%=System.getProperties().getProperty("os.arch")%></td>
			</tr>
			<tr>
				<th>操作系统的版本</th>
				<td><%=System.getProperties().getProperty("os.version")%></td>
			</tr>
			<tr>
				<th>文件分隔符</th>
				<td><%=System.getProperties().getProperty("file.separator")%></td>
			</tr>
			<tr>
				<th>路径分隔符</th>
				<td><%=System.getProperties().getProperty("path.separator")%></td>
			</tr>
			<tr>
				<th>行分隔符</th>
				<td><%=System.getProperties().getProperty("line.separator")%></td>
			</tr>
			<tr>
				<th>用户的账户名称</th>
				<td><%=System.getProperties().getProperty("user.name")%></td>
			</tr>
			<tr>
				<th>用户的主目录</th>
				<td><%=System.getProperties().getProperty("user.home")%></td>
			</tr>
			<tr>
				<th>用户的当前工作目录</th>
				<td><%=System.getProperties().getProperty("user.dir")%></td>
			</tr>
		</table>

 

java中实现HashMap中的按照key的字典顺序排序输出

 HashMap<String, String> maptest =new  HashMap<String, String>();
		 maptest.put("1天","day1");
		 maptest.put("5天","day5");
		 maptest.put("4天","day4");
		 maptest.put("2天","day2");
		 maptest.put("3天","day3");
		 
		 Collection<String> keyset= maptest.keySet();
		 List<String> list = new ArrayList<String>(keyset);
		 
		 //对key键值按字典升序排序
		 Collections.sort(list);
		 
		 
		 for (int i = 0; i < list.size(); i++) {
			 System.out.println("key键---值: "+list.get(i)+","+maptest.get(list.get(i)));
		 }

 

Exception occurred during processing request: Cannot create XmlPullParser 解决方法

严重: Exception occurred during processing request: Cannot create XmlPullParser
com.thoughtworks.xstream.io.StreamException: Cannot create XmlPullParser
	at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:56)
	at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1040)
	at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1031)
	at cn.com.sway.cms.web.weixin.action.TokenAction.token(TokenAction.java:109)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:446)
	at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:285)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
	at cn.com.sway.cms.interceptor.GlobalInterceptor.intercept(GlobalInterceptor.java:116)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
	at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:238)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:238)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
	at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
	at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
	at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
	at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:544)
	at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
	at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:119)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:122)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
	at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:170)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)

 

原因:缺少jar包

解决方法:

使用xStream需要引入以下3个jar包
xstream-1.4.7.jar
xmlpull_1_1_3_4c.jar
kxml2-2.3.0.jar

 

$.getJSON回调函数不执行

最近做的项目遇到一个跨域请求做验证的问题,为了解决这个跨域问题,愁了老半天,最后发现jQuery提供的一个特别简单的方法,就是用jQuery.getJSON(url, [data], [callback])

jQuery的Api提供一个列子:

$.getJSON(“http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?”, function(data){
$.each(data.items, function(i,item){
$(“<img/>”).attr(“src”, item.media.m).appendTo(“#images”);
if ( i == 3 ) return false;
});
});

说明一下注意事项:

1、请求地址一定要有 jsoncallback=? ,例如我请求 百度,传一个参数userName=123,应该这样写:http://www.baidu.com?userName=123&jsoncallback=? ,如果我不传参数userName,应该这样写:http://www.baidu.com?jsoncallback=?

2、第二个参数一定要是json格式键/值对的格式。例:{ “email”: “”}

3、回调函数,您请求的地址需要返回数据,返回的数据必须是严格的json格式的数据,还需要用 参数jsoncallback加小括号包裹jsoncallback(json格式的数据),否则就会出现回调函数不会执行的问题。

下面有有两个页面参考:

a.jsp:

<html>
<head>
<title>TEST</title>
<script type=”text/javascript” src=”./jquery-1.6.2.min.js”></script>

<script type=”text/javascript”>
jQuery(function(){
$.getJSON(“http://www.ma.com/ids/cn/b.jsp?email=123@163.com&callback=?”, function(data){
alert(data.resultMsg);
});

});
</script>
</head>
<body>
</body>
</html>

 

b.jsp

 

<%@ page contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″ errorPage=”/error.jsp” %>

<%
response.setContentType(“application/json”);//这个一定要加
String callback =    request.getParameter(“callback”);
int status = 0;
String remsg = “{\”resultMsg\”:\””+status+”\”}”;
%>
<%=callback+”(“+remsg+”)”%>

 

Struts2中使用execAndWait后,在 Action中调用getXXX()方法报告java.lang.NullPointerException异常的原因和解决方法

使用 Struts2 编写页面,遇到一个要长时间运行的接口,因此增加了一个execAndWait ,结果在 Action 中调用 getContext()的时候报告异常

1 ActionContext context = ActionContext.getContext();
2 ServletContext servletContext = (ServletContext) context.get(ServletActionContext.SERVLET_CONTEXT);  //抛空指针异常
3 String rootPath = servletContext.getRealPath("/");

查询了很多评论,最终找到原因跟解决方案,具体解释在 http://stackoverflow.com/questions/16692658/execandwait-interceptor-not-redirecting-to-success-page-after-waiting。大致意思为:execAndWait 会导致执行的Action 在另外一个线程中被执行,而getText 依赖 ActionContext ,他从 ActionContext 中获得当前的Locale 从而根据语言的不同加载不同的文字,可是,由于ActionContext 是ThreadLocal 的,而execAndWait 新开线程的时候并没有把父线程的ActionContext 传递给子线程 结果导致在新开的子线程中的ActionContext中的数据都是null ,因此出现异常信息就不足为怪了。

解决方法如下:需要重载两个类,来解决这个问题

ActionInvocationEx.java

package byrs.rms.interceptors;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionEventListener;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import com.opensymphony.xwork2.util.ValueStack;

public class ActionInvocationEx implements ActionInvocation {

    /**
     * 
     */
    private static final long serialVersionUID = 2434502343414625665L;

    private final ActionInvocation mActionInvocation;

    private final ActionContext context;

    public ActionInvocationEx(ActionInvocation aActionInvocation,ActionContext aContext)
    {
        mActionInvocation = aActionInvocation;
        context = aContext;
    }

    public Object getAction() {
        return mActionInvocation.getAction();
    }

    public boolean isExecuted() {
        return mActionInvocation.isExecuted();
    }

    public ActionContext getInvocationContext() {
        return mActionInvocation.getInvocationContext();
    }

    public ActionProxy getProxy() {
        return mActionInvocation.getProxy();
    }

    public Result getResult() throws Exception {
        return mActionInvocation.getResult();
    }

    public String getResultCode() {
        return mActionInvocation.getResultCode();
    }

    public void setResultCode(String resultCode) {
        mActionInvocation.setResultCode(resultCode);
    }

    public ValueStack getStack() {
        return mActionInvocation.getStack();
    }

    public void addPreResultListener(PreResultListener listener) {
        mActionInvocation.addPreResultListener(listener);
    }

    public String invoke() throws Exception {
        return mActionInvocation.invoke();
    }

    public String invokeActionOnly() throws Exception {
        return mActionInvocation.invokeActionOnly();
    }

    public void setActionEventListener(ActionEventListener listener) {
        mActionInvocation.setActionEventListener(listener);
    }

    public void init(ActionProxy proxy) {
        mActionInvocation.init(proxy);
    }

    public ActionInvocation serialize() {
        return mActionInvocation.serialize();
    }

    public ActionInvocation deserialize(ActionContext actionContext) {
        return mActionInvocation.deserialize(actionContext);
    }

    /**
     * @return the context
     */
    public ActionContext getContext() {
        return context;
    }

}

 

ExecAndWaitInterceptorEx.java

package byrs.rms.interceptors;

import org.apache.struts2.interceptor.BackgroundProcess;
import org.apache.struts2.interceptor.ExecuteAndWaitInterceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

public class ExecAndWaitInterceptorEx extends ExecuteAndWaitInterceptor {

    /**
     * 
     */
    private static final long serialVersionUID = 8829373762598564300L;
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected BackgroundProcess getNewBackgroundProcess(String arg0, ActionInvocation arg1, int arg2) {
        ActionInvocationEx aActionInvocationEx = new ActionInvocationEx(arg1,ActionContext.getContext());
        return new BackgroundProcessEx(arg0, aActionInvocationEx, arg2);
    }

    private class BackgroundProcessEx extends BackgroundProcess {
        public BackgroundProcessEx(String threadName,
                ActionInvocation invocation, int threadPriority) {
            super(threadName, invocation, threadPriority);
        }

        private static final long serialVersionUID = -9069896828432838638L;
        /**
         * {@inheritDoc}
         * @throws InterruptedException 
         */
        @Override
        protected void beforeInvocation() throws InterruptedException {
            ActionInvocationEx aActionInvocationEx = (ActionInvocationEx)this.invocation;
            ActionContext context = aActionInvocationEx.getContext();
            ActionContext.setContext(context);
        }

        /**
         * {@inheritDoc}
         */
       @Override
        protected void afterInvocation() {
            ActionContext.setContext(null);
        }

    }

}

 

然后在struts.xml中覆盖默认拦截器即可

<interceptors > 
	<interceptor name="execAndWait" class="byrs.rms.interceptors.ExecAndWaitInterceptorEx"/> 
</interceptors >

参考自:http://www.mobibrw.com/?p=1046

Linux环境 Mysql+Hibernate command denied to user 错误

栈头的样子:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user ‘XXX’@’XXX.XXX.XXX.XXX’ for table ‘XXX’

1.可能是权限原因

2.如果权限没有问题,并且是使用hibernate框架的前提下。可能是你的xml映射文件或者是annotation映射类里面设置了一个默认的schema名称,而叫这个名的schema在当前数据库不存在。这个shema变量叫catalog,均在xml和映射类中的头部,将其去掉即可以解决问题。

close