本人新手,学习extjs3.0,要把它整合到struts2.1+spring2.5+hibernate3.0的框架中去,想做个添加管理员的功能,可是每次都是调用failure函数还提示错误类型connect,相关代码如下,请各位解答!(本人已经测试过了,如是用jsp做表现层,能把数据写入到数据库而用extjs做表现层则不行)
struts.xml的配置文件如下(action的配置)
<?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>
<package name="json" namespace="/" extends="json-default">
<action name="addUser" class="addUser" method="add">
<result type="json"/>
</action>
</package>
</struts> addUser.action的代码如下:
package per.ssh.action.admin;import java.io.IOException;
import com.opensymphony.xwork2.ActionSupport;import per.ssh.dao.Admin.Admin;
import per.ssh.services.admin.adminInterface;public class adminAction extends ActionSupport{
private boolean success;
private String msg="";
/**
* @return the success
*/
public boolean isSuccess() {
return success;
}
/**
* @param success the success to set
*/
public void setSuccess(boolean success) {
this.success = success;
}
/**
* @return the msg
*/
public String getMsg() {
return msg;
}
/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this.msg = msg;
}
private User user;
public User getUser(){
return this.user;
}
public void setUser(User user){
this.user=user;
}
private adminInterface testService=null;
public void setTestService(adminInterface testService){
this.testService=testService;
}
public String add() throws IOException{
Admin admin=new Admin(user.getUserName(),user.getPassword(),Integer.parseInt(user.getUserLevel()));
try{
testService.insert(admin);
success=true;
msg="添加管理员成功!";
}
catch(Exception ex){
success=false;
msg="用户名或密码错误!";
}
System.out.println(user.getUserName()+user.getPassword()+user.getUserLevel());
return SUCCESS;
}
}extjs的代码如下:
Ext.ns("per.extjs.userManager");
Ext.apply(Ext.form.VTypes,{
confrimPwd:function(val,field){
if(field.initalPassword){
var pwd=Ext.getCmp(field.initalPassword);
return (val==pwd.getValue());
}
return true;
},
confrimPwdText:'两次密码输入不一致!'
});
per.extjs.userManager.userForm=function(){
var boxStore=new Ext.data.SimpleStore({
fields:['user','level'],
data:[['超级管理员','1'],['一般管理员','2']]
});
var userLevel=new Ext.form.ComboBox({
id:'userLevel',
name:'user.userLevel',
fieldLabel:'用户类别',
forceSelection:true,
triggerAction:'all',
editable:false,
labelWidth:100,
width:125,
store:boxStore,
displayField:'user',
valueField:'level',
mode:'local',
value:'1',
width:600
});
var userName=new Ext.form.TextField({
id:'userName',
name:'user.userName',
fieldLabel:'用 户 名',
allowBlank:false,
blankText:'用户名不能为空!',
msgTarget:'under',
width:600
});
var password=new Ext.form.TextField({
id:'password',
name:'user.password',
fieldLabel:'用户密码',
allowBlank:false,
blankText:'密码不能为空',
inputType:'password',
msgTarget:'under',
width:600
});
var confrimPassword=new Ext.form.TextField({
id:'cpwd',
name:'cpwd',
fieldLabel:'重复密码',
inputType:'password',
initalPassword:'password',
vtype:'confrimPwd',
allowBlank:false,
blankText:'重复密码不能为空',
msgTarget:'under',
width:600
});
per.extjs.userManager.userForm.superclass.constructor.call(this,{
id:'userForm',
title:'添加管理员',
frame:true,
autoHeight:true,
applyTo:'main',
labelAlign:'right',
buttonAlign:'center',
labelWidth:'100',
width:760,
collapsible:true,
animCollapse:true,
bodyStyle:'padding:5px;',
items:[
{
xtype:'fieldset',
title:'管理员信息',
autoHeight:true,
autoWidth:true,
items:[userLevel,userName,password,confrimPassword]
}
],
buttons:[
{
text:'提交',
handler:submit
},{
text:'取消',
handler:reset
}
]
});
function submit(){
Ext.getCmp('userForm').form.submit({
clientValidation:true,
//waitMsg:'正在提交数据,请稍等...',
//waitTitle:'提示',
url:'addUser.action',
method:'POST',
//params:{userLevel:userComboBox,userName:userName,password:userpassword},
success:function(form,action){
Ext.Msg.alert('提示',"添加管理员成功!");
},
failure:function(form,action){
Ext.Msg.alert('提示',action.failureType+":"+action.getUrl());
}
});
}
function reset(){
Ext.getCmp('userForm').form.reset();
}
}
Ext.extend(per.extjs.userManager.userForm,Ext.form.FormPanel,{}); 相应的jsp页面代码如下:<%@ page language='java' import='java.util.*' pageEncoding='GBK'%>
<!-- <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> --><!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<base>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/ext-all-debug.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/pages/common/common.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/pages/userManager/userManager.js"></script>
<title>My JSP "test.jsp" starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
Ext.onReady(function(){
Ext.BLANK_IMAGE_URL="${pageContext.request.contextPath}/extjs/resources/images/default/s.gif",
Ext.QuickTips.init();
var top=new per.extjs.common.top();
var nav=new per.extjs.common.nav();
var foot=new per.extjs.common.foot();
var form=new per.extjs.userManager.userForm();
});
</script>
</head>
<body>
<table width="960" align="center">
<tr>
<td id="head" colspan="2"></td>
</tr>
<tr>
<td id="nav" width="200"></td>
<td id="main" style="vertical-align:top"></td>
</tr>
<tr>
<td id="foot" colspan="2"></td>
</tr>
</table>
</body>
</html>页面效果如下图所示:点击提交按钮则出现如下错误提示:请各位大虾给予解答!
struts.xml的配置文件如下(action的配置)
<?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>
<package name="json" namespace="/" extends="json-default">
<action name="addUser" class="addUser" method="add">
<result type="json"/>
</action>
</package>
</struts> addUser.action的代码如下:
package per.ssh.action.admin;import java.io.IOException;
import com.opensymphony.xwork2.ActionSupport;import per.ssh.dao.Admin.Admin;
import per.ssh.services.admin.adminInterface;public class adminAction extends ActionSupport{
private boolean success;
private String msg="";
/**
* @return the success
*/
public boolean isSuccess() {
return success;
}
/**
* @param success the success to set
*/
public void setSuccess(boolean success) {
this.success = success;
}
/**
* @return the msg
*/
public String getMsg() {
return msg;
}
/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this.msg = msg;
}
private User user;
public User getUser(){
return this.user;
}
public void setUser(User user){
this.user=user;
}
private adminInterface testService=null;
public void setTestService(adminInterface testService){
this.testService=testService;
}
public String add() throws IOException{
Admin admin=new Admin(user.getUserName(),user.getPassword(),Integer.parseInt(user.getUserLevel()));
try{
testService.insert(admin);
success=true;
msg="添加管理员成功!";
}
catch(Exception ex){
success=false;
msg="用户名或密码错误!";
}
System.out.println(user.getUserName()+user.getPassword()+user.getUserLevel());
return SUCCESS;
}
}extjs的代码如下:
Ext.ns("per.extjs.userManager");
Ext.apply(Ext.form.VTypes,{
confrimPwd:function(val,field){
if(field.initalPassword){
var pwd=Ext.getCmp(field.initalPassword);
return (val==pwd.getValue());
}
return true;
},
confrimPwdText:'两次密码输入不一致!'
});
per.extjs.userManager.userForm=function(){
var boxStore=new Ext.data.SimpleStore({
fields:['user','level'],
data:[['超级管理员','1'],['一般管理员','2']]
});
var userLevel=new Ext.form.ComboBox({
id:'userLevel',
name:'user.userLevel',
fieldLabel:'用户类别',
forceSelection:true,
triggerAction:'all',
editable:false,
labelWidth:100,
width:125,
store:boxStore,
displayField:'user',
valueField:'level',
mode:'local',
value:'1',
width:600
});
var userName=new Ext.form.TextField({
id:'userName',
name:'user.userName',
fieldLabel:'用 户 名',
allowBlank:false,
blankText:'用户名不能为空!',
msgTarget:'under',
width:600
});
var password=new Ext.form.TextField({
id:'password',
name:'user.password',
fieldLabel:'用户密码',
allowBlank:false,
blankText:'密码不能为空',
inputType:'password',
msgTarget:'under',
width:600
});
var confrimPassword=new Ext.form.TextField({
id:'cpwd',
name:'cpwd',
fieldLabel:'重复密码',
inputType:'password',
initalPassword:'password',
vtype:'confrimPwd',
allowBlank:false,
blankText:'重复密码不能为空',
msgTarget:'under',
width:600
});
per.extjs.userManager.userForm.superclass.constructor.call(this,{
id:'userForm',
title:'添加管理员',
frame:true,
autoHeight:true,
applyTo:'main',
labelAlign:'right',
buttonAlign:'center',
labelWidth:'100',
width:760,
collapsible:true,
animCollapse:true,
bodyStyle:'padding:5px;',
items:[
{
xtype:'fieldset',
title:'管理员信息',
autoHeight:true,
autoWidth:true,
items:[userLevel,userName,password,confrimPassword]
}
],
buttons:[
{
text:'提交',
handler:submit
},{
text:'取消',
handler:reset
}
]
});
function submit(){
Ext.getCmp('userForm').form.submit({
clientValidation:true,
//waitMsg:'正在提交数据,请稍等...',
//waitTitle:'提示',
url:'addUser.action',
method:'POST',
//params:{userLevel:userComboBox,userName:userName,password:userpassword},
success:function(form,action){
Ext.Msg.alert('提示',"添加管理员成功!");
},
failure:function(form,action){
Ext.Msg.alert('提示',action.failureType+":"+action.getUrl());
}
});
}
function reset(){
Ext.getCmp('userForm').form.reset();
}
}
Ext.extend(per.extjs.userManager.userForm,Ext.form.FormPanel,{}); 相应的jsp页面代码如下:<%@ page language='java' import='java.util.*' pageEncoding='GBK'%>
<!-- <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> --><!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<base>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/extjs/ext-all-debug.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/pages/common/common.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/pages/userManager/userManager.js"></script>
<title>My JSP "test.jsp" starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
Ext.onReady(function(){
Ext.BLANK_IMAGE_URL="${pageContext.request.contextPath}/extjs/resources/images/default/s.gif",
Ext.QuickTips.init();
var top=new per.extjs.common.top();
var nav=new per.extjs.common.nav();
var foot=new per.extjs.common.foot();
var form=new per.extjs.userManager.userForm();
});
</script>
</head>
<body>
<table width="960" align="center">
<tr>
<td id="head" colspan="2"></td>
</tr>
<tr>
<td id="nav" width="200"></td>
<td id="main" style="vertical-align:top"></td>
</tr>
<tr>
<td id="foot" colspan="2"></td>
</tr>
</table>
</body>
</html>页面效果如下图所示:点击提交按钮则出现如下错误提示:请各位大虾给予解答!
我怎么感觉应该是
<action name="addUser" class="per.ssh.action.admin.adminAction" method="add"> 呢
这么说来,建议你直接在浏览器里输入addUser.action,看看返回JSON串跟你想要的能不能对上
<bean id="addUser" class="per.ssh.action.admin.adminAction">
<property name="testService" ref="testService"/>
</bean>
{"msg":"用户名或密码错误!","success":false,"user":{"cpwd":null,"password":null,"userLevel":"1","userName":"tj05407"}}
我是在jsp做表现层时返回类型设置为json时得到的json串,但是数据确实已经插入数据库了,怎么回事呢?请解答