在前端页面通过异步发送请求到后台时,后台接受请求处理后返回,在struts2中前台的请求发送到后台后交由相应的action来处理,所以ajax请求的url形式类似于“/ActionClass.action?params=x&...”
(1)环境:首先要在工程中添加依赖的包,后台需要json数据格式的支持,而且struts2需要能够支持json,所以在maven工程的pom.xml文件中加入以下配置:
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.3</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.3.24</version> </dependency>
(2)struts2的配置文件:
<package name="front" namespace="/" extends="json-default"> //支持json <action name="listContractsAjaxAction" class="contractAjaxAction" method="listContracts"> <result type="json"> //返回json格式数据,不需要加返回后的页面 <param name="root">contracts</param> </result> </action> </package>
(3)Action类:
package com.mz.action;
import com.mz.service.IContractService;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
/**
* Created by hadoop on 15-9-29.
*/
public class ContractAjaxAction extends ActionSupport {
private JSONArray contracts; //返回的json数据
private JSONObject countJson;
private IContractService contractService; //spring的注入
public JSONArray getContracts() { //数据的get和set方法
return contracts;
}
public void setContracts(JSONArray contracts) {
this.contracts = contracts;
}
public JSONObject getCountJson() {
return countJson;
}
public void setCountJson(JSONObject countJson) {
this.countJson = countJson;
}
public void setContractService(IContractService contractService) {
this.contractService = contractService;
}
public String countContracts(){
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");
String chars = request.getParameter("chars");
int count = contractService.getContractsSize(chars);
countJson = new JSONObject();
countJson.put("count", count);
return SUCCESS;
}
public String listContracts(){ //请求页面当前页内容
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=utf-8");
Integer pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
String chars = request.getParameter("chars"); //当前页数和页大小
contracts = new JSONArray();
List<Map<String, Object>> cbs = contractService.listContracts(pageIndex,pageSize,chars);//调用后台逻辑
for(Map<String, Object> cb : cbs) {
contracts.add(JSONObject.fromObject(cb));
}
return SUCCESS;
}
}
(action对应的类在spring的配置文件中设置)
总结:
前端ajax请求指定action,在struts2文件中根据action名对应的类及方法交由对应的方法处理,返回结果在配置文件中说明格式及名称,该名称是action类中属性,并需要定义set和get方法,这样返回的时候struts2才能将对应的属性的值按照说明的格式返回给前端。
相关内容
(1)Web开发:分页技术的实现(上)——jBootstrapPage.js+ajax
(2)Web开发:分页技术的实现(下)——Hibernate查询
(3)Web开发:Struts2 Spring Hibernate整合(一)——Struts2的使用
(4)Web开发:Struts2 Spring Hibernate整合(二)——Spring的使用