struts2初学笔记

struts2在ssh中扮演的主要角色就是对请求的拦截和分发,相当于控制器的功能。struts2的配置需要定义一个struts.xml文件,这个文件主要描述拦截配置,比如拦截指定.action请求并分发到指定类的指定方法进行处理,处理完成后再有方法返回的字符串根据对应节点进行跳转。

例:
struts.xml



<struts>
    
    <constant name="struts.action.extension" value="action" />
    
    <constant name="struts.configuration.xml.reload" value="true" />
    
    <constant name="struts.devMode" value="true" />

    <package name="default" extends="struts-default">
        <action name="index">
            <result >/index.jspresult>
        action>
        
        <action name="login" class="com.emiya.sshFreamWork.action.UsrAction" method="login">
            
            <result name="success">/stutsActn/welcome.jspresult>
            <result name="notfound">/index.jspresult>
        action>

        <action name="mdfUsr" class="com.emiya.sshFreamWork.action.UsrAction" method="mdfPwd">
            <result name="mdfOk">/index.jspresult>
            <result name="mdfFail">/usrMngr/fail.jspresult>
        action>

        <action name="usrOut" class="com.emiya.sshFreamWork.action.UsrAction" method="usrLoginOut">
            <result name="out">/index.jspresult>
        action>

        <action name="delUsr" class="com.emiya.sshFreamWork.action.UsrAction" method="delUsr">
            <result name="delOk">/stutsActn/welcome.jspresult>
        action>

        <action name="sentBlog" class="com.emiya.sshFreamWork.action.blogAction" method="sntBlog">
            <result name="sentOk">/stutsActn/welcome.jspresult>
            <result name="sentErro">/stutsActn/sntBlogErro.jspresult>
        action>

        <action name="mdfBlog" class="com.emiya.sshFreamWork.action.blogAction" method="mdfBlog">
            <result name="mdfOk">/stutsActn/welcome.jspresult>
            <result name="mdfErro">/stutsActn/welcome.jspresult>
        action>


    package>

struts>

配置完拦截信息之后,还需要再web,xml中加入:

web.xml


<filter>
    <filter-name>struts2filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>
        
        <init-param>  
            <param-name>configparam-name>  
            
            <param-value>struts-default.xml,struts-plugin.xml,../strutsConfig/struts.xmlparam-value>
        init-param> 
filter>
    
    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

你可能感兴趣的:(struts2初学笔记)