Struts2的Action编写
Action的編寫的方式:
第一種方式:
Struts2的Action編寫的最簡(jiǎn)單的方式就是寫一個(gè)普通類,不繼承自任何類,也不實(shí)現(xiàn)接口。如下:
1 package cn.geore.action; 2 3 public class OneAction { 4 /** 5 * 在Servlet中每次執(zhí)行的是service方法,而在Struts2種每次調(diào)用執(zhí)行的方法是execute() 6 * 因此對(duì)于具體的功能只需要卸載execute()中即可 7 * @return 8 */ 9 public String execute() { 10 return "first"; 11 } 12 13 public String add() { 14 return "add"; 15 } 16 }?
第二種方式:
創(chuàng)建一個(gè)普通類,然后實(shí)現(xiàn)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??梢哉{(diào)用它,也可以在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?
第三種方式:
創(chuàng)建類,繼承父類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類方法的訪問:
對(duì)于Action類的方法,在默認(rèn)的情況下,在每次執(zhí)行的時(shí)候,默認(rèn)訪問的多是execute()方法,如果要訪問其他的方法,Struts2提供了三種方式進(jìn)行方法的訪問。對(duì)于Action類的方法,如果又返回值的時(shí)候就必須是String類型。如果方法不返回,可以使用void修飾,但是不建議這么寫,一般使用return "none";表示返回為空,如下:
1 // 無返回值的時(shí)候建議這樣子定義 2 public String add() { 3 // ...... 4 return Action.NONE; 5 } 6 7 // 有返回值的時(shí)候,返回值必須為String類型 8 public String update() { 9 // ...... 10 return Action.SUCCESS; 11 }
定義一個(gè)BookAction類,在使用下面的三種方式實(shí)現(xiàn)對(duì)這個(gè)類方法的訪問:
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標(biāo)簽的method屬性決定調(diào)用Action類的哪一個(gè)方法。
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>運(yùn)行截圖:
?
第二種方式:使用通配符的方式實(shí)現(xiàn)方法的訪問
? 對(duì)于第一種方式,我們?cè)趐ackage標(biāo)簽中,要配置action,每一個(gè)方法均要配置一個(gè)action,如果對(duì)于一個(gè)很多方法的開發(fā),那么就要寫非常多的action配置。那么這樣寫無疑是比較麻煩的,而Struts2的通配符方式就可以解決這個(gè)問題。
使用的方式:在action標(biāo)簽的name屬性,給name屬性的值一個(gè)*號(hào)(星號(hào)表示匹配任意的內(nèi)容)。
<?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_*;這個(gè)表示action接收所有以book_開始的任意的字符串method中的{1},表示取得第一個(gè)占位符的值,也就是去的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>
第三種方式:動(dòng)態(tài)訪問的方式訪問方法
?
轉(zhuǎn)載于:https://www.cnblogs.com/geore/p/7526934.html
總結(jié)
以上是生活随笔為你收集整理的Struts2的Action编写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数字转换大写核心类
- 下一篇: 什么是ioc(控制反转)什么是di(依赖