框架学习之Struts2 第二节 Action的详解

本文详细介绍了Struts2框架的基本概念、处理流程、配置文件的使用、请求指定、action属性注入、result转发类型、动态调用方法等功能,通过XML和Java代码展示了配置和实现过程,包括多个配置文件的指定、请求后缀的修改、action类属性赋值、全局视图和动态方法调用等关键点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.cnblogs.com/yinger/archive/2011/07/25/2116707.html

 

1.Action 名称的搜索顺序

它是对namespace从后面往前面的递推的搜索,如果当前的这个path组成的namespace不是一个package,那么就减少一个path继续搜索

如果是一个package,并且在那个package中找到了action就会执行,但是如果这个package下没有这个action,那么就会直接在默认的package中搜索

如果找到了就执行,如果还是没有,就提示找不到action

 

2. Action 配置中的各个默认值

action的class--->ActionSupport

action的method--->execute

result的name--->SUCCESS=”success”

 

3.Action中的result的各种转发类型

①dispatcher:默认的,内部请求转发

②redirect:浏览器重定向

③redirectAction:重定向到另一个action

④plainText:原样输出网页源代码,不执行Jsp

 

4.为action属性注入值

在action配置中为响应的action的类的属性赋值,同时可以在返回的结果视图中得到这个值

这个属性一定要提供set方法

 

5.指定struts2的请求

指定请求的后缀:修改常量struts.action.extension 的值,如果有多个后缀,可以使用逗号隔开

 

常量的定义:常量可以定义在多个配置文件中,但是由于加载常量的顺序原因,后面的文件的常量值会覆盖前面的常量值

建议在struts.xml中配置

 

几种常见的常量配置介绍

 

6. struts2的处理流程

这一部份很重要,解释一下:用户在地址栏里输入了url后,就向服务器发送了一个请求,根据 web.xml 中的配置找到
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

对应的类 StrutsPrepareAndExecuteFilter 来处理,它是框架的核心控制器,它负责拦截

<url-pattern>/*</url-pattern> (web.xml 中配置了)指定的用户请求

如果用户请求的路径合法(以默认的.action为后缀或者没有后缀),那么就会进入到框架中处理,进入之前还会经过一系列的拦截器

才会转到action类处理

如果路径不合法,那么此次请求失败

 

7.指定多个struts配置文件

<include file=”XXX.xml” />    分模块的管理

 

8. 动态指定调用action中的某个方法

有两种方式:第一种,动态方法调用,但是已经不建议使用

如果想关闭动态方法调用,可以在struts.xml中配置

<constant name=”struts.enable.DynamicMethodInvocation” value=”false”>

 

第二种方式,使用通配符定义action

action name=”helloworld_*”   method=”{1}”

如果请求的url尾是helloworld_execute,调用的是execute方法

如果是 helloworld_other,调用的是 other 方法

 

测试:

文档结构图

 

 代码展示:

XML 部分
[web.xml]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

[struts.xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="strutspackage.xml"></include>
</struts>

[strutspackage.xml]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <package name="base" extends="struts-default">
        <global-results><!-- 全局视图 -->
            <result name="success">/sucess.jsp</result>
        </global-results>
        <action name="base">
            <result name="success">/base.jsp</result>
        </action>
    </package>

    <package name="yinger" namespace="/yinger" extends="struts-default">
        <action name="helloworld_*" class="com.yinger.HelloWorld"
            method="{1}">
            <param name="actionPara">{1}</param>
            <result name="message">/message.jsp</result>
        </action>
        <action name="helloworld" class="com.yinger.HelloWorld"
            method="helloworld">
            <result name="helloworld">/helloworld.jsp</result>
        </action>
    </package>

    <package name="redirect" extends="struts-default">
        <action name="redirect" class="com.yinger.HelloWorld"
            method="redirect">
            <result name="redirect" type="redirect">/helloworld.jsp?${message}</result>
        </action>
    </package>
    
    <package name="plaintext" extends="struts-default">
        <action name="plaintext" class="com.yinger.HelloWorld"
            method="plaintext">
            <result name="plaintext" type="plainText">
                <param name="location">/plainText.jsp</param>
                <param name="charSet">GBK</param>
            </result>
        </action>
    </package>
    
    <package name="itcast" namespace="/itcast" extends="base">
        <action name="itcast" class="com.itcast.ItCast" method="execute">
            <result name="message">/message.jsp</result>
        </action>
        <action name="redirectAction_samePackage" class="com.itcast.ItCast"
            method="redirectAction_samePackage">
            <result name="redirectAction_samePackage" type="redirectAction">
                <param name="actionName">itcast</param>
                <param name="namespace">/itcast</param>
            </result>    
        </action>
        <action name="redirectAction_otherPackage" class="com.itcast.ItCast"
            method="redirectAction_otherPackage">
            <result name="redirectAction_otherPackage" type="redirectAction">
                <param name="actionName">helloworld</param>
                <param name="namespace">/yinger</param>
            </result>
        </action>
    </package>

</struts>

  

Java部分
HelloWorld.java

package com.yinger;

public class HelloWorld {
    private String message;
    private String actionPara;
    
    public String helloworld(){
        message = "HelloWorld.helloworld!";
        return "helloworld";
    }
    
    public String hello(){
        message = "HelloWorld.hello!";
        return "message";
    }

    public String execute(){
        message = "HelloWorld.execute!";
        return "message";
    }
    
    public String redirect(){
        message = "HelloWorld.redirect!";
        return "redirect";
    }
    
    public String plaintext(){
        message = "HelloWorld.plaintext";
        return "plaintext";
    }
    
    public String getActionPara() {
        return actionPara;
    }

    public void setActionPara(String actionPara) {
        this.actionPara = actionPara;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

}


ItCast.java

package com.itcast;

public class ItCast {
    private String message;
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String execute(){
        message = "ItCast.execute!";
        return "message";
    }
    
    public String redirectAction_samePackage(){
        message = "redirectAction_samePackage";
        return "redirectAction_samePackage";
    }
    
    public String redirectAction_otherPackage(){
        message = "redirectAction_otherPackage";
        return "redirectAction_otherPackage";
    }
}

  

JSP部分
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
    index.jsp <br>
  </body>
</html>

base.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'sucess.jsp' starting page</title>
    
  </head>
  
  <body>
    base--->success! <br>
  </body>
</html>

success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'sucess.jsp' starting page</title>
    
  </head>
  
  <body>
    sucess <br>
  </body>
</html>

helloworld.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'helloworld.jsp' starting page</title>
  </head>
  
  <body>
    HelloWorld.jsp! <br>
    ${message }
  </body>
</html>

message.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'message.jsp' starting page</title>
  </head>
  
  <body>
    ${message} <br />
    ${actionPara }<br/>
  </body>
</html>


plainText.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'plainText.jsp' starting page</title>
    
  </head>
  
  <body>
    
  </body>
</html>

   

测试结果:

1.http://localhost:8080/struts2test/base

 

2. http://localhost:8080/struts2test/yinger/helloworld_hello

 

3.http://localhost:8080/struts2test/yinger/helloworld_execute

 

4.http://localhost:8080/struts2test/yinger/helloworld

 

5.http://localhost:8080/struts2test/itcast/redirectAction_samePackage—>http://localhost:8080/struts2test/itcast/itcast

 

6.http://localhost:8080/struts2test/itcast/redirectAction_otherPackage—>http://localhost:8080/struts2test/yinger/helloworld

 

7.http://localhost:8080/struts2test/redirect/redirect—>http://localhost:8080/struts2test/helloworld.jsp?HelloWorld.redirect!

 

8.http://localhost:8080/struts2test/plaintext/plaintext

 

9.http://localhost:8080/struts2test/itcast/itcast

 

10.http://localhost:8080/struts2test/itcast/1234/5678/itcast

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值