一、如何获取异常信息的String类型信息
(1)java类的写法
try {
result = invocation.invoke();
} catch (Exception e) {
StringWriter sw=new StringWriter();
PrintWriter pw=new PrintWriter(sw);
e.printStackTrace(pw);
String exceptionMessage = sw.toString();
}
(2)jsp页面如何打印异常信息
<%@ pagelanguage="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isErrorPage="true"import="java.io.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"prefix="c"%>
<%=exception.getMessage()%><br>
<%=exception.getLocalizedMessage()%>
<%
StringWritersw=new StringWriter();
PrintWriterpw=new PrintWriter(sw);
exception.printStackTrace(pw);
response.setStatus(500);
out.print(sw);
%>
二、C标签if语句的写法
<%if (exception !=null ){%>
<%=exception.getMessage()%><br>
<% }else {%>
error!
<% }%>
三、设置全局错误页面
步骤一、web.xml配置
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
步骤二、编写错误页面
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ pageisErrorPage="true" import="java.io.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%=exception.getMessage()%><br>
<%=exception.getLocalizedMessage()%>
<%
StringWriter sw=new StringWriter();
PrintWriter pw=new PrintWriter(sw);
exception.printStackTrace(pw);
response.setStatus(500);
out.print(sw);
%>
四、如何为div赋值
<div id="urlDetailWindow" class="easyui-dialog" title="访问路径详情" style="width:500px;height:200px;padding:10px" closed="true"shadow="true" modal="true">
<div id="urlDetailIdDiv"></div>
</div>
function urlDetail(){
var row =$('#datalist').datagrid('getSelected');
if(row){
$("#urlDetailIdDiv").text(row["url"]);
$("#urlDetailWindow").window('open');
}
}
或者
document.getElementById("urlDetailIdDiv ").innerHTML=" aaaaa ";
五、自定义拦截器的使用
(1)struts.xml配置
<?xmlversion="1.0" encoding="UTF-8" ?>
<!DOCTYPEstruts PUBLIC
"-//Apache Software Foundation//DTDStruts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constantname="struts.ognl.allowStaticMethodAccess" value="true" />
<constantname="struts.multipart.maxSize" value="100000000"/>
<package name="default" extends="struts-default" namespace="/">
<interceptors>
<interceptor name="exceptionInterceptor" class="com.qiyi.cms.web.interceptor.ExceptionInterceptor">
<paramname="includeMethods">execute</param>
</interceptor>
<interceptorname="loginInterceptor" class="com.qiyi.cms.web.interceptor.LoginInterceptor"/>
<interceptor-stack name="cmsDefaultStack">
<interceptor-ref name="loginInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="cmsDefaultStack"></default-interceptor-ref>
<global-results>
<result name="login" type="redirectAction">index</result>
</global-results>
<action name="compile" class="com.qiyi.cms.web.action.CompileAction">
<result name="exec">/WEB-INF/generate/${jspFileName}</result>
<interceptor-ref name="cmsDefaultStack"/>
<interceptor-ref name="exceptionInterceptor"/>
</action>
</package>
</struts>
(2)编写自定义拦截器类
publicclassExceptionInterceptor extends MethodFilterInterceptor{
protected Log log =LogFactory.getLog(this.getClass());
/**
* @FieldsserialVersionUID : TODO(用一句话描述这个变量表示什么)
*/
privatestaticfinallongserialVersionUID = -6071035934377365730L;
@Override
public StringdoIntercept(ActionInvocation invocation) throwsException {
String result;
try {
result = invocation.invoke();
} catch(Exception e) {
//对捕获到的异常进行处理,例如将异常存入库中(上述一)
//处理完后可将异常抛出,通过设置的全局错误页面(上述三),将异常捕获到显示到页面
thrownewException(e.getMessage());
}
returnresult;
}
}
(3)被拦截的action方法
publicclass CompileActionextends BaseAction {
@Override
public Stringexecute() throws Exception {
//逻辑代码,有异常的时候抛出
System.out.print("execute 方法");
return"exec";
}
}
补充说明:struts2API