源码分析之struts1自定义方法的使用与执行过程

最近有人问我,你做项目中用户的一个请求是怎么与struts1交互的,我说请求的url中包含了action的名字和方法名,这样就可以找到相应方法,执行并返回给用户了。 他又问,那struts1中有什么方法呢,execute;那怎么能调用到自定义的方法,在请求中指定,在struts-config.xml中配置。。。  接下来他疑惑了,说struts2 是可以以struts.xml中配置方法名的。 是,他说的没错,只是我没有表述清楚struts1自己定义方法的关键点。 下面具体说一下struts1中使用自定义方法及自定义方法执行的过程。

在struts1中我们知道核心的控制器是ActionServlet这个类,那么它负责了获取用户的请求,改变modle状态,返回用户试图,这是MVC模型的一个交互方式。那么他的内部实现是如何的呢? 通常我们在使用struts定义一个action中会继承MappingDispatchAction、LookupDispatchAction,或者直接继承DispatchAction,他们的区别这里暂不做解释。 这里我们直接继承DispatchAction类。如我们的类就叫LoginAction。先上代码

test.LoginAction

1 public class LoginAction extends DispatchAction{
2     public ActionForward login(ActionMapping mapping, ActionForm form,
3              HttpServletRequest resquest, HttpServletResponse response) throws Exception{
4         //TODO 逻辑
5         mapping.findForward("login");
6     }
7 }

struts-config.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 3 <struts-config>
 4  <form-beans>
 5     <form-bean name="loginFormBean" type="test.LoginActionForm" />
 6  </form-beans>
 7  <action-mappings>
 8     <action name="loginFormBean" parameter="method"
 9             path="/loginAction" scope="request"
10             type="test.LoginAction">
11             <forward name="login" path="/index.jsp" />
12     </action>
13   </action-mappings>
14 </struts-config>

login.jsp

 1 <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
 2 <html>
 3 <head>
 4     <script type="text/javascript">
 5         function subForm(){
 6             document.loginFormBean.submit();
 7         }
 8     </scipt>
 9 </head>
10 
11 <body>
12   <html:form action="/loginAction.do?method=login" method="post"
13     onsubmit="return subForm()">
14       <input type="text" name="name"/>
15       <input type="password" name="pwd"/>
16       <input type="button" value="submit" onclick="subForm"/>
17   </html:form>
18 </body>
19 </html>

现在就开始说下struts1是如何找到我们自定义的login这个方法的

我们首先看login.jsp,这个是我们首先访问的页面,它模拟一个登录的操作,那么输入用户名、密码,点击submit后,会执行subForm这个函数,这个函数要做的就是执行path为"/loginAction.do?method=login"的操作且用的是POST方式,这里看html:form中action的值。

当开始执行loginAction.do时,首先进入ActionServlet中的doPost方法(假设ActionServlet的初始化方法已经在server启动时执行了,那么也就把相关的配置包括struts-config的内容加载了进来),然后调用process方法,在process方法中首先有selectModule的过程(其实就是找到这个请求的url对应的actionMapping配置,并放到request中),在process方法的最后会调用RequestProcess的process方法,而process最终会调用到DispatchAction的execute方法,这个就是struts请求的默认执行方法了。而我们的LoginAction类中并没有execute方法,那它肯定没法执行了。

其实这里就要回头再看一下我们的请求url中是不是有个method=login这个参数,在struts-config.xml中对loginAction的配置中有个 parameter="method" 的配置。那现在在回到我们的DispatchAction的execute方法中,在这里,这里就会获会获取这个值(如果配置了),取到这个parateter的值为method。而拿到这个method后又做了什么,其实就是把method作为参数通过request.getParameter(method)获取到一个值,这个值是什么,其实就是请求的url中的login,这样,要执行的方法就获取到了,而再通过反射的机制,去调用login这个方法,从而完成对login方法请求,并返回视图给用户。

原文地址:https://www.cnblogs.com/blacksonny/p/4605358.html