Struts2的Action编写

Action的编写的方式:

第一种方式:

  Struts2的Action编写的最简单的方式就是写一个普通类,不继承自任何类,也不实现接口。如下:

 1 package cn.geore.action;
 2 
 3 public class OneAction {
 4     /**
 5      * 在Servlet中每次执行的是service方法,而在Struts2种每次调用执行的方法是execute()
 6      * 因此对于具体的功能只需要卸载execute()中即可
 7      * @return
 8      */
 9     public String execute() {
10         return "first";
11     }
12     
13     public String add() {
14         return "add";
15     }
16 }

 

第二种方式:

  创建一个普通类,然后实现Action接口。

 1 package cn.geore.action;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 
 5 public class TwoAction implements Action {
 6 
 7     @Override
 8     public String execute() throws Exception {
 9         return "success";
10     }
11 
12 }

  Action接口的常量值:

 1  /**
 2      * 成功,返回sucess。可以调用它,也可以在Action类中的execute方法中直接return "success";
 3      */
 4     public static final String SUCCESS = "success";
 5 
 6     /**
 7      * The action execution was successful but do not
 8      * show a view. This is useful for actions that are
 9      * handling the view in another fashion like redirect.
10      */
11     public static final String NONE = "none";
12 
13     /**
14      * The action execution was a failure.
15      * Show an error view, possibly asking the
16      * user to retry entering data.
17      */
18     public static final String ERROR = "error";
19 
20     /**
21      * The action execution require more input
22      * in order to succeed.
23      * This result is typically used if a form
24      * handling action has been executed so as
25      * to provide defaults for a form. The
26      * form associated with the handler should be
27      * shown to the end user.
28      * <p/>
29      * This result is also used if the given input
30      * params are invalid, meaning the user
31      * should try providing input again.
32      */
33     public static final String INPUT = "input";
34 
35     /**
36      * The action could not execute, since the
37      * user most was not logged in. The login view
38      * should be shown.
39      */
40     public static final String LOGIN = "login";
View Code

 

第三种方式:

  创建类,继承父类ActionSupport

 1 package cn.geore.action;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 import com.opensymphony.xwork2.ActionSupport;
 5 
 6 public class ThreeAction extends ActionSupport {
 7     @Override
 8     public String execute() throws Exception {
 9         // .....
10         return Action.SUCCESS;
11     }
12 }

 

Action类方法的访问:

  对于Action类的方法,在默认的情况下,在每次执行的时候,默认访问的多是execute()方法,如果要访问其他的方法,Struts2提供了三种方式进行方法的访问。对于Action类的方法,如果又返回值的时候就必须是String类型。如果方法不返回,可以使用void修饰,但是不建议这么写,一般使用return "none";表示返回为空,如下: 

 1 // 无返回值的时候建议这样子定义
 2 public String add() {
 3     // ......
 4     return Action.NONE;
 5 }
 6     
 7 // 有返回值的时候,返回值必须为String类型
 8 public String update() {
 9         // ......          
10     return Action.SUCCESS;
11 }

  

  定义一个BookAction类,在使用下面的三种方式实现对这个类方法的访问:

 1 package cn.geore.bookaction;
 2 
 3 import com.opensymphony.xwork2.Action;
 4 import com.opensymphony.xwork2.ActionSupport;
 5 
 6 public class BookAction extends ActionSupport {
 7     // 添加图书
 8     public String addBook() {
 9         System.out.println("添加图书...");
10         return Action.NONE;
11     }
12     
13     // 更新图书
14     public String updateBook() {
15         System.out.println("删除图书...");
16         return Action.NONE;
17     }
18 }

  第一种方式:在struts2的核心配置文件中,action标签的method属性决定调用Action类的哪一个方法。

  book.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- method配置 -->
    <package name="bookaction" extends="struts-default" namespace="/book">
        <action name="addBook" class="cn.geore.bookaction.BookAction" method="addBook">
            <!-- <result name="success">/jsps/one/addBook.jsp</result> -->
        </action>
        
        <action name="updateBook" class="cn.geore.bookaction.BookAction" method="updateBook">
            <!-- <result name="success">/jsps/one/updateBook.jsp</result> -->
        </action>
    </package>
</struts>

  struts.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <constant name="struts.i18n.encoding" value="UTF-8"></constant>
 7     <!-- 引入外部的Struts模块的配置文件 -->
 8     <!-- <include file="cn/geore/action/one.xml"></include> -->
 9     <include file="cn/geore/bookaction/book.xml"></include>
10 </struts>

运行截图:

 

  第二种方式:使用通配符的方式实现方法的访问

     对于第一种方式,我们在package标签中,要配置action,每一个方法均要配置一个action,如果对于一个很多方法的开发,那么就要写非常多的action配置。那么这样写无疑是比较麻烦的,而Struts2的通配符方式就可以解决这个问题。

  使用的方式:在action标签的name属性,给name属性的值一个*号(星号表示匹配任意的内容)。  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!-- 通配符配置 -->
    <package name="bookaction" extends="struts-default" namespace="/book">
        <!-- 
            name = book_*;这个表示action接收所有以book_开始的任意的字符串
            method中的{1},表示取得第一个占位符的值,也就是去的book_*的*所表示的值。
        -->
        <action name="book_*" class="cn.geore.bookaction.BookAction" method="{1}">
            <!-- 
                <result name="update">/jsps/one/updateBook.jsp</result>
                <result name="add">/jsps/one/addBook.jsp</result> 
            -->
        </action>
    </package>
</struts>

  

  第三种方式:动态访问的方式访问方法

   

  

posted @ 2017-09-15 16:04  Geore  阅读(340)  评论(0编辑  收藏  举报