(1)将struts2-json-plugin-2.3.1.2.jar拷贝到web-inf/lib目录下。
(2)修改struts.xml,添加包,继承json-default,并添加action子节点。struts.xml代码:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.objectFactory" value="spring"/>
<package name="demo" extends="struts-default">
<action name="demo" class="demoAction" method="execute">
<result name="success">/demo/demo.jsp</result>
</action>
</package>
<package name="demo-json" extends="json-default">
<action name="json_*" class="demoAction" method="{1}">
<result name="success" type="json" />
</action>
</package>
</struts>
(3)修改DemoAction.java,添加getByCode()方法,实现接收前台参数code-按该值查询数据库-得到name值-传递给前台显示。DemoAction.java代码:
package com.xy6;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
public class DemoAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private final Logger logger = Logger.getLogger(DemoAction.class.getName());
private String code;
private String name;
@Autowired
private DemoDao demoDao;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute(){
DemoHelper demoHelper = new DemoHelper();
System.out.println("---curren year:"+demoHelper.getCurYear());
DemoVO demoVO = new DemoVO();
demoVO.setCode(1);
List<DemoVO> listDemoVO = demoDao.selectByCode(demoVO);
for(int i=0;i <listDemoVO.size(); i++){
System.out.println("---i:"+listDemoVO.get(i).toString());
}
this.testLog4j();
return "success";
}
public void testLog4j(){
logger.info("info");
logger.warn("warn");
logger.debug("debug");
logger.error("error");
logger.fatal("fatal");
}
public String getByCode(){
int iCode = Integer.valueOf(this.getCode());
DemoVO demoVO = new DemoVO();
demoVO.setCode(iCode);
List<DemoVO> listDemoVO = demoDao.selectByCode(demoVO);
for(int i=0;i <listDemoVO.size(); i++){
name = listDemoVO.get(i).getName();
break;
}
return "success";
}
}
(4)修改demo.jsp,在页面显示单击按钮。demo.jsp代码:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="test json"/>
</body>
<script type="text/javascript" src="<%=request.getContextPath()%>/demo/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/demo/demo.js"></script>
</html>
(5)下载jquery-1.9.1.min.js,将其拷贝到web/demo文件夹下。在web/demo文件夹下新建文件demo.js。
(6)修改demo.js,实现单击按钮时,发起ajax请求,获取数据,弹出显示。demo.js代码:
$("input").click(function(){
var code = 1;
$.ajax({
type : "post",
dataType: "json",
data :{
"code" : code
},
url : "json_getByCode.action",
success : readcallback
});
});
function readcallback(data){
alert(data.name);
}
(7)重启域服务,浏览器访问http://localhost:9001/web/demo.action,页面显示一个按钮test json,单击按钮,弹出提示ajax获得的数据。如下图:
(8)至此,json配置结束。