之前一段时间做项目对Struts2有所了解,这里做个记录。
Struts2可以返回json字符串,比如,返回到页面上一大段json代码,
下面我们看代码:
这是action里面的代码,看到返回值是空,并不是string,因为不需要跳转任何页面,只需要向页面写数据即可。这里我们运用最经典的response向页面写入数据。这之前刚开始学servlet的时候就学过的。
/**
* ajax管辖机构数据
* @return
*/
public void jurisdiction() throws IOException{
this.printToJsp(getJurDate());//向页面打印字符串
}
/**
* 向页面打印
* @author hjh
* @return
*/
public void printToJsp(String content) throws IOException{
/*
* 在调用getWriter之前未设置编码(既调用setContentType或者setCharacterEncoding方法设置编码),
* HttpServletResponse则会返回一个用默认的编码(既ISO-8859-1)编码的PrintWriter实例。这样就会
* 造成中文乱码。而且设置编码时必须在调用getWriter之前设置,不然是无效的。
* */
response.setContentType("text/html;charset=gbk");
//response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//JSON在传递过程中是普通字符串形式传递的,这里简单拼接一个做测试
out.println(content);
out.flush();
out.close();
}
xml:配置文件,直接配置一个action即可,由于没有页面进行跳转,所以这里没有result选项,这样代码会写入到ajax返回处
<!--ajax得到管辖机构数据-->
<action name="jurisdiction" class="com.hljw.health.plat.action.pcommunity.JurisdictionAction" method="jurisdiction">
<interceptor-ref name="hljw-user"></interceptor-ref>
</action>
看看是不是很神奇,但是这个有个致命的缺点,就是你如果要得到html片段的话,你必须拼接大量的html代码,这样你会疯的。所以下面这个方法可以直接返回一个html片段,你得到这个片段之后,直接向指定的页面标签用html(data)写入即可。
action里面的代码:
这个代码返回的是有页面的,这样的话Struts可以把整个页面都返回给调用处作为一个html片段使用。同时在回调函数里面也可以使用,$(data).find("p")来寻找片段某个特定的标签,很好使用的。
public String ajaxHealthMonitorInfo(){
if("0".equals(flag)){//列表
return "list";
}else{//K线图
return "k";
}
}
xml代码:直接写要跳转的页面即可,很简单的。
<!-- ajax一个人监控信息,K线图 -->
<action name="ajaxHealthMonitorInfo" class="com.hljw.health.plat.action.healthmonitoring.HealthmonitoringAction" method="ajaxHealthMonitorInfo">
<result name="list">/WEB-INF/jsp/plat/healthmonitoring/ajaxview.jsp</result>
<result name="k">/WEB-INF/jsp/plat/healthmonitoring/ajaxkline.jsp</result>
</action>
html片段:引入一些常见的标签,就跟页面展示一样,页面不需要html,head,body之类的,仅仅是你需要展示到ajax调用处的片段是什么即可。
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<div class="right_topbox1">
<div class="rttitle"><c:if test="${pageList0.listData.size()==4}"><a href="${rootPath}/plat/healthmonitoring/list.htm?id=${queryBean1.id}&healthMonitor.list=0">更多</a></c:if>本地心电监测数据</div>
</div>
<div class="tab_bk2">
<table class="list_tab">
<c:if test="${empty pageList0.listData}">
<tr class="row" onmouseover="this.className='overrow'" onmouseout="this.className='row'" align="center">
<td>暂无数据</td>
</tr>
</c:if>
<c:if test="${not empty pageList0.listData}">
<tr class="head">
<th>发送时间</th>
<th>监测数据</th>
<th>正常/异常</th>
</tr>
<c:forEach items="${pageList0.listData}" var="p">
<tr class="row" onmouseover="this.className='overrow'" onmouseout="this.className='row'" align="center">
<td><fmt:formatDate value="${p.postDate}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>心率:${p.heartrate}</td>
<td>
<c:choose>
<c:when test="${p.isException=='0'}"><img src="${rootPath}/images/healthmonitor/ok.png" title="正常"/></c:when>
<c:when test="${p.isException=='1'}"><img src="${rootPath}/images/healthmonitor/error.png" title="异常"/></c:when>
<c:otherwise><img src="${rootPath}/images/healthmonitor/unknown.png" title="未知"/></c:otherwise>
</c:choose>
</td>
</tr>
</c:forEach>
</c:if>
</table>
</div>
</div>
jsp页面ajax调用处:
//监控数据展示
function ajaxMonitor(flag,obj){
$('.selected').removeClass("selected");
$(obj).addClass("selected");
$('#jianceshuju').html("正在加载请稍后...");
$.ajax({
async:true,
cache:false,
data:{id:"${queryBean1.id}",flag:flag},
dataType:"html",
type:"post",
url: "${rootPath}/plat/healthmonitoring/ajaxHealthMonitorInfo.htm",
success: function(data){//回调函数,$(data).find("p")来寻找片段某个特定的标签,很好使用的。
if(flag=="0"){//列表展示
$('#jianceshuju').html(data);
}else if(flag=="1"){//图形展示
$('#jianceshuju').html($(data).html());
kline();
}
}
});
}