struts2的配置和使用

一:配置stuts2的运行环境,把以下的包放到WEB-INF/lib目录下

1,structs2-core-2.1.6.jar       struts的核心库

2,xwork-2.1.2.jar      webwork的核心库,需要它的支持

3,  ognl-2.6.11.jar     ognl表达式语言,struts2支持改EL表达式

4,freemarker-2.3.13.jar       表现层框架,定义了struts的可视组件主题

5,common-logging-1.0.4 .jar     日子管理

6,commons-fileupload-1.2.1.jar   文件上传于下载



二:struts2拦截用户请求,在web.xml里面配置

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>


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

三:在src目录下配置struts.xml

     内容如下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">


<struts>


    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />


    <package name="default" namespace="/" extends="struts-default">


        <default-action-ref name="index" />


        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>


        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>


        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>


    <include file="example.xml"/>


    <!-- Add packages here -->


</struts>




原文地址:https://www.cnblogs.com/snake-hand/p/3174512.html