开发者学堂课程【Java Web开发系列课程 - Struts2框架入门:struts2流程二】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/537/detail/7290
struts2流程二
一、流程
设置 browser 和 tomcat,browser 向 tomcet 发起一个请求(request),然后打开源码,当找不到源码时,点击 Attach Source,有两种办法,如果是文件夹就选择External Folder,如果是一个压缩文件就选择 Eternal File。
打开 Action.class
public interface Action{
the action eecution was successful.show result
view to the end suer
public static final String SUCCESS = "success";
The action execution was successful but do not
show a view. This is useful for actions that are
* handling the view in another fashion like redirect.
public static final String NONE = "none" ;
/**
The action execution was a failure.
关联完之后找到 StrutsPrepareAndExecuteFilter.class
,
以断点启动,进入浏览器,
网址输入http://localhost:8080/03struts2_login/login.jsp,程序设置
if ( excludedPatterns != null && prepare .
isUrlExcluded(request
chain. doFilter( request, response);
}else {
request = prepare . wrapRequest(request);
ActionMapping mapping = prepare. findActi onMapping(request ,
if (mapping == null) {
boolean handled = execute. executeStaticResourceRequest
( !handled){
chain.dofilter(request,respose
)
;
}
}else{
这时候 mapping 是 null,是空的,表明请求的 action 找不到了,返回 json 界面就直接请求对应资源。
返回浏览器发送登录请求的时候,就进来了。
进来的过程是只要 browser 发送请求就给 tomcat,tomcat 创建 request()对象,创建 HttpServletRequest。
tomcat 创建 request 对象()给 HttpServletResponse,tomcat doFilter 给StrutsPrepareAndExecuteFilter,
public void doFilter( ServletRequest req, ServletResponse res, FilterCh
HttpServletRequest request = (HttpServletRequest) req ;
HttpServletResponse response = (HttpServletResponse) res;
try
{
prepare . setEncodingAndLocale(request, response);
prepare . createActionContext(request, response)
prepare. assignDispatcherToThread();
if ( excludedPatterns :!= null && prepare.
isUrlExcluded(requestchain. doF ilter(request, response);
}
else
{
request = prepare . wrapRequest (request);
ActionMapping mapping = prepare.
findActionMapping( request, .
if (mapping ==null)
{
boolean handled = execute. executeStatic ResourceRequest
if (!handled)
{
在 Expression 中增加 name 为 request.getParameter(“pwd”),value 为1111。
过程中 StrutsPrepareAndExecuteFilte 向 pepareOperations 进行 findActionMapping(),prepareOperations 返回 ActionMapper()。
当 mapping 不等于 null 的时候,使用 executeAction 方法,下一步使用serviceAction()方法作用于 Disoatcher。
String name = mapping. getName();
String method = mapping. getMethod();
Configuration config = configurationManager .
getConfiguration( )ActionProxy proxy = config.
getContainer() . getInstance( ActionPr
namespace,name, method, extraContext, true, false);
request . setAttribute( ServletActionContext . STRUTS_ VALUESTACK_ KE'
Dispather 通过 getconfiguration 作用于 configurationmanager,
configurationmanager 通过 getinstance 作用于
defaultActionProxyFactory
public ActionProxy createActionProxy( ActionInvocation inv,
String name:
DefaultActionProxyproxy= new DefaultActionProxy(inv,
namespace ,
container . inject(proxy);proxy . prepare();
return proxy
defaultActionProxyFactory 通过 createActionProxy()作用于 DefaultActionProxy,根据 protected Configuration confi gurat ion;
protected ActionConfig config;
protected ActionInvocation invocation;
protected UnknownHandlerManager unknownHandlerManager;protected String actionName ;
protected String namespace;
protected String method;
DefaultActionProxyFactory 调用 ActionInvocation,由他来完成一系列处理。调用Result....
public DefaultActionInvocation(final Map<String, Object> extraContext, final boolean
pushActiDefaultActionInvocation. this. extraContext = extraContext;
DefaultActionInvocation. this . pushAction = pushAction;
@Inject
public void setUnknownHandl erManager
(UnknownHandlerManager unknownHandlerManager) fthis . unknownHandlerManager = unknownHandlerManager ;
@Inject
public void setValueStackFactory(ValueStackFactory fac) fthis. valueStackFactory = fac
结束这个程序之后,挨个进行返回。