struts.properties 常用配置文件
<constant name="struts.i18n.encoding" value="UTF-8" />字符集
<constant name="struts.convention.action.packages" value="cn.jingmin.web" /> 只搜索特定package下的Action
<constant name="struts.convention.result.path" value="/WEB-INF/context" /> 指定加载页面的目录
<constant name="struts.convention.relative.result.types" value="dispatcher,velocity,freemarker" />返回的页面类型
<constant name="struts.convention.exclude.packages" value="" />忽略某些包
<constant name="struts.convention.package.locators" value="" /> Convention默认的根packages
<constant name="struts.convention.classes.reload" value="true" />
<constant name="struts.objectFactory" value="spring" /> 指定spring自动装载
<constant name="struts.action.extension" value="action" /> 指定struts的扩展名
<constant name="struts.devMode" value="true" /> 指定struts的开发模式
<constant name="struts.i18n.reload" value="action" /> 指定struts的资源文件自动重载
<constant name="struts.custom.i18n.resource" value="" /> 指定struts的国际化资源文件
<constant name="struts.configuration.xml.reload" value="" /> 指定struts的配置文件是否自动重载
<constant name="struts.ul.theme" value="xthml" /> 指定struts的模板
Struts 2的convention-plugin插件
Maven下对应依赖包配置:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.1.6</version>
</dependency>
1. 默认所有的结果页面都存储在WEB-INF/content下,你可以通过设置struts.convention.result.path这个属性的值来改变到其他路径。如:
<constant name="struts.convention.result.path" value="/WEB-INF/page" />
则将路径配置到了WEB-INF/page 下。
2. 默认包路径包含action,actions,struts,struts2的所有包都会被struts作为含有Action类的路径来搜索。你可以通过设置struts.convention.package.locators属性来修改这个配置。如:
<constant name="struts.convention.package.locators" value="web,action" />
则定义了在项目中,包路径包含web和action的将被视为Action存在的路径来进行搜索。
Com.ustb.web.*/com.ustb.action.*都将被视为含有Action的包路径而被搜索。
3. 接着,Convention从前一步找到的package以及其子package中寻找
com.opensymphony.xwork2.Action 的实现以及以Action结尾的类:
com.example.actions.MainAction
com.example.actions.products.Display (implements com.opensymphony.xwork2.Action)
com.example.struts.company.details.ShowCompanyDetailsAction
4. 命名空间。从定义的.package.locators标示开始到包结束的部分,就是命名空间。举个例子:
Com.ustb.web.user.userAction的命名空间是:”/user”。Com.ustb.web.user.detail.UserAction的命名空间是:”/user/detail”
5. Convention通过如下规则确定URL的具体资源部分:去掉类名的Action部分。然后将将每个分部的首字母转为小写,用’-’分割,你可以设置struts.convention.action.name.separator 如
<constant name="struts.convention.action.name.separator" value="-" />
还是举个例子:
UserAction->user UserDetailAction ->user-detail。结合上面的。对于com.ustb.web.user.detail.UserDetailAction,映射的url就是/WEB-INF/content/user/detail/user-detail.jsp
6. struts支持.jsp .html .htm .vm格式的文件。
下面是actiong和结果模版的映射关系:
URL Result File that could match Result Type
/hello success /WEB-INF/content/hello.jsp Dispatcher
/hello success /WEB-INF/content/hello-success.htm Dispatcher
/hello success /WEB-INF/content/hello.ftl FreeMarker
/hello-world input /WEB-INF/content/hello-world-input.vm Velocity
/test1/test2/hello error /WEB-INF/content/test/test2/hello-error.html Dispatcher
当然,简单的通过默认的方式来进行配置不能完全满足实际项目的需要。所幸,convention的零配置是非常灵活的。
通过@Action注释
对如下例子:
package com.example.web;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
@Action("action1")
public String method1() {
return SUCCESS;
}
@Action("/user/action2")
public String method2() {
return SUCCESS;
}
}
方法名 默认调用路径 默认映射路径
method1 /hello!method1.action . /WEB-INF/content/hello.jsp
method2 /hello!method2.action. /WEB-INF/content/hello.jsp
通过@Action注释后
方法名 @Action注释后调用路径 @Action注释 后映射路径
method1 /action1!method1.action. /WEB-INF/content/action1.jsp
method1 /user/action2!method2.action /WEB-INF/content/user/action2.jsp
通过@Actions注释
package com.example.web;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
public class HelloAction extends ActionSupport {
@Actions({
@Action("/different/url"),
@Action("/another/url")
})
public String method1() {
return “error”;
}
我们可以通过:/different/url!method1.action 或 /another/url!method1.action 来调用method1 方法。
对应的映射路径分别是/WEB-INF/content/different/url-error.jsp; /WEB-INF/content/another/url-error.jsp
可能误导了大家,一个方法被@Action注释后,只是多了一种调用方式,而不是说覆盖了原来的调用方式。比如对于如下例子:
package com.example.web;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
public class HelloAction extends ActionSupport {
@Action("/another/url")
public String method1() {
return “error”;
}
我们调用method1方法可以通过两种方式:
1 /hello!method1.action 映射 url:/WEB-INF/content/hello-error.jsp
2 /another/url!method1.action 映射 url:/WEB-INF/content/another/url-error.jsp
可见,两种方式均可对method1方法进行调用,唯一的区别就是,两种调用的映射是不一样的,所以,想跳转到不同的界面,这是一个非常好的选择。
通过@Namespace 注释
package com.example.web;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
@Namespace("/other")
public class HelloWorld extends ActionSupport {
public String method1() {
return “error”;
}
@Action("url")
public String method2() {
return “error”;
}
@Action("/different/url")
public String method3() {
return “error”;
}
}
通过 /other/hello-world!method1.action 访问method1 方法。
通过 /other/url!method2.action 访问method2 方法
通过 /different /url!method3.action 访问method3 方法
与@Action 注释不同的是,该注释覆盖了默认的namespace(这里是’/’),此时再用hello!method1.action 已经不能访问method1 了.
@Results和@Result
1 全局的(global)。
全局results可以被action类中所有的action分享,这种results在action类上使用注解进行声明。
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
@Results({
@Result(name="failure", location="/WEB-INF/fail.jsp")
})
public class HelloWorld extends ActionSupport {
public String method1() {
return “failure”;
}
@Action("/different/url")
public String method2() {
return “failure”;
}
}
当我们访问 /hello -world !method1.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /hello -world !method2.action 时,返回 /WEB-INF/fail.jsp
当我们访问 /different/url!method2.action 时,返回 /WEB-INF/fail.jsp
2 本地的(local)。
本地results只能在action方法上进行声明。
package com.example.actions;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
public class HelloWorld extends ActionSupport {
@Action(value="/other/bar",results={@Result(name = "error", location = "www.baidu.com",type="redirect")})
public String method1() {
return “error”;
}
}
当我们调用 /hello -world !method1.action 时,返回 /WEB-INF/content/hello-error.jsp
分享到:
相关推荐
StrutsStatics Constants used by Struts. 类概要 类 说明 RequestUtils Request handling utility class. ServletActionContext Web-specific context information for actions. StrutsConstants This class ...
将常量声明部分添加到vue组件。 这使您可以更好地陈述自己的意图,还可以避免vue在不变的值上创建观察者的开销。 通过安装 npm install vue-constants 通过在您的项目中启用它 import VueConstants from 'vue-...
Java常量接口反模式 这篇文章最初发布在我的博客上: : 您如何在Java中定义和使用常量? Internet上的大多数建议具有以下意见: 为类中的常量声明public static final 不要将接口用于常量 定义常量的最常见方法...
CI框架源码阅读,系统常量文件constants.php的配置,需要的朋友可以参考一下
/Struts2Test3.0/src/com/wuxinfengjing/comm/GlobalConstants.java中的DEFAULT_USER_PASSWORD变量上. 环境:jdk1.5 tomcat5.5.数据库用oracle.如果不用oracle的话.可能菜单表的查询出不来.我的菜单表的查询允许选择...
常数库NPM软件包用于共享常量描述用于保存常量的节点模块。 可用于更轻松地访问不会经常更改或根本不会更改的任何值。用法建立群组let vault = require('constants-vault');vault.addGroup("name", { someKey: 'some...
所有 WebGL 1.0 常量作为一个模块。 用于测试。 var constants = require ( 'gl-constants' ) texture . minFilter = constants . LINEAR texture . magFilter = constants . NEAREST 您还可以按数字“查找”...
前端开源库-oma-constantsOMA引用实现中的OMA常量、常量文件和目录名。
主要是实现flexigrid在struts2架构中的增删改查的功能。 部署说明: flexieGridStrut文件夹为源码。 accountant.sql 为数据库代码。 部署步骤: 把flexieGridStrut导入到Myeclipse。 在mysql里面运行accountant....
python win32com excel常量
Ab-initio calculation of elastic constants of TiN.pdf
Windows Constants 不是一个控件,单是能够方便你的代码!
我发现自己在一个项目又一个项目中定义了相同的常量。 该停下来了。 安装 yarn add common-constants 或者 npm add --save common-constants 导出包含一组公共常量的对象文字。 分类: 日期和时间(constants....
资源来自pypi官网。 资源全名:constants_set-0.4.tar.gz
该存储库包含一些常量,可以更轻松地使用 (CDK)。 为什么? CDK非常棒,但是在初始化诸如IAM服务主体和托管策略之类的构造时,CDK目前缺少类型。 要找到正确的构造名称,需要深入研究AWS文档。 因为没有对这些...
ESH常量生成器Maven插件什么? 该插件会生成Java常量类,并在ESH_INF文件夹文件中定义所有命名。为什么? 如果内容很多,因为很难自己编写此类。 此外,XML文件应处于领先地位,并且您的代码库始终保持同步。如何? ...
常数不变的通用值。 基本类别为: 约会时间圆常数工作簿地址系统格式化常数安装npm install formula-constants
资源分类:Python库 所属语言:Python 使用前提:需要解压 资源全名:directory_constants-12.5.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
3.常量 Constants 4.操作符/运算符 Operators 5.控制台交互 Communication through console 3.控制结构和函数 Control structures and Functions 1.控制结构 Control Structures 2.函数I Functions I 3.函数...