今天第一次接触ajax
在页面把要处理的sql传给Action:
$.ajax({
type: "POST",
url: URL+'!dealSql.action',
data: {Sql : Sql},
cache: false,
dataType: "json",
timeout: REQUEST_TIME,
error: function (XMLHttpRequest, textStatus, errorThrown) {
////
},
beforeSend: function(XMLHttpRequest){
//////
}
});在Action 中进行处理,setAttribute将结果再传回来(同一页面):
public String dealSql(){
HttpServletRequest request = Struts2Utils.getRequest();
HttpServletResponse response = Struts2Utils.getResponse();
String templateSql = getQueryParameter(request, "Sql");
List<String> names=new ArrayList<String>();
Pattern p = Pattern.compile("(##\\w*##)");
Matcher m = p.matcher(templateSql);
while(m.find()){
names.add(m.group());
}
request.setAttribute("names", names);
return "build";
}
然后jsp中:${names } 得不到想要的数据.......
------解决方案--------------------
使用AJAX的时候你的request还是之前的请求的
所以为了获取数据要在回调函数中使用js操作
------解决方案--------------------
参考 json
------解决方案--------------------
你都跳转了,当前页肯定获取不到names了,让这个方法返回值为空,在回调函数里就不能遍历动态生成页面了?