Struts2 Action

  具体实体的返回可以有用户自己定义的Action来决定

  具体的手段是根据返回的字符串找到对应的配置项,来决定实体的内容

  具体Action的实现可以是一个普通的java类,里面有public String execute方法即可

或者实现Action接口

  不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法

 ----------------------------------------------Hongten-------------------------------------------

新建web project:struts2_0300_Action

Build Path

 ----------------------------------------------Hongten-------------------------------------------

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>
    <constant name="struts.devMode" value="true" />
    <package name="front" extends="struts-default" namespace="/">
        <action name="index" class="com.bjsxt.struts2.front.action.IndexAction1">
            <result name="success">/ActionIntroduction.jsp</result>
        </action>
    </package>

</struts>

 ----------------------------------------------Hongten-------------------------------------------

当我们访问这个action的时候,他会去找对应的class:com.bjsxt.struts2.front.action.IndexAction1

在IndexAction1这个类中找到execute()方法,返回一个字符串。如:success,input,login,none

如果我们这里没有class,则他默认的会去调用:com.opensymphony.xwork2.Action这个接口中的execute()方法

还有如果我们在struts.xml中的result中没有写name="success",他 的默认也是<result name="success">

 ----------------------------------------------Hongten-------------------------------------------

在做测试的时候,分别修改:class="com.bjsxt.struts2.front.action.IndexAction2"

            和class="com.bjsxt.struts2.front.action.IndexAction3"

 在上面的三个测试当中,得到的是同样的结果;

 ----------------------------------------------Hongten-------------------------------------------

那么三种方法有什么区别呢?我们应该使用那一种方法呢?

我们在真正开发过程中就用:class="com.bjsxt.struts2.front.action.IndexAction3"这种方法;

              其他的都不用;就是从ActionSupport继承,

              原因是:

              ActionSupport已经

              帮我们封装了一系列可以直接调用的特别方便的方法;有很多方法封装在其中,

              我们在子类中可以直接拿来就用了

 ----------------------------------------------Hongten-------------------------------------------

IndexAction.java

代码:

package com.bjsxt.struts2.front.action;

public class IndexAction1 {
 public String execute() {
  return "success";
 }
}

 ----------------------------------------------Hongten-------------------------------------------

IndexAction.java

代码:

package com.bjsxt.struts2.front.action;

import com.opensymphony.xwork2.Action;

public class IndexAction2 implements Action {
 @Override
 public String execute() {
  return "success";
 }
}

 ----------------------------------------------Hongten-------------------------------------------

IndexAction.java

代码:

package com.bjsxt.struts2.front.action;

import com.opensymphony.xwork2.ActionSupport;

public class IndexAction3 extends ActionSupport {
 
 @Override
 public String execute() {
  return "success";
 }
}

 ----------------------------------------------Hongten-------------------------------------------

ActionIntroduction.jsp

代码:

<?xml version="1.0" encoding="GB18030" ?>
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030" />
<title>Insert title here</title>
</head>
<body>
具体视图的返回可以由用户自己定义的Action来决定<br />
具体的手段是根据返回的字符串找到对应的配置项,来决定视图的内容<br />
具体Action的实现可以是一个普通的java类,里面有public String execute方法即可<br />
或者实现Action接口<br />
不过最常用的是从ActionSupport继承,好处在于可以直接使用Struts2封装好的方法<br />
</body>
</html>



 

原文地址:https://www.cnblogs.com/hongten/p/2122028.html