struts2 xml文件配置

本文详细介绍了Struts2框架的基本依赖库、web.xml配置、struts.xml配置及两个额外配置文件的使用方法,包括过滤器、安全性、认证方式、错误异常配置和访问跳转配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方便自己在工作时查阅下,所以这样喽:

1. struts2依赖jar(最基本)文件:

    commons-logging-*.jar
    ognl-*.jar
    struts2-core-*.jar
    xwork-*.jar
    freemarker-*.jar

    (*表示version)

2. web.xml:

     <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
        <init-param>

            <!-- use annonation -->
            <param-name>actionPackages</param-name>
            <param-value>com.mkk.action.annonation</param-value>
        </init-param>

    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 配置安全 -->
    <security-constraint>
        <!-- 安全的资源信息 -->
        <web-resource-collection>
            <web-resource-name>resourceName</web-resource-name>
            <url-pattern>/resource/list.action</url-pattern>
            <http-method>POST</http-method>
            <http-method>GET</http-method>
        </web-resource-collection>
        <!-- 配置角色(必须与服务器上的配置一致)  -->
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
        <user-data-constraint>
            <transport-guarantee>NONE</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

    <!-- 访问时认证方式,可选有BASIC,FORM、CLIENT-CERT、DIGEST -->
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>input auth</realm-name>
    </login-config>
    <!-- 角色名称 -->
    <security-role>
        <role-name>admin</role-name>
    </security-role>

    <!-- 错误异常配置 -->
    <error-page>
        <error-code>404</error-code>
        <location>/error/error.jsp</location>
    </error-page>

    <!--
        <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/error/exception.jsp</location>
        </error-page>
    -->

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

</web-app>

3. struts.xml:

  <?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>
    <include file="struts-resource.xml"/>
    <include file="struts-action.xml"/>
    <include file="struts-aware.xml"/>
    <include file="struts-ognl.xml"/>
    <!--设置模式为开发模式 constant标签用于配置属性
    <constant name="struts.devMode" value="true" />
    -->

   <package name="first"  extends="struts-default" >
  
           <!-- 默认action跳转 -->
           <default-action-ref name="exception"/>
            <action name="exception">
                <result type="redirect">/error/exception.jsp</result>
            </action>

        <action name="login" class="com.mkk.action.LoginAction">
            <result name="success" type="redirect">/success.jsp</result>
            <result name="error">/fail.jsp</result>
            <!-- 拦截器配置(可自定义) -->
            <interceptor-ref name="timer"/>
            <interceptor-ref name="defaultStack"/>
        </action>
       
        <!-- forward action 转发跳转 -->
        <action name="result">
            <result type="redirect">/result/result.jsp</result>
        </action>
       
       
    </package>
   

</struts>

 

>>>>another struts.xml:

 

<?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>

    <package name="login_package" extends="struts-default">
        <!-- default action: return 404 -->
        <default-action-ref name="default_action" />
        <action name="default_action">
            <result type="redirect">/error/404.jsp</result>
        </action>
        <!-- login action -->
        <action name="login" class="com.mkk.struts2.action.LoginAction">
            <result name="SUCC" type="redirect">/jsp/main.jsp</result>
            <result name="FAIL">/default.jsp</result>
        </action>
        <!-- home action: return home page -->
        <action name="home">
            <result type="redirect">/</result>
        </action>
    </package>

    <!-- Multi actions -->
    <package name="multi_action" extends="struts-default"
        namespace="/multi">
        <!-- first type -->
        <action name="multi_add" class="com.mkk.struts2.action.MultiAction"
            method="add">
            <result>/multi/add.jsp</result>
        </action>
        <action name="multi_clear" class="com.mkk.struts2.action.MultiAction"
            method="clear">
            <result>/multi/clear.jsp</result>
        </action>
        <action name="multi_update" class="com.mkk.struts2.action.MultiAction"
            method="update">
            <result>/multi/update.jsp</result>
        </action>
        <action name="multi_select" class="com.mkk.struts2.action.MultiAction"
            method="select">
            <result>/multi/select.jsp</result>
        </action>
        <!-- second type -->
        <action name="multi" class="com.mkk.struts2.action.MultiAction2">
            <result>/multi/multi.jsp</result>
        </action>
        <!-- third type -->
        <action name="multi2_*" class="com.mkk.struts2.action.MultiAction3"
            method="{1}">
            <result>/multi/{1}.jsp</result>
        </action>
        <!--
            pattern all actions.like add_User.action
        <action name="*_*" class="com.mkk.struts2.action.{2}Action" method="{1}">
            <result>/multi/{1}.jsp</result> </action> -->
    </package>


</struts>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值