---
title: "struts2 基础知识"
author: "yangzhibin_java"
date: "2009-08-03T23:16:03Z"
tags: []
slug: "struts2"
draft: false
lastmod: "2009-08-06T09:28:42Z"
id: 438974
url: "https://yangzhibin-java.iteye.com/blog/438974"
---

**struts2 Action是一个pojo类**

1，可以实现**Action接口**

      excute()方法可以不抛出异常

2，可以继承**ActionSupport类**

 

 

**二，struts2的5个配置文件**

**Web.xml**  
**Struts.xml** //主配置文件  
**Struts.properties** //属性文件  
**Struts-default.xml** //struts2的默认配置  
**Struts-plugin.xml**   //Struts2框架的插件配置文件

**三，&lt;action&gt;**

```
<result name="success" type="dispatcher">
    <param name="location">/ThankYou.jsp</param>
</result>
```

 

    **1，** **name属性配置**

             1.1 name属性默认不能加**斜杠** （因为**struts.enable.SlashesInActionNames** = **false** ）

                           解决办法：**struts.enable.SlashesInActionNames=true**

             1.2 配置

                           正确格式：&lt;action name="**namespace** /**test1** " class=""&gt;

                           错误格式：&lt;action name=" **/namespace** / **test1** " class=""&gt;

    **2，method属性配置**

              2.1 作用：可以**静态的调用** 访问action方法的某个方法（**DMI：动态调用** ）

              2.2 用法：

                            1，action里的方法名和method里配置的一样

                            2，action里的方法名前加do（**避免关键字** ，**如main** ）

   **3，DMI动态配置**

              访问action时，在action名称后加上“**!add** ” （如：&lt;a href="**user!add .action** "&gt;访问&lt;/a&gt;）

   **4，Forword Action(跳转action，空action)**

             专门用来跳转用的

 

```
<action name="main">
	<result type="redirect">/main.jsp</result>
	<!--加上type="redirect"可以直接掉到main.jsp上,不加跳转到main.action上-->
</action>
```

 

   **5，默认action**

```
<default-action-ref name="error"/>

<action name="error">
	<result>/error.jsp</result>
</action>

<!--变种-->
<action name="*">
	<result>/error.jsp</result>
</action>
<!-- 
	如果路径中没有通配符，则此Action优先级最高
	如果路径中有通配符，不管是什么通配符，一律按顺序匹配
-->
```

 

    **6，通配符配置（解决action过多）（多个action映射一个请求时，要注意优先级）**

          1，基本知识

                * ：o-n个字符（不包括“/ ”）

                **\*\*** ：o-n个字符（包括“/ ”）

                **\\** ：转义符

```
<action name="*User" class="cn.cstp.org.action.UserAction" method="{1}">
	<result>/{1}UserSuc.jsp</result>
</action>

<action name="*_*" class="cn.cstp.org.action.{1}Action" method="{2}">
	<result>/{0}Suc.jsp</result>
        <!--{0}表示匹配完整的信息（*_*）-->
</action>

<action name="*" class="cn.cstp.org.action.ErrorAction">
	<result>/error.jsp</result>
</action>

<!-- 
	如果路径中没有通配符，则此Action优先级最高
	如果路径中有通配符，不管是什么通配符，一律按顺序匹配
-->
```

 

 

    **7，Zero Configuration-注解配置**

          @ParentPackage  
          @Namespace  
          @Result  
          @Results

     **struts2.0配置方法**

```
<filter-name>struts</filter-name> 
       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
            <init-param> 
                <param-name>actionPackages</param-name> <!--固定参数名-->
                <param-value>com.struts2.action</param-value><!--所有action所在的包-->
            </init-param> 
 </filter>
```

 

 

**四，&lt;pakeage&gt;namespace（命名空间 ）属性配置**

        1，运行原理

                 先到指定的命名空间里去找，找不到就到默认的命名空间里去找

                  （所有你随便个什么路径，只要action名称正确就可以访问到）

        2，&lt;pageage name="" extends="" **namespace=""** &gt; **-------建议都给配上**

            **namespace** 不配置是就为默认包  

        3，如何配置：

            正确格式：&lt;pageage name="" extends="" namespace="**/namespace** " &gt;

                           &lt;pageage name="" extends="" namespace="**/** " &gt;跟空间

            错误格式：&lt;pageage name="" extends="" namespace="**namespace** " &gt;

 

**五，&lt;result&gt;**

       1，type属性

chain 用来处理Action链 跳转到action上 **dispatcher** （默认） 用来转向页面，通常处理JSP freemarker 处理FreeMarker模板 httpheader 控制特殊HTTP行为的结果类型 redirect 重定向到一个URL redirectAction 重定向到一个Action 跳转到action上 stream

向浏览器发送InputSream对象，通常用来处理文件下载，

还可用于返回AJAX数据

velocity 处理Velocity模板 xslt 处理XML/XLST模板 plainText 显示原始文件内容，例如文件源代码 返回源代码

 

       2，跳转到action上

```
<action name="" class="">
    <!--服务器跳转（请求转发）-->
    <result name="success" type="chain">
             <param name="actionName">user</param>
             <param name="namespace">/xxx</param>
    </result>
</action>
```

```
<action name="" class="">
    <!--客服端跳转跳转（请求跳转）-->
    <result name="success" type="redirectAction">
             <param name="actionName">user</param>
             <param name="namespace">/xxx</param>
    </result>
</action>
```

      3，全局结果集

```
<global-results>
	<result name="success">/main.jsp</result>
	<result name="login">/login.jsp</result>
	<result name="input">/login.jsp</result>
</global-results>
```

     4，动态结果

```
<action name="" class="">
    <result name="success" type="redirectAction">${nextActionName}</result>
</action>
```

 

**六，异常处理**

```
<!--全局异常-->
<global-results>
	<result name="global-exception">/global-exception.jsp</result>
</global-results>
<global-exception-mappings>
	<exception-mapping result="global-exception" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>

<!--局部异常-->
<result name="exception" type="dispatcher">/exception.jsp</result>
<exception-mapping result="exception" exception="java.sql.SQLException"></exception-mapping>
```