当前位置: 代码迷 >> JavaScript >> struts2 jquery json 兑现ajax技术 返回list
  详细解决方案

struts2 jquery json 兑现ajax技术 返回list

热度:404   发布时间:2012-09-10 11:02:32.0
struts2 jquery json 实现ajax技术 返回list
jsp页面:
      
  <input type="hidden" id="currentPage2" value="1">
 <br/><br/><div></div>
										<DIV id=in_send>
		<div align="left" style="padding-left:40px; font-weight:blod; margin-top:10px;" >发表评论:</div>
			<table width="720" border="0" cellspacing="5" cellpadding="0"><tr>
			<td align="left">姓 名:</td>
			<td  align="left"><input type="text" name="picCriticismName" id="picCriticismName" style="width:220px"  maxlength="49" tabindex=1>
			<div id="nmerror" style="float:left;" >*姓名最长为50字节</div></td></tr>
			<tr><td  align="left" valign="top" class="f14" id="reTitle">内 容:</td>
			<td  align="left" ><textarea name="picCriticismContent" id="picCriticismContent" style="width:520px;height:155px"  tabindex=4></textarea>
			</td></tr>
			<tr id="3_err" style="display:none"><td>&nbsp;</td><td><div class="error" id="3_err_con"></div></td></tr>
			<tr><td valign="top">&nbsp;</td>
			<td valign="top" align="left" >
			<input  type="button" onclick="good()" value="发表评论"  >
			</td>
			</tr></table>

										
										
										</DIV>
									</DIV>

xml配置文件:
  <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <!-- 
               作者:ljz
              时间:2010/09/15           
    -->
<struts>

  <package name="piccriticism"  extends ="json-default">
  <action name="addPicCriticism" class="PicCriticismBean" method="addPicCriticism">   
	
<result  name="success" type="json">
<param name="includeProperties">
	list5\[\d+\]\.criticismContent,list5\[\d+\]\.criticismName,list5\[\d+\]\.criticismIp,list5\[\d+\]\.criticismTime,list5\[\d+\]\.totalPage,list5\[\d+\]\.currentPage
</param>
</result>   
</action>
</package>
</struts>



package cn.ljzblog.ljz.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import test.SuperPicCriticims;
import cn.ljzblog.ljz.common.CalculatePage;
import cn.ljzblog.ljz.common.Page;
import cn.ljzblog.ljz.model.PicCriticism;
import cn.ljzblog.ljz.model.PicCriticismTemp;
import cn.ljzblog.ljz.service.PicCriticismServices;
import cn.ljzblog.ljz.util.ActionContextUtil;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;



/**
 * 相片评论
 * @author ljz
 * @time 2010/10/03
 */
public class PicCriticismAction extends ActionSupport implements ModelDriven<PicCriticismTemp>{
	 private PicCriticismTemp pt = new PicCriticismTemp();
	 private SuperPicCriticims sc = new SuperPicCriticims();
	 private ActionContextUtil acu = new ActionContextUtil();
	 private PicCriticism pc = new PicCriticism();
	 private String pictureDetail;
	 private String picCriticismName;
	  private String currentPage2 = "1";
	   private Page page = new Page();
	   private String firstOrLast = new String();//判断是否是首页或者是尾页
		private CalculatePage cal = new CalculatePage();
		private PicCriticismServices picCriticismServices;
		private int currentPage  = 1;
		private List<PicCriticismTemp> list5 ;
		private String picCriticismId;
   public String addPicCriticism(){
	   System.out.println("进入评论先相片action");
	   System.out.println(this.getFirstOrLast());
	   System.out.println(pt.getPicName()+"相片名称");
	   page.setPageSize(5);
	   //String sql = "select t.pic_criticism_content,t.piccriticismip,t.piccriticismtime,t.piccriticismname from t_pic_criticism t inner join t_picture pic on t.picture_id = pic.picture_id where pic.picture_name = '"+pt.getPicName()+"' order by t.pic_criticism_id desc";
	   String sql = "select t.pic_criticism_content,t.piccriticismip,t.piccriticismtime,t.piccriticismname from t_pic_criticism t inner join t_picture pic on t.picture_id = pic.picture_id where pic.picture_name = '"+pt.getPicName()+"' order by t.pic_criticism_id desc";
	   List<PicCriticismTemp> list4 = picCriticismServices.queryPicCriticismLength(sql);
	   System.out.println("list4大小:"+list4.size());
   	page.setTotalResultNumbers(list4.size());
   	page.setTotalPage(cal.getTotalPage(page.getTotalResultNumbers(), page.getPageSize()));
   	 if(this.getFirstOrLast().equals("first")){
   		 System.out.println("进入first");
    		currentPage = 1;
    	}else if(this.getFirstOrLast().equals("last")){
    		//currentPage = page.getTotalPage();
    		currentPage = cal.getTotalPage(list4.size(), page.getPageSize());    		
    		System.out.println("last页尾:"+currentPage);
    	}else if(this.getFirstOrLast().equals("next")){
    		currentPage = cal.nextPage(Integer.parseInt(this.getCurrentPage2()), cal.getTotalPage(list4.size(), page.getPageSize()));
    		System.out.println("next页尾:"+currentPage);
    	}else if(this.getFirstOrLast().equals("pre")){
    		currentPage = cal.previousPage(Integer.parseInt(this.getCurrentPage2()));
    		System.out.println("pre页尾:"+currentPage);
    	}else{
    		System.out.println("进入其他");
    		
       	Date currentTime = new Date();		  
   		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
   		pc.setPicCriticismContent(pt.getCriticismContent());
   		//System.out.println(pt.getCriticismIp());
   		pc.setPicCriticismIp(acu.getIpAddr(acu.getRequest()));
   		System.out.println(acu.getIpAddr(acu.getRequest())+"ip是");
   		pc.setPicCriticismName(this.getPicCriticismName());
   		pc.setPicCriticismTime(formatter.format(currentTime));
   		pc.setPictureId(Integer.parseInt(this.getPicCriticismId()));
   		System.out.println("评论内容:"+pt.getCriticismContent());
   		//System.out.println("评论者ip:"+this.getIpContent.split(";")[1]);
   		System.out.println("评论ip:"+pt.getCriticismIp());
   		System.out.println("评论者昵称:"+this.getPicCriticismName());
   		System.out.println("评论相片ID:"+this.getPicCriticismId());
         picCriticismServices.addPicCriticism(pc) ;    	
    		currentPage = Integer.parseInt(this.getCurrentPage2());
    		System.out.println("添加成功");
    	}
   	
   	 page.setCurrentPage(Integer.parseInt(this.getCurrentPage2()));
   	 System.out.println("当前是 :"+currentPage+":this:"+page.getCurrentPage());
   	 page.setBeginIndex(cal.pageToRow(this.getCurrentPage(), page.getPageSize()));
    	page.setEndIndex(cal.calLastIndex(this.getCurrentPage(), page.getPageSize()));
    	System.out.println(page.getTotalResultNumbers()+"-------------");
    	
   	//String sql = "from Criticism as criti where criti.articleId = ? order by criti.criticismId desc ";
    	System.out.println(":begin"+page.getBeginIndex()+":endindex"+page.getEndIndex());
   	list5 = picCriticismServices.queryPicCriticism(sql,  cal.pageToRow(this.getCurrentPage(), page.getPageSize()), cal.calLastIndex(this.getCurrentPage(), page.getPageSize()));
   	sc.setCurrentPage(this.getCurrentPage());
   	sc.setTotalPage(cal.getTotalPage(list4.size(), page.getPageSize()));
   	System.out.println("总共"+cal.getTotalPage(list4.size(), page.getPageSize()));
   	//sc.setPictureName(pt.getPicName());
   //	sc.setPicDetail(this.getPictureDetail());
   	list5.add(sc);
   	System.out.println(list5.size()+"list5.size");
    for(PicCriticismTemp pt :list5){
    	System.out.println(pt.getCriticismContent()+":"+pt.getCriticismIp()+":"+pt.getCriticismName()+":"+pt.getCriticismTime()+":");
    }
	   return "success";
   }
@Override
public PicCriticismTemp getModel() {	
	return pt;
}
public PicCriticismTemp getPt() {
	return pt;
}
public void setPt(PicCriticismTemp pt) {
	this.pt = pt;
}
public String getCurrentPage2() {
	return currentPage2;
}
public void setCurrentPage2(String currentPage2) {
	this.currentPage2 = currentPage2;
}
public Page getPage() {
	return page;
}
public void setPage(Page page) {
	this.page = page;
}
public String getFirstOrLast() {
	return firstOrLast;
}
public void setFirstOrLast(String firstOrLast) {
	this.firstOrLast = firstOrLast;
}
public CalculatePage getCal() {
	return cal;
}
public void setCal(CalculatePage cal) {
	this.cal = cal;
}
public PicCriticismServices getPicCriticismServices() {
	return picCriticismServices;
}
public void setPicCriticismServices(PicCriticismServices picCriticismServices) {
	this.picCriticismServices = picCriticismServices;
}
public int getCurrentPage() {
	return currentPage;
}
public void setCurrentPage(int currentPage) {
	this.currentPage = currentPage;
}


public SuperPicCriticims getSc() {
	return sc;
}
public void setSc(SuperPicCriticims sc) {
	this.sc = sc;
}
public String getPicCriticismId() {
	return picCriticismId;
}
public void setPicCriticismId(String picCriticismId) {
	this.picCriticismId = picCriticismId;
}
public PicCriticism getPc() {
	return pc;
}
public void setPc(PicCriticism pc) {
	this.pc = pc;
}
public String getPicCriticismName() {
	return picCriticismName;
}
public void setPicCriticismName(String picCriticismName) {
	this.picCriticismName = picCriticismName;
}
public List<PicCriticismTemp> getList5() {
	return list5;
}
public void setList5(List<PicCriticismTemp> list5) {
	this.list5 = list5;
}
public String getPictureDetail() {
	return pictureDetail;
}
public void setPictureDetail(String pictureDetail) {
	this.pictureDetail = pictureDetail;
}

}



由于返回的list不仅包含对象  我还想返回一些字符串  所以就写一个继承类来实现!
package test;

import cn.ljzblog.ljz.model.Criticism;
import cn.ljzblog.ljz.model.PicCriticismTemp;

public class SuperPicCriticims extends PicCriticismTemp {
      private int totalPage;
      private int currentPage;
      private String picLoginTime;
  	private String picDetail;
  	private String pictureName;
  	
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public String getPicLoginTime() {
		return picLoginTime;
	}
	public void setPicLoginTime(String picLoginTime) {
		this.picLoginTime = picLoginTime;
	}
	public String getPicDetail() {
		return picDetail;
	}
	public void setPicDetail(String picDetail) {
		this.picDetail = picDetail;
	}
	public String getPictureName() {
		return pictureName;
	}
	public void setPictureName(String pictureName) {
		this.pictureName = pictureName;
	}
      
}

返回前台jquery 处理!

 var firstOrLast='none';	
 var params;
function go() {

		validate('next')

	}
	function end() {
		validate('last');
	}
	function first() {
		validate('first');
	}
	function gg() {
		validate('pre')
	}
	
   var validate = function(flag) { 
	   if(flag=='none'){
		   //alert(flag+"证明可以啊");
			  params ={
					  CriticismIp:$("#picCriticismIp").val(),					
					  CriticismContent:$("#picCriticismContent").val(),
				      firstOrLast:flag,
				      picName:$("#currentPicName").val(),
				      picCriticismId:$("#picCriticismId").val(),
				      picCriticismName:$("#picCriticismName").val(),
				      currentPage2:$("#currentPage2").val()
				    }
		  }else{
			  params ={
					  picCriticismId:$("#picCriticismId").val(),
					  picName:$("#currentPicName").val(),
				      firstOrLast:flag,
				      currentPage2:$("#currentPage2").val()
				    }
		  }
	   if(flag=='query'){
		   firstOrLast='first';		 
	   }else if(flag=='first'){
		   firstOrLast = 'first';
	   }else if(flag == 'pre'){
		   firstOrLast ='pre';
	   }else if(flag == 'next'){
		   firstOrLast ='next';
	   }else if(flag == 'last'){
		   firstOrLast ='last';
	   }else if(flag == 'none'){
		   firstOrLast  ='none';
		   if(params.picCriticismName==""){
			     alert("昵称不能为空");
			     return ;
			   }else if(params.CriticismContent==""){
			    	alert("评论内容不能为空");
			    	return ;
			    }
		  
			   
	   }
	 
	var url='addPicCriticism.action';
	
  
    $.ajax({
         url:url, 
         type:'post',
         dataType:'json', 
         data:params,
         success:function(data){ 
    	
    	var index = 0;
    	
    	var tar = "<TABLE style=\"BORDER-COLLAPSE: collapse\"  border=\"0\" cellSpacing=\"0\"><TBODY>";
    	 tar+="<tr><td align=\"left\" colspan=\"2\">网友评论:</td></tr>";
    	 $.each(data.list5,function(i,value){   
    		 index = i;
    		
    	
    		 tar+="<tr id=0"+i+"><td align=\"left\">"+(i+1)+"&nbsp;&nbsp;"+value.criticismName+"说:&nbsp;&nbsp;&nbsp;"+value.criticismContent+"</td></tr>";
    		 tar+="<tr id=1"+i+"><td align=\"left\">ip:"+value.criticismIp+"</td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<td align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;时间 :"+value.criticismTime+"</td></tr>";             
                                     
         })
         
         
         
       
	tar+="<tr><td colspan=\"2\">  <table align=\"left\" width=\"100%\" id=\"table1\" style=\"display:none\">";
		tar +="<tr><td align=\"left\"><a href=\"javascript:void(0)\"><font id=\"totalpage\"  color=blue></font></a></td>";
		tar+="	<td align=\"left\"><a href=\"javascript:void(0)\"><font id=\"currentpage\" color=blue></font><font id=\"totalpage2\" color=blue></font></a>";
		tar+="</td><td><a onclick=\"first()\" style=\"cursor: hand\"><font color=blue>首页</font></a></td>";
		tar+="<td><a onclick=\"gg()\" style=\"cursor: hand\"><font color=blue>上一页</font></a></td><td>";
		tar+="<a onclick=\"go()\" style=\"cursor: hand\"><font color=blue>下一页</font></a></td><td>";
		tar+="<a onclick=\"end()\" style=\"cursor: hand\"><font color=blue>尾页</font></a></td></tr>";
	tar+="</table></tr>";
         
         
         tar+="</TBODY></TABLE>";
    	 $('#result').html(tar);    	
    	 var totalpage = data.list5[index].totalPage;
    	 var currentpage = data.list5[index].currentPage;
    	 $('#totalpage').html("总共"+totalpage+"页");
    	 $('#currentpage').html("当前"+currentpage+"/");
    	 $('#totalpage2').html(totalpage+"页");
    	 $("#currentPage2").val(currentpage);
    	
    	$('#0'+index).hide();
    	$('#1'+index).hide();
    	if(index==0){
    		 $("#table1").hide();
    		 var nocontent = "<span align=\"left\" ><font color=\"red\">沙发空缺中,请您快----------</font></span>";
    		 $('#result').html(nocontent);
    	}else{
    	 $("#table1").show();
    	 }
         }
      });
           
   }
	 

  相关解决方案