lib中加入包
struts2-json-plugin-2.3.20.jar
commons-lang3-3.2.jar
WebRoot下新建js文件夹
放入json2.js
json2.js是一个著名开源js库,提供了json的序列化和反序列化方法 JSON.stringify:L353:把Json对象转换成字符串 JSON.parse: L400:把字符串转成Json对象 http://www.json.org/ 有 JavaScript: JSON. json2.js. clarinet. Oboe.js.
ajaxUtil.js 自己封装的一个Ajax工具类
// 参考:XMLHttpRequest 对象 // http://www.w3school.com.cn/xml/xml_http.asp xmlhttp = null; if (window.XMLHttpRequest) {// code for all new browsers xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) {// code for IE5 and IE6 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } function ajaxRequest(url, param, callBackFunction) { try { if (xmlhttp != null) { var sUrl = url + ‘?date=‘ + new Date(); var oRequest = new XMLHttpRequest(); oRequest.onreadystatechange = function() { if (oRequest.readyState == 4) { callBackFunction(oRequest); } }; oRequest.open(‘POST‘, sUrl); oRequest.setRequestHeader("Content-Type", "application/json"); oRequest.send(param); } } catch (e) { } }
index.jsp核心代码:
<script type="text/javascript" src="js/ajaxUtil.js"></script> <script type="text/javascript" src="js/json2.js"></script> <script type="text/javascript"> function checkuser(o) { var url = "testjson" var obj = { "uname" : o.value } // 借助json2.js:Json对象→字符串,以便发送 var params = JSON.stringify(obj); // 借助ajaxUtil.js:发送Ajax请求 ajaxRequest(url, params, ajaxCallBack); } // Ajax回调函数 function ajaxCallBack(oRequest) { // 借助json2.js:字符串→Json对象,以便解析 var obj1 = JSON.parse(oRequest.responseText); document.getElementById("msgdiv").innerHTML = obj1.msg1; } </script>
<p id="msgdiv"></p> <s:form action="" name="test" method="post"> <s:textfield name="uname" onblur="checkuser(this)" /> <s:submit></s:submit> </s:form>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 不再继承struts-default,而是json-default --> <package name="p1" namespace="/" extends="json-default"> <action name="testjson" class="org.ah.s2.C1"> <!-- 配置json拦截器,在struts2-json-plugin-2.3.20.jar中 --> <interceptor-ref name="json" /> <result type="json"> <!-- root:固定值,返回给客户端的对象 --> <!-- data:Action中的一个属性 --> <param name="root">data</param> <!-- 当返回结果有空值时,不包含进来 --> <!-- 这里AhJsonBean.msg2不包含进来 --> <param name="excludeNullProperties">true</param> </result> </action> </package> </struts>
AhJsonBean:
package org.ah.s2; /** * Json用的Java Bean */ public class AhJsonBean { private String msg1; private String msg2; public String getMsg1() { return msg1; } public void setMsg1(String msg1) { this.msg1 = msg1; } public String getMsg2() { return msg2; } public void setMsg2(String msg2) { this.msg2 = msg2; } }
Action:
package org.ah.s2; import com.opensymphony.xwork2.ActionSupport; public class C1 extends ActionSupport { private String uname; // ----------------------------- public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } // ----------------------------- // <param name="root">data</param> // 不需要setter,需要getter,需要new private AhJsonBean data; public AhJsonBean getData() { return data; } // ----------------------------- @Override public String execute() throws Exception { // 需要new一个Json的对象 data = new AhJsonBean(); if ("admin".equals(uname)) { data.setMsg1("这是管理员"); } else { data.setMsg1("OK!"); } return ActionSupport.SUCCESS; } }
运行一下:输入aa,焦点移开:
时间: 2024-10-25 13:10:56