Struts2+JSON+prototype使用说明
Ajax技术用了一段时间了,用了几个ajax框架,也写过没有用ajax框架的程序,总体感觉prototype简单易用,因此写一下使用说明1.下载Strutes2的JSON插件:jsonplugin-0.19.jar
2.添加一个JSON的action
import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class JsonAction extends ActionSupport{
private final Logger log = Logger.getLogger(getClass());
private String isTrue;
public String CheckPhone(){
isTrue = "true";
return SUCCESS;
}
public String getIsTrue() {
return isTrue;
}
public void setIsTrue(String isTrue) {
this.isTrue = isTrue;
}
如果不想返回JSON则在某属性的get方法中加上@JSON(serialize=false)
3.配置action
<action name="CheckTestPhoneAction" class="JsonAction " method="CheckPhone">
<result type="json"/>
</action>
4.页面中使用prototype
1)声明使用prototype
<script src="<%=request.getContextPath()%>/js/prototype-1.6.0.3.js"></script>
2)js方法
function activeSub(action,type){
//请求的地址
var url = 'CheckTestPhoneAction.shtml';
//将form1表单域的值转换为请求参数
var params = Form.serialize('viewTestPhoneForm');
//创建Ajax.Request对象,对应于发送请求
var myAjax = new Ajax.Request(
url,
{
//请求方式:POST
method:'post',
//请求参数
parameters:params,
//指定回调函数
onComplete: confirmCheckTestPhoneBack,
//是否异步发送请求
asynchronous:true
});
}
function confirmCheckTestPhoneBack(request){
var jsonObj = request.responseJSON;
alert(jsonObj.isTrue);
}
呵呵,用起来挺简单!!
页:
[1]