【SSH】——Struts2中的动态方法调用(一)
生活随笔
收集整理的這篇文章主要介紹了
【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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用java实现zip压缩
- 下一篇: 谁是谁的first-child