Struts2系列:(2)Struts2配置文件
2016-06-21 04:56
573 查看
Struts2默认的配置文件为struts.xml,该文件需放在当前工程(Project)的src下,部署时会被放到在WEB-INF/classes目录下。
在一篇文章中,提到一个struts.xml文件,其中的内容如下:
1、struts-default
(1-1)struts-default包是struts内置的,定义了struts2内部的众多拦截器和Result类型。Struts2很多核心的功能都是通过这些内置的拦截器实现,例如文件上传和数据验证等等都是通过拦截器实现的。
(1-2)当包(package)继承struts-default包才能使用struts2提供的这些功能。
(2-1)struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。
(2-2)struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件。(2-3)包可通过abstract=“true”定义为抽象包,该包不能包含action。
2、package
(1)Struts2 把各种 Action 分门别类地组织成不同的包(package)。
(2)一个典型的 struts.xml 文件可以有一个或多个包(package)。
(3)每个 package 元素都必须有一个 name 属性。
(4)namespace 可选, 如没有给出, 则以 “/” 为默认值. 若 namespace 有一个非默认值, 调用这个包(package)里的Action, 须把这个属性所定义的命名空间添加到有关的 URI 字符串里。
(5)package 元素通常要对 struts-default.xml 文件里定义的 struts-default包进行扩展。这样,包(package)里的所有Action就可使用在 struts-default.xml 文件里定义的result类型和拦截器.
3、action
(1)action 元素嵌套在 package内, 表示一个 Struts请求.
(2)每个 action 必须有一个 name 属性, 该属性和用户请求路径间存在一一对应关系
(3)action 元素的 class 属性是可选的。 如没配置 class 属性, Struts 将把 com.opensymphony.xwork2.ActionSupport 作为默认值。如配置了 class 属性,可以使用method属性配置该类的一个动作方法. method属性的默认值为execute。
对于ActionSupport类的定义如下:
另外,从ActionSupport类的定义可以看出,它实现了Action接口。再来看一下Action接口。
4、result
(1)result元素是<action>的一个子元素, 指导struts在完成action后控制权转向。
(2)name属性。result元素name属性对应着Action方法返回值。因method方法在不同情况下可能返回不同的值, 所以同一个action元素可有多个 result元素。
(3)type属性。result元素的type属性负责指定结果类型。type属性的值必须是在包含当前包或者是当前包的父包里注册过的结果类型。type 属性的默认值为dispatcher。
(4)name属性的默认值。result元素的name属性建立<result>和Action方法返回值之间的映射关系。name属性的默认值为 “success”。
在一篇文章中,提到一个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"> <action name="HelloWorld" class="com.rk.struts2.a_basic.HelloWorld"> <result name="success"> /Success.jsp </result> </action> </package> </struts>接下来,本文将按下图中的(1)(2)(3)(4)顺序来介绍struts的配置文件。
1、struts-default
(1-1)struts-default包是struts内置的,定义了struts2内部的众多拦截器和Result类型。Struts2很多核心的功能都是通过这些内置的拦截器实现,例如文件上传和数据验证等等都是通过拦截器实现的。
(1-2)当包(package)继承struts-default包才能使用struts2提供的这些功能。
(2-1)struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定义。
(2-2)struts-default.xml也是Struts2默认配置文件。 Struts2每次都会自动加载 struts-default.xml文件。(2-3)包可通过abstract=“true”定义为抽象包,该包不能包含action。
2、package
(1)Struts2 把各种 Action 分门别类地组织成不同的包(package)。
(2)一个典型的 struts.xml 文件可以有一个或多个包(package)。
(3)每个 package 元素都必须有一个 name 属性。
(4)namespace 可选, 如没有给出, 则以 “/” 为默认值. 若 namespace 有一个非默认值, 调用这个包(package)里的Action, 须把这个属性所定义的命名空间添加到有关的 URI 字符串里。
(5)package 元素通常要对 struts-default.xml 文件里定义的 struts-default包进行扩展。这样,包(package)里的所有Action就可使用在 struts-default.xml 文件里定义的result类型和拦截器.
3、action
(1)action 元素嵌套在 package内, 表示一个 Struts请求.
(2)每个 action 必须有一个 name 属性, 该属性和用户请求路径间存在一一对应关系
(3)action 元素的 class 属性是可选的。 如没配置 class 属性, Struts 将把 com.opensymphony.xwork2.ActionSupport 作为默认值。如配置了 class 属性,可以使用method属性配置该类的一个动作方法. method属性的默认值为execute。
对于ActionSupport类的定义如下:
/** * Provides a default implementation for the most common actions. */ public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable{ public String execute() throws Exception { return SUCCESS; } //其它实现代码。。。 }在ActionSupport类中,execute方法返回SUCCESS(事实上,SUCCESS是在Action接口中定义的)。在编写 Action 类时, 通常会对这个类(ActionSupport)进行扩展。
另外,从ActionSupport类的定义可以看出,它实现了Action接口。再来看一下Action接口。
package com.opensymphony.xwork2; /** * All actions <b>may</b> implement this interface, which exposes the <code>execute()</code> method. * */ public interface Action { /** * The action execution was successful. * Show result view to the end user. */ public static final String SUCCESS = "success"; /** * The action execution was successful but do not * show a view. This is useful for actions that are * handling the view in another fashion like redirect. */ public static final String NONE = "none"; /** * The action execution was a failure. * Show an error view, possibly asking the * user to retry entering data. */ public static final String ERROR = "error"; /** * The action execution require more input in order to succeed. * * This result is typically used if a form * handling action has been executed so as * to provide defaults for a form. The * form associated with the handler should be * shown to the end user. * <p/> * This result is also used if the given input * params are invalid, meaning the user * should try providing input again. */ public static final String INPUT = "input"; /** * The action could not execute, since the * user most was not logged in. The login view * should be shown. */ public static final String LOGIN = "login"; /** * Where the logic of the action is executed. * * @return a string representing the logical result of the execution. * See constants in this interface for a list of standard result values. * @throws Exception thrown if a system level exception occurs. * <b>Note:</b> Application level exceptions should be handled by returning * an error value, such as <code>Action.ERROR</code>. */ public String execute() throws Exception; }
4、result
(1)result元素是<action>的一个子元素, 指导struts在完成action后控制权转向。
(2)name属性。result元素name属性对应着Action方法返回值。因method方法在不同情况下可能返回不同的值, 所以同一个action元素可有多个 result元素。
(3)type属性。result元素的type属性负责指定结果类型。type属性的值必须是在包含当前包或者是当前包的父包里注册过的结果类型。type 属性的默认值为dispatcher。
(4)name属性的默认值。result元素的name属性建立<result>和Action方法返回值之间的映射关系。name属性的默认值为 “success”。
相关文章推荐
- JQuery+Strusts1.x无刷新登录
- java struts常见错误以及原因分析
- Struts之logic标签库详解
- 通过实例深入学习Java的Struts框架中的OGNL表达式使用
- Java的Struts框架中append标签与generator标签的使用
- struts2的select标签用法实例分析
- SSH框架网上商城项目第20战之在线支付平台
- 在Java的Struts框架下进行web编程的入门教程
- 详解Java的Struts框架中注释的用法
- java中struts 框架的实现
- 利用Java的Struts框架实现电子邮件发送功能
- 在Java的Struts框架中ONGL表达式的基础使用入门
- 详解Java的Struts框架中栈值和OGNL的使用
- Java的Struts框架简介与环境配置教程
- Java的Struts框架中的if/else标签使用详解
- 简单说明Java的Struts框架中merge标签的使用方法
- 详解Java的Struts框架中上传文件和客户端验证的实现
- Java的Struts框架中Action的编写与拦截器的使用方法
- sprng和struts有什么区别?
- Java的Struts框架中<results>标签的使用方法