2009年10月21日
摘要: Struts2默认使用Jakarta的Common-FileUpload的文件上传解析器。见struts.properties配置文件中:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#指定使用COS的文件上...
阅读全文
对于login方法的校验,可以通过<ActionClassName>-<ActionAliasName>-validation.xml文件来校验,即RegistAction-login-validation.xml文件进行校验。同时,RegistAction-validation.xml的校验规则仍旧对login方法有效。即login方法的校验规则是Region-validation.xml和RegistAction-login-validation.xml的总和。
如果RegistAction继承了BaseAction,那么对于BaseAction类的校验规则也会被RegistAction类所继承校验。具体来说,其校验规则的搜索文件规则如下:
BaseAction-validation.xml
BaseAction-别名-validation.xml
RegistAction-validation.xml
RegistAction-别名-validation.xml
对于Struts2所支持的内建校验器,我们可以通过xwork.2.1.2.jar中的com/opensymphony/xwork2/validator/validators/default.xml文件查看。
除了配置文件,Struts2也支持Annotation。下面就是使用Annotation配置的RegistAction:
@Validation()
public class RegistAction extends ActionSupport {
private static final long serialVersionUID = -2113900523366315993L;
//该请求包含的4个请求参数
private String name;
private String pass;
private int age;
private Date birth;
public String getName() {
return name;
}
@RequiredStringValidator(type=ValidatorType.FIELD,key="name.required",message="")
@RegexFieldValidator(type=ValidatorType.FIELD,expression="\\w{4,25}",key="name.regex",message="")
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
@RequiredStringValidator(type=ValidatorType.FIELD,key="pass.required",message="")
@RegexFieldValidator(type=ValidatorType.FIELD,expression="\\w{4,25}",key="pass.regex",message="")
public void setPass(String pass) {
this.pass = pass;
}
public int getAge() {
return age;
}
@IntRangeFieldValidator(message="",key="age.range",min="1",max="150")
public void setAge(int age) {
this.age = age;
}
public Date getBirth() {
return birth;
}
@DateRangeFieldValidator(message="",key="birth.range",min="1900/01/01",max="2050/01/21")
public void setBirth(Date birth) {
this.birth = birth;
}
}

对于不能通过内置校验器进行校验的逻辑,则可通过重写validate()方法来实现。如果一个Action中存在多个逻辑处理,则需要通过validateXxx()方法来分别进行校验,其中Xxx代表方法名。如login()方法对应的校验方法为validateLogin()。
Struts2的输入校验流程:
1。类型转换器负责对字符串的请求参数执行类型转换,并将这些值设置成Action的属性值。
2。在执行类型转换过程中,如果发生异常,则将异常保存到ActionContext中,并由conversionError拦截器负责将其封装到fieldError里。然后执行第3步;如无异常,直接执行第3步。
3。调用Struts2内置的输入校验规则进行输入校验
4。通过放射调用validateXxx()方法。
5。调用validate()方法。
6。如果上述都未发生fieldError,将调用Action里处理用户请求的处理方法;如果出现了fieldError,则转入input逻辑视图所指定的视图资源。
摘要: Struts2提供了基于验证框架的输入校验,在这种校验方式下,所有的输入校验只需要通过指定的配置文件即可。Struts2中每个Action都有一个校验文件,其规则为:<Action名字>-validation.xml。如:
Code highlighting produced by Actipro CodeHighlighter (freewar...
阅读全文
摘要: Ext.data.Connection
Ext.data.Connection是对Ext.lib.Ajax的封装,它提供了配置使用Ajax的通用方式,它在内部通过Ext.lib.Ajax实现与后台的异步调用。与底层的Ext.lib.Ajax相比,Ext.data.Connection提供了更简洁的配置方式,使用起来更方便。
 ...
阅读全文
摘要: 简单菜单
//创建工具条
var tb = new Ext.Toolbar();
tb.render('toolbar');
//为工具条添加按钮
tb.add({
text:'新建',
//对应的事件处理函数
...
阅读全文
摘要: 布局概述
在EXT中,所有的布局都是从Ext.Container开始的,Ext.Container的父类是Ext.BoxComponent。Ext.BoxComponent是一个盒子组件,可以定义宽度,高度和位置等属性。作为子类,Ext.Co...
阅读全文
Ext.MessageBox
1
//Ext.MessageBox.alert()
2
Ext.MessageBox.alert('标题','内容',function(btn)
{
3
alert('你刚刚点击了' + btn);
4
});
5
6
//Ext.MessageBox.confirm()
7
Ext.MessageBox.confirm('选择框','你到底是选择Yes还是No?', function(btn)
{
8
alert('你刚刚点击了' + btn);
9
});
10
11
//Ext.MessageBox.prompt()
12
Ext.MessageBox.prompt('输入框','随便输入一些东西', function(btn,text)
{
13
alert('你刚刚点击了' + btn + ", 刚刚输入了" + text);
14
});
对话框的更多配置
1
//可以输入多行的输入框
2
Ext.MessageBox.show(
{
3
title:'多行输入框',
4
msg:'你可以多输入好几行',
5
width:300,
6
buttons:Ext.MessageBox.OKCANCEL,
7
multiline:true,
8
fn:function(btn,text)
{
9
alert('你刚刚点击了' + btn + ", 刚刚输入了" + text);
10
}
11
});
12
13
//自定义对话框的按钮
14
Ext.MessageBox.show(
{
15
title:'随便按个按钮',
16
msg:'从三个按钮里随便选择一个',
17
buttons:Ext.MessageBox.YESNOCANCEL,
18
multiline:true,
19
fn:function(btn)
{
20
alert('你刚刚点击了' + btn);
21
}
22
});
Ext.MessageBox中预设的4个按钮分别是OK,Yes,No,Cancel。如果不使用YESNOCANCEL这种预设变量,也可以直接使用{ok:true, yes:true, no:true,cancel:true}的形式,这样4个按钮都会显示在对话框中。
进度条
1
Ext.MessageBox.show(
{
2
title:'请等待',
3
msg:'读取数据中',
4
width:240,
5
progress:true,
6
closable:false //隐藏对话框右上角的关闭按钮,从而禁止用户关闭进度条
7
});
8
9
//也可以直接使用Ext.MessageBox.progress()
10
Ext.MessageBox.progress('请等待',msg:'读取数据中');
11
上述的进度状态时不会发生变化的,我们需要调用Ext.MessageBox.updateProgress()函数,如以下为每秒变化,10秒后隐藏:
1
var f = function(v)
{
2
return function()
{
3
if (v == 11)
{
4
Ext.MessageBox.hide();
5
} esle
{
6
Ext.MessageBox.updateProgress(v/10,'正在读取第' + v + '个,一共10个');
7
}
8
};
9
};
10
for (var i = 1 ; i < 12 ; i++)
{
11
setTimeout(f(i) , i*1000);
12
}
还可以使用一种自动变化的进度条提示框,如Ext.MessageBox.wait('请等待', msg: ' 读取数据中');
动画效果
可以为对话框这是弹出和关闭的动画效果,使用animEl参数指定一个HTML元素,对话框就会依据指定的HTML元素播放弹出和关闭的动画。
窗口分组
1
<script type="text/javascript">
2
var i = 0 , mygroup;
3
4
function newWin()
{
5
var win = new Ext.Window(
{
6
title:'窗口'+ i++,
7
width:400,
8
height:300,
9
maximizable:true,
10
manager:mygroup
11
});
12
win.show();
13
}
14
15
function toBack()
{
16
mygroup.sendToBack(mygroup.getActive());
17
}
18
19
function hideAll()
{
20
mygroup.hideAll();
21
}
22
23
Ext.onReady(function()
{
24
mygroup = new Ext.WindowGroup();
25
26
Ext.get("btn").on("click",newWin);
27
Ext.get("btnToBack").on("click",toBack);
28
Ext.get("btnHide").on("click",hideAll);
29
});
30
</script>
31
32
<BODY>
33
<input id="btn" type="button" name="add" value="新窗口"/>
34
<input id="btnToBack" type="button" name="btnToBack" value="放到后台"/>
35
<input id="btnHide" type="button" name="btnHide" value="隐藏所有"/>
36
</BODY>
示例中,所有通过newWind()方法产生的窗口都属于mygroup窗口组
摘要: 先看最简单的拖放:
1new Ext.dd.DDProxy('block');
2//对应的HTML部分代码
3<div id="block" style="background:red;"> </div>
拖放组件的体系
简单来说,左面4个组件都是...
阅读全文
摘要: 基本输入控件Ext.form.Field
Ext.form.Field是所有表单输入控件的基类,其他的输入控件都是基于它扩展的来的。其定义了输入控件通用的属性和功能函数,这些通用的属性和功能函数大致分为3大类:页面显示样式、控件参数配置和数据有效性校验。
页面显示样式:clearCls, cls, ...
阅读全文
摘要: 制作一个简单的Grid
1var cm = new Ext.grid.ColumnModel([
2 {header:'编号',dataIndex:'id'},
3 {header:'名称',dataIndex:'name...
阅读全文