struts2学习笔记(一)

来源:互联网 发布:三菱fx2n编程手册 编辑:程序博客网 时间:2024/06/07 22:46

初识struts2

Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet

strust2工作流程

strut2工作流程图

client–>请求(HttpServletRequest)–>(ActionContextCleanUp)过滤器 –> Other filters(SiteMesh ,etc) –> 调用FilterDispatcher核心控制器 –> 调用ActionMapper,确定请求Action –> 返回ActionMaping对象 –> FilterDispatcher将控制权委派给ActionProxy –> ActionProxy调用配置管理器(ConfigurationManager) –>读取配置信息(struts.xml) –> 创建ActionInvocation对象 –> 依次调用所有配置拦截器(Interceptor N) –> 执行Action –> 返回结果字符串给ActionInvocation –> 返回结果视图 –> 再次执行拦截器(Interceptor N)(顺序和Action执行之前相反 –> 最后响应(HttpServletResponse)被返回在web.xml中配置的那些过滤器和核心控制器(FilterDispatcher)–> client

自定义拦截器配置struts.xml

    <package name="my" extends="struts-default" namespace="/manage">        <interceptors>            <!-- 定义拦截器 -->            <interceptor name="拦截器名" class="拦截器实现类"/>            <!-- 定义拦截器栈 -->            <interceptor-stack name="拦截器栈名">                <interceptor-ref name="拦截器一"/>                <interceptor-ref name="拦截器二"/>            </interceptor-stack>        </interceptors>        ......    </package>

result type

dispatcher:用来转向页面,通常处理JSP,底层调用RequestDispatcher的forward()或include()方法,dispatcher是 type属性的默认值。localtion指定JSP的位置,parse如果为false表示location的值不会被当作 OGNL解析,默认为true

freemarker:用freemaker模板引擎呈现视图,location指定模板(*.ftl)的位置,parse如果为false,location的值不会被OGNL解析。contentType指定以何中类型解析,默认为text/html

httpheader:控制特殊HTTP行为的结果类型

velocity: 使用velocity模板输出结果,location指定模板的位置(*.vm),parse如果为false,location不被OGNL解析,默认为true。

xslt: 处理XML/XLST模

plainText:以原始文本显示JSP或者HTML,location指定文件的位置,charSet指定字符集

chain:将action的带着原来的状态请求转发到新的action

redirectAction: 重定向到一个Action

redirect: 重定向到一个URL,支持在配置文件中,读取并解析源Action中ValueStack的值,并成为参数传递到Redirect的地址中
example:

    <action name="Login" class="steven.actions.LoginAction">        <result name="success" type="redirect">User.action?u_id=${loginBean.u_id}</result>    </action>

stream:向浏览器发送InputStream对象,通常用来处理文件下载,还可用于返回AJA数据,直接向响应中发送原始数据,通常在用户下载时使用,contentType指定流的类型,默认为 text/plain,contentLength以byte计算流的长度,contentDisposition指定文件的位置,通常为 filename=”文件的位置”,input指定InputStream的名字,例如:imageStream,bufferSize指定缓冲区大小,默认为1024字节
example:

    <result name="success" type="stream">        <param name="contentType">image/jpeg</param>        <param name="inputName">imageStream</param>        <param name="contentDisposition">filename="document.pdf"</param>        <param name="bufferSize">1024</param>    </result>

redirect和redirectAction chain的区别
redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失。
redirect-action:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失。
chain:action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失。