java中調用ajax返回html格式

1.在jsp頁面中

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ taglib uri="webwork" prefix="ww" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Inser here</title>
<script src="/js/jquery-1.8.1.js" type="text/javascript"></script>
<script type="text/javascript">

//回調函數
function callback(data){
    $("#checkmenunamespan").html("");
    $("#checkmenunamespan").html(data);
}

function search(id){
     var parentName = id;
     var params = "aboutId=" + parentName;
    $.ajax({
       type:"POST",             
       url:"/aboutlistAction.action?actionType=detailAbout",
       dataType:"html",
       data: params,
       success:callback,
      
      });
    
}
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0"  style="margin-top:20px;">
            <tr>
              <td class="t2">關於我們 →</td>
            </tr>
            <tr>
              <td><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-top:10px;">
                
                <ww:if test="aboutlist3 != null">
                <ww:iterator value="aboutlist3">
                <div>  <a href="javascript:search(<ww:property value='id'/>);" ><ww:property value="title"/></a></div>
                </ww:iterator>
                </ww:if>
               <ww:else>暫無數據</ww:else>
              </table></td>
            </tr>
          </table>
          <br />
</body>
</html>
在配置模塊
<!-- 關於我們模塊 -->
<action name="aboutlistAction" class="com.hxxy.pub.action.AboutAction">
    <result name="detail_about" type="dispatcher">/ajax_about.jsp</result>
</action>

action裏面的相關代碼

package com.hxxy.pub.action;

import java.sql.SQLException;
import java.util.List;

import com.hxxy.framework.BaseAction;
import com.hxxy.pojo.About;
import com.hxxy.pojo.News;
import com.hxxy.query.QueryAbout;
import com.hxxy.query.QueryNews;
import com.hxxy.service.AboutService;
import com.hxxy.service.NewsService;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.QEncoderStream;

public class AboutAction extends BaseAction {

    private static final long serialVersionUID = 1L;
    private AboutService aboutService;
    private About about;
    private QueryAbout queryAbout;
    private String actionType;

    private int aboutId;
    private List<QueryAbout> aboutlist3;

    public String execute() throws SQLException {

        // 根據id查詢新聞
        if (actionType != null && actionType.equals("detailAbout")) {
            about = aboutService.QueryAboutInfoByid(aboutId);
            if (about != null)
                return "detail_about";
            else
                return "error";
        }
        return "error";
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章