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

1.Action 名称的搜索顺序

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

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

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

4_1

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

action的class--->ActionSupport

action的method--->execute

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

4_2

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

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

②redirect:浏览器重定向

③redirectAction:重定向到另一个action

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

5_1

4.为action属性注入值

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

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

7_1

5.指定struts2的请求

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

8_1

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

建议在struts.xml中配置

8_2

几种常见的常量配置介绍

8_3

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类处理

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

9_1

7.指定多个struts配置文件

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

10_1

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

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

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

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

11_1

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

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

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

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

11_2

测试:

文档结构图

01

 代码展示:

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

02

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

03

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

04

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

05

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

06

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

07

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

08

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

09

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

10

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

10

原文地址:https://www.cnblogs.com/yinger/p/2116707.html