struts2 ajax json 异步刷新整合
来源:互联网 发布:windows 10有扫雷 编辑:程序博客网 时间:2024/06/09 20:05
必要的包有:struts2主要包,json包
1.html关键代码如下:
搜索代码:
<div id="searchtitle">
<div>
<div><input id="textfield" type="text" name="condition" value='<s:property value="condition"/>'/><input id="submit" type="button" value="搜索"onclick="search()"/>
</div>
</div>
</div>
<table width="765" border="0" cellpadding="0" cellspacing="0"
id="tableContent">
此处将填充异步刷新内容
</table>
2.ajax的js代码如下:
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function handleStateChange1()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var divObj = document.getElementById("tableContent");
var userinfos = eval('(' + xmlHttp.responseText + ')');//将action层返回的json格式数组转换为js的数组
if(userinfos.length > 0){
for(var i = 0; i < userinfos.length ;i++){
divObj.innerHTML = divObj.innerHTML + "<tr><td height='105' align='center' valign='middle' class='td2'>"+
"<img src='images/mainBannerContent2People2Img.gif' width='54' height='54' alt='' /></td>"+
"<td height='105' align='left' valign='bottom' class='td3'><font color='#005dc3' size='3' face='微软小黑'><b>"+userinfo[i].username+"</b></font> </td>"+//输出数值属性
"</tr>";
}
}else {
divObj.innerHTML = "<div>无结果</div>";
}
}
}
}
var obj = 0;
var condition;
function search(){
condition = document.getElementById("textfield");
createXMLHttpRequest();
var url = "SearchOne.action?condition="+condition.value+"&searchtype="+searchtype;//异步刷新的action
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = handleStateChange1;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(null);
}
3.action层代码如下:
package com.nbf.find.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;
import com.nbf.find.service.UserService;
import com.nbf.find.vo.Userinfo;
import com.opensymphony.xwork2.ActionSupport;
public class SearchOneAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 7848488788100591233L;
private UserService userService;
public String execute(){
int currentpage = 0;
int searchtype = 1;
List<Userinfo> userinfos = new ArrayList<Userinfo>(); //异步数据对象列表
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=UTF-8");//设置异步传输格式
response.setHeader("Cache-Control", "no-cache");//设置不缓存
String cp = request.getParameter("currentPage");
String st = request.getParameter("searchtype");
if(st != null){
searchtype = Integer.parseInt(st);
}
if(cp != null ){
currentpage = Integer.parseInt(cp);
}
String condition = null;
try {
condition = new String(request.getParameter("condition").getBytes("iso-8859-1"), "utf-8");//转换格式为utf-8防止乱码
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
if(searchtype == 1){//找号码
userinfos.addAll(userService.getUserinfoByUserId(Long.parseLong(condition)));//从service层得到对象列表
} else if(searchtype == 2){//找姓名
userinfos.addAll(userService.getUserinfosByUsername(condition, currentpage, 20));
}
JSONArray jsons = new JSONArray();//json数组格式
if(!userinfos.isEmpty()){
for(Userinfo userinfo : userinfos){
jsons.put(new JSONObject(userinfo));
}
}
PrintWriter writer = null;
try {
writer = response.getWriter();
writer.write(jsons.toString());//将json格式数组返回到客户端
} catch (IOException e) {
e.printStackTrace();
}finally{
writer.flush();
writer.close();
}
return SUCCESS;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserService getUserService() {
return userService;
}
}
4.struts2.xml配置
<action name="SearchOne" class="com.nbf.find.action.SearchOneAction">
<result>/searchone.jsp</result>
<interceptor-ref name="defaultStack"/>
</action>
- struts2 ajax json 异步刷新整合
- ajax+struts2+JQuery+json实现异步刷新
- Struts2+ajax+json整合简介
- Struts2+ajax+json整合简介
- struts2下利用ajax实现异步刷新
- ajax异步刷新得到json数据
- Struts2整合jQuery实现ajax异步登录
- 在struts2下实现ajax的异步刷新
- ajax实现异步刷新,并通过json接收发送数据!
- Struts2+jquery+ajax+json实现无刷新登陆验证.
- Struts2+ajax+json异步上传图片回显
- json+struts2+ajax+list实现异步传输一个列表
- ajaxfileupload.js、struts2、ajax、json异步上传图片
- 简单实用的json+struts2+ajax异步校验
- struts2+jquery实现ajax异步提交并返回json串
- struts2+jquery+Ajax异步请求发送与解析json数据
- struts2+jquery+Ajax异步请求发送与解析json数据
- struts2 json 与jquery整合实现ajax,用户注册校验
- C++程序设计语言学习笔记(一)
- CLOSE_WAIT生成的原因
- Remove Duplicates from Sorted List II
- oracle 触发器
- HTML5——canvas自定义饼图
- struts2 ajax json 异步刷新整合
- MySQL 数据库性能优化之缓存参数优化
- 浏览器标准CSS设置图片居中的方法
- 在Linux系统如何识别和打开摄像头
- poi读写excel数据
- day7 test02
- Tab系列
- Arduino笔记三气压模块BMP85
- 客户端分页处理器