2-struts2基础设置(实现Action的三种方式,路径通配,常量,默认配置)

目录:

  1. 实现action的三种方式
  2. struts2 的路径通配
  3. struts2 常量
  4. struts默认配置
  5. action属性注入

1.实现Action的三种方式:

  action是用来处理一次用户请求的对象。

  • Action不需要继承或实现任何接口或类
1 package action;
2 
3 public class UserAction {
4     
5     public String login() throws Exception{
6         System.out.println("UserAction.login()");
7         return "success";
8     }
9 }
UserAction
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
        

<struts>    
        <package name="default" namespace="/" extends="struts-default">
            <action name="login" class="action.UserAction" method="login">
                <result name="success">/success.jsp</result>
            </action>
        </package>
        <!-- 感叹号可以使用 -->
        <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
struts.xml
  • 实现Action接口
    • 定义了默认的execute方法, 提供了项目中常用的视图标记。
    • 注意在导入Action接口时候,会有很多同名不同包的类或接口都叫做Action,如java.swing.Action, 要注意我们要导入的Action接口在xwork包下。可以在 D:install_foldlibSource1_struts2_recoursestruts-2.5.14.1-allstruts-2.5.14.1srccoresrcmainjavacomopensymphonyxwork2 下找到Action接口的定义规范:
      package com.opensymphony.xwork2;
      
      public interface Action {
      
          public static final String SUCCESS = "success";
          public static final String NONE = "none";
          public static final String ERROR = "error";
          public static final String INPUT = "input";
          public static final String LOGIN = "login";
          public String execute() throws Exception;
      
      }
  • 继承ActionSupport类
    • 提供了常用的视图标记及数据校验功能, 而且不必一定实现execute方法。
    • 由于Action过于简单,对于开发者帮助有限,一般很少直接实现Action接口,而是直接继承ActionSuppor类。

2. struts2 的路径通配:

解决方法:

1.修改struts.enable.DynamicMethodInvocation, 复制为true,默认为false并且在action配置中设置allowed-methods标签

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <package name="default" namespace="/" extends="struts-default">
        <action name="user_*" class="action.UserAction" method="{1}">
            <result name="login">/success.jsp</result>
            <allowed-methods>login</allowed-methods>
        </action>
    </package>
</struts>
通用符匹配

2. 或直接在package标签中配置global-allowed-methods

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <global-allowed-methods>login</global-allowed-methods>
        <action name="user_*" class="action.UserAction" method="{1}">
            <result name="login">/success.jsp</result>
        </action>
    </package>
</struts>
global-allowed-methods
<global-allowed-methods>login,register</global-allowed-methods>
global-allowed-method定义多个方法
<global-allowed-methods>regex:.*</global-allowed-methods>
全部方法匹配

多个通配符使用:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="*_*" class="action.{1}Action" method="{2}">
            <result name="{2}">/{2}.jsp</result>
        </action>
    </package>
</struts>
*_*

3. struts2 常量

通常推荐在struts.xml中配置常量,便于集中管理。

  • 设置访问路径的匹配:
<constant name="struts.action.extension" value="action,do,"></constant>

* 可以用 ".do",".action",及空来访问

  • struts2动态方法调用:

提供了一种访问action的方法, 访问路径规则:user!login(action的名称!action的方法)

<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

不建议使用,用户可以直接调用方法,存在安全隐患。

常量说明

  • 全局视图

当有很多action公用一个result且跳转视图相同时可直接设置全局视图

<global-results>
        <result name="login">/login.jsp</result>
</global-results>        
<action name="user" class="action.UserAction" method="login"></action>

4. struts默认配置:

<action>

  name:必填

  class: 默认为ActionSupport类。继承自struts-default中的配置<default-class-ref class="com.opensymphony.xwork2.ActionSupport">。

  method:默认为execute

  <result>

    name:可选。默认为success

    type:可选。默认为dispatcher

* 全部使用默认配置的action可专门用于转发到WEB-INF下的页面。

<action name="book">
    <result>/WEB-INF/jsp/login.jsp</result>
</action>

<result>

  • name属性:指定逻辑视图的名称,默认值是success
  • type属性:指定返回的视图的处理类型,默认为dispatcher。
    • 类型 说明
      chain 用来处理Action链,被跳转的Action中人能获取上个action中的值,如request信息
      dispatcher 用来转向页面,通常处理jsp,也是默认的结果类型
      freemarker 用来整合FreeMarker模板结果类型
      httpheader 用来处理特殊的HTTP行为结果类型
      redirect 重定向到一个URL,被跳转的页面中丢失传递信息(完成一次交互,浏览器需要发送两次请求)
      redirectAction 重定向到一个Action,跳转的页面中丢失传递信息
      stream 向浏览器发送InputStream对象,通常用来处理文件下载,还可以用于返回Ajax数据
      velocity 用来整合Velocity模板结果类型
      xslt 用来整个XML/XSTL结果类型
      plainText 显示原始文件内容,例如文件原代码
  • 全局result:作用范围是整个包。当重名时局部result会覆盖全局result。
        <package name="test" extends="struts-default" namespace="/">
            <global-results>
                <result>/index.jsp</result>
            </global-results>
        </package>

默认action

当请求一个不存在的action时,页面将会显示404错误,struts允许我们配置一个默认的action,即如果一个不存在的action被请求,那么默认的action将会被执行。

<struts>
    <package name="test" extends="struts-default" namespace="/">
    <default-action-ref name="defaultAction"></default-action-ref>
    <action name="defaultAction" class="类名">
        <result>jsp文件名.jsp</result>
    </action>
    </package>
</struts>

5. action的属性注入

引入:

  • 原始代码:
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>    
    <package name="default" namespace="/" extends="struts-default">
        <action name="upload" class="action.UserAction" method="upLoad">
            <result>/login.jsp</result>
        </action>
    </package>
</struts>
struts.xml
 1 package action;
 2 
 3 import java.io.File;
 4 import org.apache.commons.io.FileUtils;
 5 
 6 /**
 7  * 
 8 请求参数的封装
 9 自定义类型转换
10 文件的上传和下载
11  */
12 import com.opensymphony.xwork2.ActionSupport;
13 
14 public class UserAction extends ActionSupport{
15     /**
16      * 模拟文件上传方法
17      * @return
18      * @throws Exception
19      */
20     public String upLoad() throws Exception{
21         //已经获取文件
22         File srcFile = new File("D:/ajava/user.jpg");
23         
24         //保存到服务器硬盘
25         //下面的写法会把服务器的地址硬编码
26         FileUtils.copyFile(srcFile, new File("D:/ajava/test/user.jpg"+srcFile.getName()));
27         return SUCCESS;
28     }
29     
30 }
模拟文件上传方法
  • 问题:
    • 上面把服务器地址写到了upload方法中,这样会把服务器的地址硬编码。如果Action对象中需要把一些经常改变的参数提取到配置文件在,那么就可以使用属性注入的方法。
  • 改进:
 1 package action;
 2 
 3 import java.io.File;
 4 
 5 import org.apache.commons.io.FileUtils;
 6 
 7 
 8 /**
 9  * 
10 请求参数的封装
11 自定义类型转换
12 文件的上传和下载
13  */
14 import com.opensymphony.xwork2.ActionSupport;
15 
16 public class UserAction extends ActionSupport{
17     //在Action中提供属性, 并提供set方法给外部action的参数进行注入
18     private String savePath;
19     public void setSavePath(String savePath) {
20         this.savePath = savePath;
21     }
22     
23 
24     public String upLoad() throws Exception{
25         //获取文件
26         File srcFile = new File("D:/ajava/user.jpg");
27         
28         //保存到服务器硬盘
29         FileUtils.copyFile(srcFile, new File(savePath+srcFile.getName()));
30         return SUCCESS;
31     }
32     
33 }
UserAction
        <action name="upload" class="action.UserAction" method="upLoad">
        <!-- 使用param往action对象的属性中注入 name= setter方法的名字去掉set后剩下的部分 -->
            <param name="savePath">D:/ajava/test/</param>
            <result>/login.jsp</result>
        </action>
struts.xml

 

方法:

  1. 在action类中声明一个成员变量
  2. 在action中提供该变量的setter方法
  3. 在对应的struts.xml文件中找到对应的action配置,使用<param>标签进行注入
原文地址:https://www.cnblogs.com/clairexxx/p/9861497.html