首先,由于之前没用过json,初学extjs使用json封装数据以为不用导入任何包,并且百度上说json需要的包struts包中都有(此说话是正确的,不过json要的包必须再导入)。 郁闷了一两天的问题终于解决了。下面是我自己做的一个ssh + extjs 4 登录的小例子,无数据验证。
要添加的包,直接放到lib下即可
1.Action代码
public class ExtjsAction extends BaseAction {
//声明相关的属性,用来封装数据,json封装数据和struts2有些想象,前台到后台会自动封装
private String username;//对应页面上的用户名文本框name
private String password;
private boolean success;//用来保存success信息,传到前台
private String msg;
public String logins(){
System.out.println(username + "+++" + password);
//String res = "{success:true,msg:\"account right!\"}";
//String res = "{success:false,msg:\"account right!\"}";
this.success = true;
this.msg = "aa";
String js = "{'success':true,'msg':'登录成功'}";
//outputAjaxJsonData(js);
return SUCCESS;
}
2. Struts 配置文件代码
<package name="ext2" extends="json-default">
<action name="loginExt" class="org.comm.action.ExtjsAction">
<result type="json">
<param name="includeProerties">success,msg</param>
</result>
</action>
</package>
3. Jsp或者html 代码
<link rel="stylesheet" type="text/css" href="../resources/css/ext-all.css">
<script type="text/javascript" src="../ext-all.js"></script>
<script type="text/javascript">
Ext.onReady(function(){
Ext.QuickTips.init();
var loginWindo = new Ext.form.FormPanel({
title:'Login',//窗口标题
id:'elId',//给form添加id
autoHeight:true,//自动高度(根据英文自己理解的)
x:200,//x坐标
y:120,//y坐标
width:300,//宽度
renderTo:Ext.getBody(),
frame:true,
cls:'text-align:center',//文本居中显示
items:[
//username
{
xtype:'textfield',
name:'username',//对应action中属性名
fieldLabel:'name',//label上的名字
allowBlank:false,//是否为空
blankText:'please input name.',//空时提示文本msgTarget:'under'
}
],
buttonAlign:'center',
buttons:[{
xtype:'button',
text:'logins',
scope:this,
handler:logins
},{
xtype:'button',
text:'reset',
scope:this,
handler:reset
}]
});
//登录方法
function logins(){
loginWindo.form.submit({
clientValidation:true,
waitMsg:'正在登录中。。。',//单击登录按钮后提示消息
url:'ext2/loginExt!logins.action',
method:'POST',
success:function(form,action){
var jsontext = Ext.decode(action.response.responseText);
Ext.Msg.alert('登录提示',"登录成功"+jsontext.msg);
},
failure:function(){
Ext.Msg.alert('登录提示','登录失败');
}
});
}
//重置
function reset(){
loginWindo.form.reset();
}
//显示登录窗口
loginWindo.show();
});
</script>
有问题可以联系我
http://hi.baidu.com/danforgtlove/item/e9d4d8e0dad25fcbeb34c9f5