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屬性的值一個*號(星號表示匹配任意的內容)?! ?/p> <?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>
第三種方式:動態訪問的方式訪問方法
?
轉載于:https://www.cnblogs.com/geore/p/7526934.html
總結
以上是生活随笔為你收集整理的Struts2的Action编写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数字转换大写核心类
- 下一篇: 什么是ioc(控制反转)什么是di(依赖