在JSP页面怎么利用Ajax和jQuery得到结果集
4个回答
展开全部
page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ajax</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url:"test.action",
type:"post",
dataType:"json",
success:function(data){
for(var i in data)
$("#d").append("<input type='text' value='"+data[i].name+"' /><br />");
}
});
})
</script>
</head>
<body>
<div id="d"></div>
</body>
</html>
struts.xml:
<package name="struts" extends="json-default">
<action name="test" class="com.test.action.testAction" method="jsonString">
<result name="success" type="json">
/jsonString.jsp
</result>
</action>
</package>
action:
public class testAction extends ActionSupport {
private String wenBen;
public String getWenBen() {
return wenBen;
}
public void setWenBen(String wenBen) {
this.wenBen = wenBen;
}
public String jsonString() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "zhang");
map.put("sex", "男");
map.put("age", "22");
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("name", "zhang1");
map1.put("sex", "男1");
map1.put("age", "221");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "zhang111");
map2.put("sex", "男111");
map2.put("age", "22111");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(map);
list.add(map1);
list.add(map2);
wenBen = JSONArray.fromObject(list).toString();
System.out.println(wenBen);
pw.write(wenBen);
pw.flush();
return null;
}
}
需要导入json jar包
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ajax</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url:"test.action",
type:"post",
dataType:"json",
success:function(data){
for(var i in data)
$("#d").append("<input type='text' value='"+data[i].name+"' /><br />");
}
});
})
</script>
</head>
<body>
<div id="d"></div>
</body>
</html>
struts.xml:
<package name="struts" extends="json-default">
<action name="test" class="com.test.action.testAction" method="jsonString">
<result name="success" type="json">
/jsonString.jsp
</result>
</action>
</package>
action:
public class testAction extends ActionSupport {
private String wenBen;
public String getWenBen() {
return wenBen;
}
public void setWenBen(String wenBen) {
this.wenBen = wenBen;
}
public String jsonString() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "zhang");
map.put("sex", "男");
map.put("age", "22");
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("name", "zhang1");
map1.put("sex", "男1");
map1.put("age", "221");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("name", "zhang111");
map2.put("sex", "男111");
map2.put("age", "22111");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(map);
list.add(map1);
list.add(map2);
wenBen = JSONArray.fromObject(list).toString();
System.out.println(wenBen);
pw.write(wenBen);
pw.flush();
return null;
}
}
需要导入json jar包
展开全部
Jsp是服务器端的,不能直接操题目中的作结果集合。
你的意思是不是:用Ajax或者jQuery发出用户请求,得到Jsp服务器tomcat返回的Json或者特殊的信息,然后操作该Response改变html代码?
那么是可以的!学习的Jsp比较少,可能有误:
--------------------------------------------------------------------------------------------------------------------
import net.sf.json.JSONObject;
JSONObject json= new JSONObject();
json.put("id", 1);
json.put("site", "www.baidu.com");
json.put("time", "xxx");
response.getWriter().print(json); // 输出
Js端,查看参考资料。
你的意思是不是:用Ajax或者jQuery发出用户请求,得到Jsp服务器tomcat返回的Json或者特殊的信息,然后操作该Response改变html代码?
那么是可以的!学习的Jsp比较少,可能有误:
--------------------------------------------------------------------------------------------------------------------
import net.sf.json.JSONObject;
JSONObject json= new JSONObject();
json.put("id", 1);
json.put("site", "www.baidu.com");
json.put("time", "xxx");
response.getWriter().print(json); // 输出
Js端,查看参考资料。
参考资料: http://www.jb51.net/shouce/jquery/jquery_api/Ajax/$.ajax.html
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$(function(){
$("#mybut").click(function(){
$.ajax({
url:"${pageContext.request.contextPath}/UserServlet",
type:"post",
success:function(data){
alert(data); //这里的data就是servlet中 out.print();的值
}
});
});
});
$("#mybut").click(function(){
$.ajax({
url:"${pageContext.request.contextPath}/UserServlet",
type:"post",
success:function(data){
alert(data); //这里的data就是servlet中 out.print();的值
}
});
});
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$.ajax({
url:"xxxx";
method:"post",
dataType:"text",
success:function(data){
//data就是你返回的结果集
}
});
url:"xxxx";
method:"post",
dataType:"text",
success:function(data){
//data就是你返回的结果集
}
});
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询