struts2 拦截器开发的总结
时间:2025-05-14
时间:2025-05-14
一、如何获取异常信息的String类型信息二、C标签if语句的写法三、设置全局错误页面四、如何为div赋值五、自定义拦截器的使用
一、如何获取异常信息的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页面如何打印异常信息
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" import="java.io.*"%>
<%@ taglib uri="http://www.77cn.com.cn/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);
%>
二、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>
步骤二、编写错误页面
一、如何获取异常信息的String类型信息二、C标签if语句的写法三、设置全局错误页面四、如何为div赋值五、自定义拦截器的使用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" import="java.io.*"%>
<%@ taglib uri="http://www.77cn.com.cn/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配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://www.77cn.com.cn/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/"> <constant name="struts.ognl.allowStaticMethodAccess" value="true" /> <constant name="struts.multipart.maxSize" value="100000000"/>
一、如何获取异常信息的String类型信息二、C标签if语句的写法三、设置全局错误页面四、如何为div赋值五、自定义拦截器的使用
<interceptors>
<interceptor name="exceptionInterceptor"
class="com.web.interceptor.ExceptionInterceptor">
<param name="includeMethods">execute</param>
</interceptor>
<interceptor name="loginInterceptor"
class="com.web.interceptor.LoginInterceptor"/>
<interceptor-stack name="defaultStack">
<interceptor-ref name="loginInterceptor"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack"></default-interceptor-ref>
<global-results>
<result name="login" type="redirectAction">index</result>
</global-results>
<action name="compile" class="http://www.77cn.com.cnpileAction">
<result name="exec">/WEB-INF/generate/${jspFileName}</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="exceptionInterceptor"/>
</action>
</package>
(2) 编写自定义拦截器类 </struts>
public class ExceptionInterceptor extends MethodFilterInterceptor{
protected Log log = LogFactory.getLog(this.getClass()); /** * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) */ private static final long serialVersionUID = -6071035934377365730L;
@Override
public String doIntercept(ActionInvocation invocation) throws Exception {
String result;
try {
result = invocation.invoke();
一、如何获取异常信息的String类型信息二、C标签if语句的写法三、设置全局错误页面四、如何为div赋值五、自定义拦截器的使用
} catch (Exception e) {
//对捕获到的异常进行处理,例如将 …… 此处隐藏:1863字,全部文档内容请下载后查看。喜欢就下载吧 ……
下一篇:世界室内设计史-第20章