Struts2 configuration file load order

For the struts2 framework to be executed, StrutsPrepareAndExecuteFilter must be loaded first. The Dispatcher is initialized in the init method of StrutsPrepareAndExecuteFilter. The init method defined in the Dispatcher class describes the loading order of the struts2 configuration file

init_DefaultProperties(); // [1] ---------- org/apache/struts2/default.properties              init_TraditionalXmlConfigurations(); // [2] --- struts-default.xml,struts-plugin.xml,struts.xml             init_LegacyStrutsProperties(); // [3] --- custom struts.properties              init_CustomConfigurationProviders(); // [5] ----- custom configuration provider             init_FilterInitParameters() ; // [6] ----- web.xml              init_AliasStandardObjects( ) ; // [7] ---- Bean loads  1.default.properties file Function: defines all constant locations in the struts2 framework: org/apache/struts2/default.properties 

       










2.Struts-default.xml
function: configure bean, interceptor, result, etc.
Location: in the core core jar package of struts.   struts-plugin.xml It is the configuration file of the plugin used in the struts2 framework.   struts.xml The configuration file we use to make struts2. 3. The custom struts.properties is the constant that can be customized. 4. In the development of web.xml, the configuration in the later loading file will overwrite the configuration in the first loading file. default.properties struts-default.xml



  











struts.xml

.About Action configuration :

1. <package> role: is used to declare a package. Used to manage actions.
1.name It is used to declare a package name, the package name cannot be repeated, that is, it is unique. 
2.namespace It is combined with the name attribute of the action tag to determine a unique path to access the action.
3.extends It represents the inherited package name.
4.abstrace It can take the value of true/false, if it is true, it means that the package is used to be inherited.
2<action> is used to declare an action
1.name is a name of the action, which is unique (within the same package) and determines the path to access the action with the namespace in the package.
2.class The full name
of the Action class 3.method The name of the method in the Action class to be accessed, the method has no parameters, and the return value is String.
3.<result> is used to determine the return result type
1.name It is the same as the one in the action Compare the return value of the method to determine the jump path. Other details about action configuration: 1. About the default value problem <package namespace="default value"> The default value of namespace is "" <action class="default value" method="default value"> The default value of class is com. The default value of opensymphony.xwork2.ActionSupport method is execute <









X name="default value"> The default value of name is "success"  2. Regarding the path to access the action, the current configuration of the action is: <package name="default" namespace="/" extends="struts-default" > <action name="hello" class="cn.itcast.action.DefaultAction"> <result>/hello.jsp</result> </action> </package> when we type: http://localhost/struts2_day01_2 /a/b/c/hello also accesses the action. Reason: When the action in struts2 is accessed, it will first look for 1.namespace="/a/b/c" action name=hello no. 2.namespace="/a/b action name=hello no 3. The name=hello of the namespace="/a" action is not found. 4. The name=hello of the namespace ="/" action is found. If it cannot be found at the end, a 404 error will be reported. 3. The default action. Function: Handle paths that cannot be handled by other actions. <default-action-ref name="action name" />



















 






When this is configured, when the access path cannot be processed by other actions, the action of the name specified by name will be executed. 4. The default processing class of action is configured in action, if class is not written. By default it is com.opensymphony.xwork2.ActionSupport. <default-class-ref class="cn.itcast.action.DefaultAction"/>





If it is set, then under the current package, the default processing class for processing action requests is the class specified by class.

Separation of struts.xml file: Purpose: It is for the convenience of reading. You can make a module a configuration file, pass in the struts.xml file


<include file="test.xml"/> imports other configuration files.


关于Action类的创建方式介绍:
有三种方式
1.创建一个POJO类.
简单的Java对象(Plain Old Java Objects)
指的是没有实现任何接口,没有继承任何父类(除了Object)

优点:无耦合。
缺点:所以工作都要自己实现。

在struts2框架底层是通过反射来操作:
* struts2框架 读取struts.xml 获得 完整Action类名 
* obj = Class.forName("完整类名").newInstance();
* Method m = Class.forName("完整类名").getMethod("execute");  m.invoke(obj); 通过反射 执行 execute方法


2.创建一个类,实现Action接口.  com.opensymphony.xwork2.Action

优点:耦合低。提供了五种结果视图,定义了一个行为方法。
缺点:所以工作都要自己实现。

public static final String SUCCESS = "success";  // 数据处理成功 (成功页面)
public static final String NONE = "none";  // 页面不跳转  return null; 效果一样
public static final String ERROR = "error";  // 数据处理发送错误 (错误页面)
public static final String INPUT = "input"; // 用户输入数据有误,通常用于表单数据校验 (输入页面)
public static final String LOGIN = "login"; // 主要权限认证 (登陆页面)


3.创建一个类,继承自ActionSupport类.  com.opensymphony.xwork2.ActionSupport
ActionSupport类实现了Action接口。

优点:表单校验、错误信息设置、读取国际化信息 三个功能都支持.
缺点:耦合度高。

在开发中,第三种会使用的比较多.


在struts2框架中获取servlet api:

2.注入方式获取(这种方式是真正的获取到了servlet api)

1.要求action类必须实现提定接口。
ServletContextAware : 注入ServletContext对象
ServletRequestAware :注入 request对象
ServletResponseAware : 注入response对象


2.重定接口中的方法。
private HttpServletRequest request;
3.声明一个web对象,使用接口中的方法的参数对声明的web对象赋值.
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}


扩展:分析其实现:
是使用struts2中的一个interceptor完成的.
<interceptor name="servletConfig" class="org.apache.struts2.interceptor.ServletConfigInterceptor"/>

 if (action instanceof ServletRequestAware) { //判断action是否实现了ServletRequestAware接口
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST); //得到request对象.
((ServletRequestAware) action).setServletRequest(request);//将request对象通过action中重写的方法注入。
}

3.通过ServletActionContext获取.
在ServletActionContext中方法都是static。
getRequest();
getResposne();

getPageContext();


Result结果类型:

<result>标签
1.name  与action中的method的返回值匹配,进行跳转.

2.type  作用:是用于定义跳转方式

对于type属性它的值有以下几种:
在struts-default.xml文件中定义了type可以取的值

<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>

<resulttype name="dispatcher"class="org.apache.struts2.dispatcher.ServletDispatcherResult"default="true"/>

<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>

<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>

<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>

<result-typename="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>

<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>

<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>

<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>

<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />


必会: chain  dispatcher  redirect redirectAction  stream


dispatcher:它代表的是请求转发,也是默认值。它一般用于从action跳转到页面。

        chain:它也相当于请求转发。它一般情况下用于从一个action跳转到另一个action。
redirect:它代表的是重定向  它一般用于从action跳转到页面
redirectAction: 它代表的是重定向  它一般用于从action跳转另一个action。
stream:代表的是服务器端返回的是一个流,一般用于下载。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324809476&siteId=291194637