看了不少文章,也看了不少代碼。始終找不到自己最出錯在什麼地方了,現在直接上代碼吧。然後說一下注意的地方.
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><br /><%@include file="/common/taglib.jsp" %><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br /><html><br /> <head><br /> <title>My JSP 'index.jsp' starting page</title><br /><meta http-equiv="pragma" content="no-cache"><br /><meta http-equiv="cache-control" content="no-cache"><br /><meta http-equiv="expires" content="0"><br /><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><br /><meta http-equiv="description" content="This is my page"><br /><%@include file="/common/meta.jsp" %><br /><mce:script type="text/javascript"><!--<br />Ext.onReady(function(){<br />var lform = new Ext.form.FormPanel({<br />frame: true,<br />width: 250,<br />height: 130,<br />labelWidth: 60,<br />method: 'post',<br />url: 'servlet/ExtServlet',<br />items:[<br /> {fieldLabel: '使用者名稱', xtype: 'textfield', name: 'name', width: 150, allowBlank: true, vtype:'email'},<br />{fieldLabel: '密碼', xtype: 'textfield', name: 'password', width: 150, inputType: 'password'},<br />{fieldLabel: '出生日期', xtype: 'datefield', name: 'birthday', width: 150}<br />],<br />buttons:[<br />{text: '登入', handler: login},<br />{text: '取消'}<br />]</p><p>});<br />function login() {<br />lform.form.submit({<br />waitMsg: '請稍候正在登入...',<br />success: function(form, action) {<br />alert(action.result.success);<br />},<br />failure: function(form, action) {<br />alert('no');<br />}<br />});<br />};<br />lform.render('grid');<br />});</p><p>// --></mce:script><br /> </head></p><p> <body><br /> <!--<br /> <input type="button" id="b"/> --><br /> <div id="grid" style="margin-left: 20px; margin-top: 5px" mce_style="margin-left: 20px; margin-top: 5px"></div></p><p> </body><br /></html><br />
servlet代碼:
package com.tsts.sunyanan.ext;</p><p>import java.io.IOException;<br />import java.io.PrintWriter;</p><p>import javax.servlet.ServletException;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;</p><p>@SuppressWarnings("serial")<br />public class ExtServlet extends HttpServlet {</p><p>private String getResult() {<br />StringBuffer sb = new StringBuffer();<br />//{success:true, message: 'ok'}<br />sb.append("{success:true, message:'a'}");<br />///sb.append("{/"result/":[{/"r/":/"true/", /"m/":/"a/"}]}");<br />return sb.toString();<br />}</p><p>/**<br /> * The doGet method of the servlet. <br><br /> *<br /> * This method is called when a form has its tag value method equals to get.<br /> *<br /> * @param request the request send by the client to the server<br /> * @param response the response send by the server to the client<br /> * @throws ServletException if an error occurred<br /> * @throws IOException if an error occurred<br /> */<br />public void doGet(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {<br />System.out.println("get");<br />response.setContentType("text/x-json");<br />PrintWriter out = response.getWriter();<br />out.println(getResult());<br />out.flush();<br />out.close();<br />}</p><p>/**<br /> * The doPost method of the servlet. <br><br /> *<br /> * This method is called when a form has its tag value method equals to post.<br /> *<br /> * @param request the request send by the client to the server<br /> * @param response the response send by the server to the client<br /> * @throws ServletException if an error occurred<br /> * @throws IOException if an error occurred<br /> */<br />public void doPost(HttpServletRequest request, HttpServletResponse response)<br />throws ServletException, IOException {<br />System.out.println("post");<br />response.setContentType("text/x-json");<br />PrintWriter out = response.getWriter();<br />out.println(getResult());<br />out.flush();<br />out.close();<br />}</p><p>}<br />
首先是 jsp中,method: 'get', url: 'xxx', 最開始寫成了 action: 'xxx',總是找不到錯誤。無奈看了一點原始碼忽然看到這個屬性。
其次,頁面怎麼判斷你到底是success了還是failure了呢,這個取決於背景json。
也就是說背景json也不能亂寫 應該按照這個格式來 {success:true} 這樣前台才知道你是成功了。
自己可以在{success:true, message:'login ok'}
這樣來添加自己的別的代碼。
記錄一下,歡迎拍磚。
網上的確沒有說的這麼詳細的東西
在前台得到json訊息的時候是這樣子的。參數2個名字按照順序為 form,action action為返回的json資料
但是json.result才到了json的開始,如果是success的話 alert(action.result.success) 顯示為true。
剩下的以此類推。