首先我们来看下struts2的执行过程

struts2执行过程.jpg

如果各位有兴趣的话可以去查看下Struts2的源码,这里我只贴出 StrutsPrepareAndExecuteFilter 这个过滤器在中的inti方法

public class StrutsPrepareAndExecuteFilter implements StrutsStatics, Filter {
    protected PrepareOperations prepare;
    protected ExecuteOperations execute;
    protected List<Pattern> excludedPatterns = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        InitOperations init = new InitOperations();
        Dispatcher dispatcher = null;
        try {
            FilterHostConfig config = new FilterHostConfig(filterConfig);
            init.initLogging(config);
            dispatcher = init.initDispatcher(config);
            init.initStaticContentLoader(config, dispatcher);

            prepare = new PrepareOperations(dispatcher);
            execute = new ExecuteOperations(dispatcher);
            this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);

            postInit(dispatcher, filterConfig);
        } finally {
            if (dispatcher != null) {
                dispatcher.cleanUpAfterInit();
            }
            init.cleanup();
        }
    }

根据代码实现过滤器在服务器启动的时候创建,创建过滤器的时候执行init方法,在init方法中主要加载配置文件,包含自己创建的配置文件和struts2自带配置文件struts.xml 

        Struts2配置详解

       1、 struts2的核心配置文件的名称和位置是固定的,位置只能存放在src下面,名称固定为struts.xml。

image.png

1、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>
    <package name="default" namespace="/" extends="struts-default">
 
    <action name="userAction" class="com.netxintai.action.UserAction">
    
    	<result name="login">/index.jsp</result>
    
    </action>
    
    </package>

</struts>

   配置文件中主要有三个标签分别是: package、action和result

  标签 package

    1、类似于代码包,区别不同的action 要配置action 首先写pagckage标签,在package里面才能配置action

    2、package标签属性

         一、name属性:一个配置文件可以写多个package标签,name属性用于区分不同的package,每个name属性值不能相同

         二、extends属性:属性值固定为 struts-default 有了这个属性之后,在package里面配置的类具有action功能  extends="struts-default"

         三、namespace属性:与action标签里面的name属性值构成访问路径  namespace="/"

   action标签

      1 name属性 :一、namespace属性值和action和标签里面的name属性值构成访问路径 例如http://localhost:8080/day01/userAction 红色字                                      体部分就是package中的nameespace="/"和action标签中的name="userAction"构成的

                             二、在package标签里面可以写多个action标签,但是action的name属性值不能相同

      2 class 属性 :Action的全路径; class="com.netxintai.action.UserAction"

      3 method属性 : 让一个action可以执行多个方法。method="{1}"   

   result 标签  根据action中方法的返回值,配置到不同的路径里面

       1 name属性:必须和方法返回值一样,否则无法找到路径

       2 type属性:配置为转发或者重定向属性 默认是转发操作

        

2 Thoughts on “struts2教程-配置文件详解

  1. hey there and thank you for your info – I’ve definitely picked up
    something new from right here. I did however expertise a few technical points using
    this web site, since I experienced to reload the website a lot of times previous to I could
    get it to load correctly. I had been wondering if
    your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will very frequently
    affect your placement in google and could damage your quality score if advertising
    and marketing with Adwords. Well I am adding this RSS to my email and
    could look out for a lot more of your respective
    exciting content. Make sure you update this again very soon..

    Lista escape room

  2. Very interesting information!Perfect just what I was looking for!!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Post Navigation