日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【SSH】——Struts2中的动态方法调用(一)

發(fā)布時(shí)間:2024/4/17 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【SSH】——Struts2中的动态方法调用(一) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


首先我們來看一個(gè)簡單的調(diào)用:

? ? ? ?1、在web.xml中配置攔截器StrutsPrepareAndExecuteFilter。StrutsPrepareAndExecuteFilter實(shí)現(xiàn)了filter接口,在執(zhí)行action之前,利用filter做一些操作。

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><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> </web-app>

? ? ? ?2、提供Struts2的配置文件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><package name="Struts2_006" extends="struts-default" ><action name="user" class="com.struts2.UserAction"><result>/add_success.jsp</result></action></package></struts>
注:<result>標(biāo)簽的默認(rèn)值是success,此處省略。

? ? ? ?3、頁面顯示部分。

index.jsp頁面,轉(zhuǎn)向到action中,調(diào)用action中的方法。

<body><a href="user.action">調(diào)用</a> </body>
調(diào)用完后,跳轉(zhuǎn)到成功頁面,并顯示message中的消息。

<body>我的操作:${message} <br></body>

? ? ? ?4、編寫Action類 UserAction。

public class UserAction extends ActionSupport{//消息字符串,用來顯示調(diào)用結(jié)果private String message;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}/**** execute方法*/public String execute() throws Exception{message="執(zhí)行execute方法";return SUCCESS;} }
? ? ? ?注意:這里我們讓UserAction繼承自ActionSupport類,從源碼中可以看到ActionSupport類實(shí)現(xiàn)了Action接口。在ActionSupport類中也處理了execute()方法,但他并沒有做什么操作,只是返回SUCCESS。因而,如果我們?cè)赨serAction中不寫execute方法,也不會(huì)報(bào)錯(cuò)。

public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {protected static Logger LOG = LoggerFactory.getLogger(ActionSupport.class);private final ValidationAwareSupport validationAware = new ValidationAwareSupport();private transient TextProvider textProvider;private Container container;/*** A default implementation that does nothing an returns "success".* <p/>* Subclasses should override this method to provide their business logic.* <p/>* See also {@link com.opensymphony.xwork2.Action#execute()}.** @return returns {@link #SUCCESS}* @throws Exception can be thrown by subclasses.*/public String execute() throws Exception {return SUCCESS;} }


? ? 如果在UserAction中不寫execute方法,message中沒有值。




? ? ? ? ? 這篇博客介紹了Struts2的簡單的方法調(diào)用,下篇博客將繼續(xù)介紹,當(dāng)action中有多個(gè)方法時(shí),應(yīng)該如何實(shí)現(xiàn)調(diào)用。


轉(zhuǎn)載于:https://www.cnblogs.com/saixing/p/6730265.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的【SSH】——Struts2中的动态方法调用(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。