Struts2 项目配置总结(拦截错误,通配符等)

Struts2 项目配置总结(拦截异常,通配符等)
虽说api都可以查到,各个版本还有不同.但还是记下
1.简单配置 - 使用通配符
<action name="*_*" class="house.action.sys.{1}" method="{2}">
    <result type="titles">front.out.other</result>
</action>
使用通配符点简单程度,得看设计程序时命名的规范是怎么样
本例: loginAction_create[看配置 一般有.*;.do;.action等]
将进入 loginAction 的create方法执行
*代表任何;从1开始数就对了
很简单,灵活配置就可以了
  主要的变动在配置返回上
    也就是<result>上;

看几个常见类型:
1:挎包配置

<result name="input" type="chain">
      <param name="namespace">/broker</param>
     <param name="actionName">index_BrokerIndex</param>
</result>

这样就可以简单的挎包了
result 配置除了这个挎包的 其他的跳转一般集中在type的配置上了.
首先的看看有哪些类型:
chain       用来处理Action链(action的值会保留穿过去)  
dispatcher   用来转向页面,通常处理JSP  
freeMarker     处理FreeMarker模板  
httpHeader     用来控制特殊的Http行为  
redirect       重定向到一个URL  
redirectAction    重定向到一个Action(前action的值不保留) 
stream          向浏览器发送InputSream对象,通常用来处理文件下载  
velocity       处理Velocity模板  
XLS             处理XML/XLST模板  
plainText      显示原始文件内容,例如文件源代码  
tiles    结合tile使用[上例中使用的就是这种]

---拦截先自定义的异常
1.定义自己的异常 如 sql注入
package house.exception;
/**
 * sql注入异常
 * @author forest.chen
 */
public class SqlInjectionException extends RuntimeException {
	
	private static final long serialVersionUID = 6314572084249365786L;
	
	public  SqlInjectionException(){
		super("警告!含非法字符.");
	}
	
	public  SqlInjectionException(String msg){
		super(msg);
	}
}


用运行时异常

配置struts2.xml拦截
<package name="sys" extends="tiles-default" namespace="/sys">
  <global-results>
       <result name="input">/jsp/sys/sqlinjectionerror.jsp</result>
       <result name="error">/jsp/sys/error.jsp</result>
        <result name="login">/jsp/sys/sys.jsp</result>
    </global-results>
      <global-exception-mappings> 
          <exception-mapping result="input"
       exception="house.exception.SqlInjectionException">
        </exception-mapping>   
         <exception-mapping result="login" 
       exception="house.exception.SysNotLoginException">
      </exception-mapping>   
           <exception-mapping result="error" 
         exception="java.lang.Exception">
       </exception-mapping>       
   </global-exception-mappings>


这里我定义了三个异常
这样 只要我们
throw new SqlInjectionException();
这样,就像在struts中返回了input 自动就会进入
/jsp/sys/sqlinjectionerror.jsp
中了..嘻嘻 在sqlinjectionerror.jsp页面上只要写上
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>用户输入信息有误</title>
  </head>
  <body>
    <ul>
      <li><img src="<s:property value='sysResourceBase'/>/images/house2009_img63.gif"></li>
      <li><s:actionerror/><s:property value="exception.message" /></li>
      <div class="error_bg1"><a href="javascript:history.go(-1)">返回上一页</a></div>
    </ul>
  </body>
</html>


就可以了,throw new SqlInjectionException("哈哈哈哈哈哈哈");
简单概述下;titles 写不下了.单独写个呗.