Struts2执行流程
应用程序启动,进入strutspreparAndExcuteFilter 类,进入配置 文件struts.xml,xml 文件配置了请求的路径和请求的类型
里面的一些参数:
默认的类型为:dispatcher类型,跳转的是页面
redirectAction :跳转的为其他的action,使用param ${param}传递参数
stream:用于文件导出,导入操作
chain:用于action之间的条转
redirect:用于页面和action之间的跳转
json:输出的文件格式为json格式
注意:
redirect表示客户端重定向,用于重定向到某个页面。
redirectAction也表示客户端重定向,用于重定向到某个Action
chain表示服务端请求转发,用于多个Action之间数据信息的共享。
redirect和redirecAction类似于response.sendRedirect(url);
chain类似于request.getRequestDispatcher(url).forward(request, response);
根据nameplace 匹配前端url来路径,进入配置的class类里面,根据action里面的name和class
进入匹配的方法,执行controller层的逻辑,返回不同的常量 :
SUCCESS:Action正确的执行完成,返回相应的视图,success是name属性的默认值。
NONE:表示Action正确的执行完成,但并不返回任何视图。
ERROR:表示Action执行失败,返回到错误处理视图。
INPUT:Action的执行,需要从前端界面获取参数,INPUT就是代表这个参数输入的界面,一般在应用中,会对这些参数进行验证,如果验证没有通过,将自动返回到该视图。
LOGIN:Action因为用户没有登陆的原因没有正确执行,将返回该登陆视图,要求用户进行登陆验证。
,在根据返回值,进入不同的页面或者进入不同的action
参考例子:
<?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>
<constant name="struts.multipart.maxSize" value="10485760000"/>
<package name="global" extends="struts-default">
<interceptors>
<interceptor-stack name="globalStack">
<interceptor-ref name="exception">
<param name="logEnabled">true</param>
<param name="logLevel">info</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="globalStack"/>
<global-results>
<result name="error">/common/error.jsp</result>
<result name="warning">/common/warning.jsp</result>
</global-results>
<!-- 定义所有的全局异常映射 -->
<global-exception-mappings>
<exception-mapping exception="com.sse.report.core.exception.ThrowedException"
result="warning" />
</global-exception-mappings>
</package>
<package name="report" namespace="/" extends="global">
<action name="report" class="reportAction" method="report">
<result type="chain">
<param name="actionName">${appId}_report</param>
<param name="namespace">/app</param>
</result>
</action>
<action name="dynamicReport" class="reportAction" method="dynamicReport">
<result>
/common/dynamic_report.jsp
</result>
<result name="uploadDisplay" type="chain">
<param name="actionName">uploadDisplay</param>
<param name="namespace">/</param>
</result>
<result name="upload" type="chain">
<param name="actionName">upload</param>
<param name="namespace">/</param>
</result>
</action>
<action name="customReport" class="reportAction" method="customReport">
<result>
/app/report/${appId}.jsp
</result>
</action>
<action name="display" class="reportAction" method="display">
<result type="chain">
<param name="actionName">${appId}_display</param>
<param name="namespace">/app</param>
</result>
</action>
<action name="dynamicDisplayRs" class="reportAction" method="dynamicDisplayRs">
<result type="chain">
<param name="actionName">${appId}_dynamicDisplayRs</param>
<param name="namespace">/app</param>
</result>
</action>
<action name="dynamicDisplay" class="reportAction" method="dynamicDisplay">
<result>
/common/dynamic_display.jsp
</result>
</action>
<action name="xDisplay" class="reportAction" method="xDisplay">
<result name="success">
/common/x_display.jsp
</result>
<result name="xExport" type="redirectAction">
<param name="actionName">xExport</param>
<param name="appId">${appId}</param>
<param name="uuid">${uuid}</param>
<param name="type">${exportType}</param>
</result>
</action>
<action name="xDisplayPiece" class="reportAction" method="xDisplayPiece">
<result name="success">
/common/x_display_piece.jsp
</result>
</action>
<action name="xExport" class="xExportAction">
<result type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
</result>
</action>
<action name="flex" class="reportAction" method="flex">
<result>
/common/flex.jsp
</result>
</action>
<action name="export" class="exportAction">
<result type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
</result>
</action>
<action name="docRemark" class="reportAction" method="docRemark">
<result type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
</result>
</action>
<action name="tableDisplay" class="tablePagingAction" method="pading">
<result>
/common/dynamic_table.jsp
</result>
</action>
<!-- 文档输出模块应用 -->
<action name="dm" class="reportAction" method="dataModule">
<result type="chain">
<param name="actionName">dm</param>
<param name="namespace">/app</param>
</result>
</action>
<action name="upload*" class="uploadReportAction">
<result>
/common/upload{1}_report.jsp
</result>
</action>
</package>
<!-- 浏览器兼容相关配置 -->
<package name="browser" namespace="/browser" extends="global">
<action name="compatible">
<result>/browser/compatible.html</result>
</action>
<action name="download" class="browserAction" method="download">
<result name="file" type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
</result>
<result name="file_not_fund">
/common/fileNotFund.jsp
</result>
</action>
</package>
<package name="json" namespace="/" extends="json-default">
<action name="textUpload" class="textUploadAction">
<result type="json">
<param name="root">result</param>
<param name="contentType">text/html</param>
</result>
</action>
<action name="search" class="searchAction">
<result type="json">
<param name="root">result</param>
<param name="contentType">text/html</param>
</result>
</action>
<action name="getBondsByType" class="bondInfoAction" method="getBondsByType">
<result type="json">
<param name="root">result</param>
<param name="contentType">text/html</param>
</result>
</action>
<!-- hkk 6/30 -->
<action name="*_json_*" class="{1}Action" method="{2}">
<result type="json">
<param name="root">result</param>
<param name="contentType">text/html</param>
</result>
</action>
</package>
<!-- portal add by lzq -->
<package name="portal-login-json" namespace="/portalLogin" extends="json-default">
<action name="*_json_*" class="{1}Action" method="{2}">
<result type="json">
<param name="root">result</param>
<param name="contentType">text/html</param>
</result>
</action>
</package>
<package name="portal-login" namespace="/portalLogin" extends="global">
<action name="browser">
<result>/portal/browser.html</result>
</action>
</package>
<package name="portal-json" namespace="/portal" extends="json-default">
<action name="*_json_*" class="{1}Action" method="{2}">
<result type="json">
<param name="root">result</param>
<param name="contentType">text/html</param>
</result>
</action>
<action name="upload*" class="UploadAction" method="{1}">
<result type="json">
<param name="contentType">text/html</param>
</result>
</action>
</package>
<package name="portal" namespace="/portal" extends="global">
<action name="index" class="IndexAction" method="portalInput">
<result>/portal/index.html</result>
</action>
<action name="admin">
<result>/portal/admin.html</result>
</action>
<action name="notAuthority">
<result>/portal/notAuthority.html</result>
</action>
<action name="signout">
<result>/portal/signout.html</result>
</action>
<action name="login">
<result>/portal/login.html</result>
</action>
<action name="user">
<result>/portal/user.html</result>
</action>
<action name="password">
<result>/portal/password.html</result>
</action>
<action name="help">
<result>/portal/help.html</result>
</action>
<action name="enter">
<result>/portal/enter.html</result>
</action>
<action name="system_frame_*">
<result>/portal/system/{1}.html</result>
</action>
</package>
<package name="app-json" namespace="/app" extends="json-default">
<action name="*_json_*" class="{1}ReportAction" method="{2}">
<result type="json">
<param name="root">result</param>
<param name="contentType">text/html</param>
</result>
</action>
</package>
<package name="app" namespace="/app" extends="global">
<action name="*_report" class="{1}ReportAction" method="reportInput">
<result>
/app/report/{1}.jsp
</result>
</action>
<action name="*_son_report_*" class="{1}ReportAction" method="reportSonInput">
<result>
/app/report/{1}_{2}.jsp
</result>
</action>
<action name="*_html_report_*" class="{1}ReportAction" method="reportHtmlInput">
<result>/app/report/{1}_{2}.html</result>
</action>
<action name="*_dynamicDisplayRs" class="{1}ReportAction" method="diy">
<result>
/common/dynamic_display.jsp
</result>
</action>
<action name="*_display" class="{1}ReportAction">
<result>/app/display/{1}_display.jsp</result>
</action>
<action name="*_region_*" class="{1}ReportAction" method="{2}">
<result>/app/display/{1}_region_{2}.jsp</result>
</action>
<action name="*_export" class="{1}ReportAction" method="export">
<result type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
</result>
<result name="file" type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
</result>
<result name="file_not_fund">
/common/fileNotFund.jsp
</result>
</action>
<!-- hkk 6/30 -->
<action name="dm_*" class="DMReportAction" method="{1}">
<result>/app/report/dm/{1}.jsp</result>
<result name="download" type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
</result>
</action>
</package>
<package name="dm" namespace="/" extends="global">
<action name="nav" class="DMReportAction">
<result>/mod/dm/nav.jsp</result>
</action>
</package>
<package name="olap" namespace="/" extends="global">
<action name="toOlap" class="reportAction" method="olap">
<result type="chain">
<param name="actionName">${appId}Olap</param>
<param name="namespace">/</param>
</result>
</action>
<action name="olap_*" class="reportAction" method="olap">
<result type="chain">
<param name="actionName">${appId}Olap_{1}</param>
<param name="namespace">/</param>
</result>
</action>
<action name="*Olap" class="{1}OlapAction">
<result name="success">
/olap/olap.jsp
</result>
<result name="error">
/olap/olap_error.jsp
</result>
</action>
<action name="*Olap_*" class="{1}OlapAction" method="{2}">
<result>
/olap/olap_report.jsp
</result>
</action>
<action name="*OlapExport_*" class="{1}OlapAction" method="{2}">
<result type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
</result>
</action>
</package>
<package name="doc" namespace="/" extends="global">
<action name="toDocSearch" class="reportAction" method="docSearch">
<result type="chain">
<param name="actionName">${appId}DocSearch</param>
<param name="namespace">/</param>
</result>
</action>
<action name="*DocSearch" class="{1}DocSearchAction">
<result>
/common/doc_search.jsp
</result>
</action>
<action name="*DocExport" class="{1}DocSearchAction" method="export">
<result type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
</result>
</action>
<action name="*DocExportByHand" class="{1}DocSearchAction" method="exportByHand">
<result type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
</result>
</action>
</package>
<package name="upload" namespace="/upload" extends="global">
<action name="*File" class="uploadReportAction" method="{1}File">
<result type="stream">
<param name="allowCaching">false</param>
<param name="inputName">fileStream</param>
</result>
</action>
<action name="*" class="uploadReportAction" method="{1}">
<result>/common/upload_{1}.jsp</result>
</action>
</package>
<package name="admin" namespace="/admin" extends="json-default">
<action name="index">
<result>
/admin/index.jsp
</result>
</action>
<action name="*_region_*" class="{1}Action" method="{2}">
<result>/admin/{1}_region_{2}.jsp</result>
</action>
<action name="*_*" class="{1}Action" method="{2}">
<result type="json">
<param name="root">result</param>
<param name="contentType">text/html</param>
</result>
</action>
</package>
</struts>