ext集成struts2的小demo

来源:互联网 发布:北京知金科技有限公司 编辑:程序博客网 时间:2024/06/04 19:52
package com.ext.model;public class User {private Integer id;private String username;private String password;public Integer getId() {    return id;}public void setId(Integer id) {    this.id = id;}public String getUsername() {    return username;}public void setUsername(String username) {    this.username = username;}public String getPassword() {    return password;}public void setPassword(String password) {    this.password = password;}}四、写LoginActionpackage com.ext.action;import com.ext.model.User;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport    {
//这个success是跟前台的success:function(){}这个值相对应的如果不设置这个值,前台永远不会进success这个方法中private boolean success;private String message;private User user;@Override    public String execute() throws Exception {        // TODO Auto-generated method stub        if(user.getUsername().equals("admin")&&user.getPassword().equals("admin")){            this.success= true;            //this.message="你的账号是:"+user.getUsername()+"密码是:"+user.getPassword();        }else{            this.success=false;            this.message="对不起,未授权的用户不能登录改系统";        }        return SUCCESS;    }public boolean isSuccess() {    return success;}public void setSuccess(boolean success) {    this.success = success;}public String getMessage() {    return message;}public void setMessage(String message) {    this.message = message;}public User getUser() {    return user;}public void setUser(User user) {    this.user = user;}}五、struts.xml如下所示:<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>
<include file="struts-plugin.xml"/><package name="extjs" extends="json-default" namespace="/"><action name="Login" class="com.ext.action.LoginAction">
<result result="success" type="json">
<!-- 只将user转变成json对象-->
<paramet name="root">user</paramet>
</result></action></package></struts> 六、login.jsp<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title>   <link rel="stylesheet" type="text/css" href="ext3/resources/css/ext-all.css"/>    <script type="text/javascript" src="ext3/adapter/ext/ext-base.js"></script>    <script type="text/javascript" src="ext3/ext-all.js"></script>    <script type="text/javascript" src="ext3/ext-lang-zh_CN.js"></script>    <script type="text/javascript" src="login.js"></script></head><body></body></html>七、login.jsExt.onReady(function(){Ext.QuickTips.init();Ext.form.Field.prototype.msgTarget="side";var form1=new Ext.FormPanel({    labelWidth:40,    baseCls:'x-plain',    defaults:{width:180},    items:[{        xtype:'textfield',        fieldLabel:"用户名",        id:"txtName",        name:'user.username',        allowBlank:false,        blankText:"用户名不能为空!"        },{        xtype:'textfield',        fieldLabel:"密码",        allowBlank:false,        blankText:"密码不能为空!",        name:'user.password',        inputType:'password'        }],    buttons:[{        text:"提交",        type:'submit',        handler:function(){            if(form1.getForm().isValid()) {            Ext.MessageBox.show({                title:'请等待',                msg:'正在加载',                progressText:'',                width:300,                progress:true,                closable:'false',                animEl:'loding'            });                var f = function(v){                return function(){                    var i=v/11;                    Ext.MessageBox.updateProgress(i,'');                }            }            for(var i=1;i<33;i++){                setTimeout(f(i),i*1500);            }            //提交到服务器操作            form1.form.doAction('submit',{            url:'Login.action',            method:'post',            success:function(form,action){                document.location="index.jsp";                Ext.Msg.alert("登录成功!",action.result.message);            },            failure:function(form,action){                Ext.Msg.alert("登录失败!",action.result.message);            }            });        }        }},            {        text:"重置",        handler:function() {    form1.getForm().reset();}}]});    var window = new Ext.Window({        title :"登录窗口",        layout:'fit',        width:290,        height:250,        plain:true,        bodyStyle:'padding:10px',        maximizable:false,        closeActon:'close',        closable:false,        collapsible:true,        buttonAlign:'center',        items:form1    });    window.show();});
关于ext表单验证还有如下一些:
Extjs表单验证包括空验证、用vtype格式进行简单的验证、高级自定义密码验证、使用正则表达式验证等等。验证可以使用js提供的脚本来进行代码编写,但ext本身对表单进行了封装,并允许客户对其进行扩展,因此使用Extjs提供的验证能够大大简化验证判断。 在验证之前,先看下面两个语句:
//放在onReady的function(){}中 Ext.QuickTips.init(); //为组件提供提示信息功能,form的主要提示信息就是客户端验证的错误信息。 Ext.form.Field.prototype.msgTarget='side'; 
//提示的方式,枚举值为:qtip-当鼠标移动到控件上面时显示提示;title-在浏览器的标题显示,但是测试结果是和qtip一样的; under-在控件的底下显示错误提示; side-在控件右边显示一个错误图标,鼠标指向图标时显示错误提示. 默认值; id-[element id]错误提示显示在指定id的HTML元件中 1.一个最简单的例子:空验证 代码如下://空验证的两个参数 allowBlank:false//false则不能为空,默认为true blankText:string//当为空时的错误提示信息js代码为: 代码如下:var form1 = new Ext.form.FormPanel({ width:350, renderTo:"form1", title:"FormPanel", defaults:{xtype:"textfield",inputType:"password"}, items:[ {fieldLabel:"不能为空", allowBlank:false, //不允许为空 blankText:"不能为空", //错误提示信息,默认为This field is required! id:"blanktest", } ] });2.用vtype格式进行简单的验证。 在此举邮件验证的例子,重写上面代码的items配置: 代码如下: items:[ {fieldLabel:"不能为空", vtype:"email",//email格式验证 vtypeText:"不是有效的邮箱地址",//错误提示信息,默认值我就不说了 id:"blanktest", anchor:"90%"}你可以修改上面的vtype为以下的几种extjs的vtype默认支持的验证: //form验证中vtype的默认支持类型 1.alpha //只能输入字母,无法输入其他(如数字,特殊符号等) 2.alphanum//只能输入字母和数字,无法输入其他 3.email//email验证,要求的格式是"" 4.url//url格式验证,要求的格式是http://www.baidu.com 3.高级自定义密码验证 前面的验证都是extjs已经提供的,我们也可以自定义验证函数。 代码如下: //先用Ext.apply方法添加自定义的password验证函数(也可以取其他的名字)Ext.apply(Ext.form.VTypes,{ 
//val指这里的文本框值,field指这个文本框组件,大家要明白这个意思,这些名字可以随意修改的,只是它表达的意思要搞清楚 
password:function(val,field){
if(field.confirmTo){//confirmTo是我们自定义的配置参数,一般用来保存另外的组件的id值,confirmTo是自己定义的,因为ext是基于面向对象的,confirmTo其实就是表示一个对象的属性而已 ext中定义对象就是一个花括号{} 里面的值就是这个对象的属性
var pwd=Ext.get(field.confirmTo);//取得confirmTo的那个id的值 
return (val==pwd.getValue()); 

return true; 

});




//配置items参数
items:[{fieldLabel:"密码", 
id:"pass1", 
},{ 
fieldLabel:"确认密码", 
id:"pass2", 
vtype:"password",//自定义的验证类型 
vtypeText:"两次密码不一致!", 
confirmTo:"pass1",//要比较的另外一个的组件的id 
}


红色的地方表示EXT可以很好的扩展我们对自己需要自定义的验证方式

4.使用正则表达式验证 
代码如下:
new Ext.form.TextField({ 
fieldLabel : "姓名", 
name : "author_nam", 
regex : /[\u4e00-\u9fa5]/, //正则表达式在/...../之间. [\u4e00-\u9fa5] : 只能输入中文. 
regexText:"只能输入中文!", //正则表达式错误提示 
allowBlank : false //此验证依然有效.不许为空.
                                             
0 0