大致上依然可以用ognl方式來做
Ex:
Ajax.jsp:
$(function() {
$("#getJson").click(function() {
var params = {
name : $("#name").val(),
email: $("#email").val()
};
$.ajax({
type: "POST",
url: "jsonAjax",
data: params,
dataType:"text", //ajax返回值text(json格式也可用這返回,也可設成json)
success: function(json){
var obj = $.parseJSON(json); //使用此方法解析json
var state_value = obj.result; //result是和action中的result變數的get方法對應的
alert(state_value);
},
error: function(json){
alert("json=" + json);
return false;
}
});
});
});
</script>
input type="text" id="name" name="name"
input type="text" id="email" name="email"
input type="button" id="getJson"
ActionSupport:
public JsonAction extends ActionSupport{
private String name;
private String email ;
private String result;
-- name、email、result.... get set --
public String execute() throws Exception {
// TODO Auto-generated method stub
if("小王".equals(name)) {
result = "小王mail為:" + email;
} else
result = "非本人";
return SUCCESS;
}
}
struts.xml
<action name="jsonAjax" class="xxx.xxxJsonAction ">
<!-- 返回類型為json -->
<result type="json"></result>
</action>
</package>
此範例參考:http://blog.csdn.net/edagarli/article/details/12858329
另外範例參考:http://www.blogjava.net/max/archive/2007/06/12/123682.html
留言列表