Struts 原理
主要内容:
- struts 执行过程
- 过滤器源代码
- 核心配置
- 常量配置
- 分模块配置方法
Struts 执行过程:
查看源代码
过滤器源代码
/*
* $Id: DefaultActionSupport.java 651946 2008-04-27 13:41:38Z apetrelli $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts2.dispatcher.ng.filter;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.dispatcher.ng.ExecuteOperations;
import org.apache.struts2.dispatcher.ng.InitOperations;
import org.apache.struts2.dispatcher.ng.PrepareOperations;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
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();
}
}
/**
* Callback for post initialization
*/
protected void postInit(Dispatcher dispatcher, FilterConfig filterConfig) {
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
if (excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
prepare.setEncodingAndLocale(request, response);
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
request = prepare.wrapRequest(request);
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
} else {
execute.executeAction(request, response, mapping);
}
}
} finally {
prepare.cleanupRequest(request);
}
}
public void destroy() {
prepare.cleanupDispatcher();
}
}
-
过滤器两个功能,预处理和执行
-
预处理就是用来加载配置文件的,对应的就是init()方法
- 在init() 中主要加载配置文件 包含自己创建的配置文件和struts2自带的配置文件
- 在上面的代码中
dispatcher = init.initDispatcher(config)
中,来进行初始化配置,进去后发现dispatcher.init() 方法,再进入
里面的一系列代码就是用来加载Struts2 配置文件的
init_DefaultProperties();//[1]
配置的是Struts的所有常量
init_TraditionalXmlConfigurations();//[2]
加载的struts-default.xml struts-plugin.xml struts.xml
init_LegacyStrutsProperties();//[3]
加载用户自定义struts.properties
init_CustomConfigurationProviders();//[5]
加载用户配置的提供对象
init_FilterInitParameters();//[6]
加载web.xml
init_AliasStandardObjects();//[7]
加载标准对象
核心配置
-
名称和位置是固定的
-
配置文件中主要三个标签 package 、action、 result
-
package 标签
- 核心组件是Action和拦截器,使用包管理他们,每个包就是多个Action.多个拦截器、多个拦截器引用的集合
-
action配置,见action文章
Struts2 常量的配置
默认的常量文件位置
通过配置文件对常量进行重新配置
敞亮配置通常有三种方式如下:
- 在struts.xml 中使用 元素配置常量
- 在Struts.xml 中配置常量
- 在 web.xml 文件中通过 元素配置常量
1、元素配置
最常用的方式,通过name和value属性
- name 指定常量的常量名
- value 指定常量的常量值
<constant name="struts.i18.encoding" value="UTF-8"</constant>
2、struts.properties文件配置
文件里面是键值对 key-value
### 设置默认编码集为utf-8
struts.i18n.encoding=UTF-8
### 设置不使用开发模式
struts.devMode=false
3、web.xml
配置为StrutsPrepareAndExecuteFilter 时 标签必须放在 标签下面
<init-param>
<param-name>struts.i18n.encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
分模块开发配置
主配置文件:struts.xml
分布配置文件:XXX.xml
<struts>
<!-- 不指定路径默认在src的下的方式-->
<include file="struts-user.xml">
<!-- 配置文件在包中时的方式-->
<include file="cn/lenks/action/struts-user.xml">
最后更新:2018.10.31