先看下页面....
<%@ page contentType="text/html;charset=GBK" pageEncoding="GBK" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>extDemo</title> <link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/js/ext-2.2/resources/css/ext-all.css" /> <script type="text/javascript" src="<%=request.getContextPath() %>/js/ext-2.2/adapter/ext/ext-base.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/js/ext-2.2/ext-all.js"></script> </head> <body> <script type="text/javascript"> Ext.onReady( function(){ //使用表单提示 Ext.QuickTips.init(); Ext.form.Field.prototype.msgTarget = 'side'; //定义表单 var simple = new Ext.FormPanel({ labelWidth: 75, baseCls: 'x-plain', defaults: {width: 150}, defaultType: 'textfield',//默认字段类型 //定义表单元素 items: [ { fieldLabel: 'Username', name: 'name',//元素名称 //anchor:'95%',//也可用此定义自适应宽度 allowBlank:false,//不允许为空 blankText:'Please input your name'//错误提示内容 }, { inputType:'password', fieldLabel: 'Password', //anchor:'95%', name: 'pws', allowBlank:false, blankText:'Password is empty' } ], buttons: [{ text: 'Login', type: 'submit', //定义表单提交事件 handler:function(){ if(simple.form.isValid()){//验证合法后使用加载进度条 Ext.MessageBox.show({ title: 'Please wait a moment.', msg: 'Loading...', 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 < 13; i++){ setTimeout(f(i), i*150); } //提交到服务器操作 simple.form.doAction('submit',{ url:'LoginServlet',//文件路径 method:'post',//提交方法post或get params:'', //提交成功的回调函数 success:function(form,action){ alert("in"); if (action.result.msg=='ok') { document.location='success.jsp'; } else { Ext.Msg.alert('Login error',action.result.msg); } }, //提交失败的回调函数 failure:function(){ Ext.Msg.alert('Error','Server is error!'); } }); } } }, { text: 'Cancel', handler:function(){simple.form.reset();}//重置表单 } ] }); //定义窗体 win = new Ext.Window({ id:'win', title:'User Login', layout:'fit', //之前提到的布局方式fit,自适应布局 width:300, height:150, plain:true, bodyStyle:'padding:5px;', maximizable:false,//禁止最大化 closeAction:'close', closable:false,//禁止关闭 collapsible:true,//可折叠 plain: true, buttonAlign:'center', items:simple//将表单作为窗体元素嵌套布局 }); win.show();//显示窗体 }); </script> </body> </html>
再看下服务端...
package demo; import java.io.IOException; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html;charset=gb2312"); request.setCharacterEncoding("gb2312"); try { String user=request.getParameter("name"); String pasd=request.getParameter("pws"); String flag=""; System.out.println("---------------------user:"+user+"----pwd:"+pasd); if(user!=null && !"".equals(user)){ if(pasd!=null && !"".equals(pasd)){ DBconnection conn = new DBconnection(); System.out.println("select * from user u where u.username="+user+" and u.password="+pasd); ResultSet rs = conn.getResult("select * from user u where u.username='"+user+"' and u.password='"+pasd+"'"); if(rs.next()){ flag="0"; } } } System.out.println("------------flag:"+flag); if(flag!=null && !"".equals(flag)){ //json形式返回数据,如何返回??? String json = "(success:true,msg:\'ok\')"; response.getWriter().write(json); response.getWriter().flush(); System.out.println(json); response.sendRedirect("index.jsp"); } else{ String json = "(success:true,msg:\'Login fail\')"; response.getWriter().write(json); response.getWriter().flush(); System.out.println(json); response.sendRedirect("index.jsp"); } } catch(Exception ex){ } } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doGet(request,response); } }
问题出在从服务端返回客户端时...页面出现脚本错误...指向页面的第24行...少')'号....哪位朋友知道帮帮忙....先谢了..初学Ext,所用API为2.2版本.
另外还想问一下,从服务端回显json格式数据时,以什么方式?是用out,还是write还是其它什么? :)