java jsp jquery json 二级联动菜单

来源:互联网 发布:itunes windows xp 编辑:程序博客网 时间:2024/03/29 08:08

关键代码:

javascript:

<script type="text/javascript"> function onchangeShow(oneId){  $.ajax({  url : "<%=basePath%>myjsps/manager/click_getJsonCategory.do",  data : {serviceid : oneId},  // 参数  type : "post",  cache : false,  dataType : "json",  //返回json数据 error: function(){ alert('error');      }, success:onchangecallback  });  }  function onchangecallback(data){   document.getElementById('adid').options.length = 0;  //清空原有的option var str="<option value='00000'>请选择</option>";  for(var i=0;i<data.length;i++){  str+="<option value='"+data[i].id+"'>"+data[i].id+"--"+data[i].val+"</option>"  }  $("#adid").html(str);  }  </script> 


jsp页面:

<s:select id="serviceid" label="对应产品" name="serviceid" list="#{'100000':'--请选择--','100001':'100001-图铃','100005':'100005-签名','100008':'100008-考   考你','100013':'100013-爱情魔法','100015':'100015-星座','100018':'100018-运势','100012':'100012-接口', '100006':'100006-藏头诗'}" onchange="onchangeSho   w(this.value)"/></td>    </tr>    <tr>       <td width="20%" class="labeltable_middle_td_01">广告推广链接</td>       <td width="80%">            <select id="adid" name="ad_id">       <option value="0">--请选择--</option>      </select> 


java代码:

//查询所有广告 public String getJsonCategory() throws Exception{ EntityDao dao = new EntityDao(Sys_adcontent.class); List<Sys_adcontent> ad = dao.getList("select * from sys_adcontent where serviceid="+this.serviceid);//这里从数据库取得数据 JSONArray jsonObj = new JSONArray();;//;//组装成json数据 for(int i=0;i<ad.size();i++) { JSONObject obj = new JSONObject(); Sys_adcontent adcontent = ad.get(i); obj.put("id", adcontent.getId()); obj.put("val", adcontent.getTitle()); jsonObj.put(obj); } sendMessage(jsonObj.toString());//向视图push json数据 return null; } /** * des:封装发送json格式的数据回js * autho:exceljava * date:Nov 20, 2009 * @param content * @throws IOException */ public void sendMessage(String content) throws IOException{      HttpServletResponse response = ServletActionContext.getResponse();      response.setCharacterEncoding("UTF-8");      response.getWriter().write(content);   }


完整代码:


jsp页面: 

<%@ page contentType="text/html; charset=utf-8" %> 
<%@ page import="java.util.*,com.union.dao.Sys_adcontent " %> 
<%@ include file="/jsp/common/jsphead.jsp"%> 
<% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"; 
%> 
<script type="text/javascript" src="js/jquery.js"></script> 
<jsp:useBean id="chk" class="com.itc.web.tools.InputCheck" scope="page"/> 
<% 
  chk.setFormName("clickForm"); 
  chk.CheckNull("begintime","起始时间不可为空"); 
  //chk.CheckNull("endtime","结束时间不可为空"); 
  out.println(chk.getCheckScript()); 
%> 
<script type="text/javascript"> 
function onchangeShow(oneId){  
$.ajax({  
url : "<%=basePath%>myjsps/manager/click_getJsonCategory.do",  
data : {serviceid : oneId},  // 参数  
type : "post",  
cache : false,  
dataType : "json",  //返回json数据 
error: function(){ 
alert('error');      
}, 
success:onchangecallback  
});  
}  
function onchangecallback(data){   
document.getElementById('adid').options.length = 0;  //清空原有的option 
var str="<option value='00000'>请选择</option>";  
for(var i=0;i<data.length;i++){  

str+="<option value='"+data[i].id+"'>"+data[i].id+"--"+data[i].val+"</option>"  
}  
$("#adid").html(str);  

}  

</script> 
<s:form method="POST" action="click_query"  name="clickForm" namespace="/myjsps/manager" onsubmit="return InputCheck()"> 
<table class="labeltable_middle_table"> 
  <tr> 
    <td width="100%" colspan="2"  class="labeltable_middle_td">广告点击率查询</td> 
  </tr> 
<tr> 
      <td width="20%" class="labeltable_middle_td_01">开始时间</td> 
      <td width="80%"><s:textfield id="begintime" name="begintime" onfocus="WdatePicker({});" readonly="readonly" size="50" maxlength="50" /></td> 
</tr> 
<tr> 
      <td width="20%" class="labeltable_middle_td_01">结束时间</td> 
      <td width="80%"><s:textfield id="endtime" name="endtime" onfocus="WdatePicker({});" readonly="readonly" size="50" maxlength="50" /></td> 
</tr> 
<tr> 
      <td width="20%" class="labeltable_middle_td_01">渠道</td> 
      <td width="80%"><s:select id="serviceid" label="对应产品" name="serviceid" list="#{'100000':'--请选择--','100001':'100001-图铃','100005':'100005-签名','100008':'100008-考考你','100013':'100013-爱情魔法','100015':'100015-星座','100018':'100018-运势','100012':'100012-接口', '100006':'100006-藏头诗'}" onchange="onchangeShow(this.value)"/></td> 
</tr> 
<tr> 
      <td width="20%" class="labeltable_middle_td_01">广告推广链接</td> 
      <td width="80%"> 
     
      <select id="adid" name="ad_id"> 
      <option value="0">--请选择--</option> 
      
      </select> 
      </td> 
</tr>

</table> 
<p align="center"> 
    <input type="submit" value="确定" name="btnOk" class="button">&nbsp;&nbsp; 
    <input type="button" value="取消" name="btnCancel" class="button" onclick="history.go(-1)"> 
</p> 
</s:form> 
<%@ include file="/jsp/common/jspfoot.jsp"%> 

java action: 
package com.union.web.action.sys; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import javax.servlet.http.HttpServletResponse; 

import org.apache.struts2.ServletActionContext; 
import org.jfree.data.time.Day; 
import org.jfree.data.time.TimeSeries; 
import org.jfree.data.time.TimeSeriesCollection; 
import org.json.JSONArray; 
import org.json.JSONObject; 

import com.itc.jdbc.DbUtils; 
import com.opensymphony.xwork2.ActionContext; 
import com.union.dao.Sys_adcontent; 
import com.union.utils.DateUtils; 
import com.union.utils.StringUtils; 
import com.union.web.common.BaseAction; 
import com.union.web.service.GoldAnalyzeManager; 
import com.union.web.utils.EntityDao; 


/** 
* @author lucas 

* 2009-12-10 
*/ 
@SuppressWarnings("serial") 
public class AdclickAction extends BaseAction { 
private String begintime; 
private String endtime; 
private String serviceid; 
//查询广告链接点击率 
public String query() throws Exception{ 
ActionContext cx = ActionContext.getContext(); 
int clicktotal = 0; //点击总数 
int regtotal = 0; //注册总数 
if (StringUtils.isNull(begintime)) { 
return INPUT; 

if(StringUtils.isNull(endtime)) { 
endtime = DateUtils.getShortNow(); 


String serviceid = getRequest().getParameter("serviceid"); 
int ad_href = StringUtils.parseInt(getRequest().getParameter("ad_id")); 
String sql = "select count(*) as total, left(a.subtime, 10) as stime from ("; 
sql += " select * from sys_click_log"; 
sql += " where length(mobileid) >= 11 and left(subtime, 10) between '"+begintime+"' and '"+endtime+"'"; 
sql += " group by mobileid"; 
sql += " ) as a join sys_adcontent as b on a.adid=b.id "; 
sql += "where left(a.subtime, 10) between '"+begintime+"' and '"+endtime+"'"; 
if(!serviceid.equalsIgnoreCase("100000")) { 
sql+= " and b.serviceid= '"+serviceid+"'"; 

if(ad_href!=0) { 
sql+= " and b.id= "+ad_href; 


sql += " group by stime"; 
String[][] result = DbUtils.getResultSetArray(sql); 
TimeSeries click = new TimeSeries("点击数"); 
List<Map<String,Object>> clickday = new ArrayList<Map<String,Object>>(); 
for(String[] s : result) { 
clicktotal += StringUtils.parseInt(s[0]); 
click.add(new Day(DateUtils.getShortDate(s[1])), StringUtils.parseInt(s[0])); 
Map<String,Object> map =new HashMap<String,Object>(); 
map.put("day", DateUtils.getShortDateString(DateUtils.getShortDate(s[1]))); 
map.put("click", StringUtils.parseInt(s[0])); 
clickday.add(map); 

TimeSeriesCollection dataset=new TimeSeriesCollection(); 
    dataset.addSeries(click); 
    
    sql = "select count(*), left(a.addtime, 10) as stime from ui_fire_user a, sys_adcontent b where a.adid = b.id and left(a.addtime, 10) between '"+begintime+"' and '"+endtime+"'"; 
    if(!serviceid.equalsIgnoreCase("100000")) { 
sql+= " and b.serviceid= '"+serviceid+"'"; 

if(ad_href!=0) { 
sql+= " and b.id= "+ad_href; 

sql += " group by stime"; 
result = DbUtils.getResultSetArray(sql); 
    TimeSeries reg = new TimeSeries("注册数"); 
    List<Map<String,Object>> regday =new ArrayList<Map<String,Object>>(); 
for(String[] s : result) { 
regtotal += StringUtils.parseInt(s[0]); 
reg.add(new Day(DateUtils.getShortDate(s[1])), StringUtils.parseInt(s[0]));
Map<String,Object> map =new HashMap<String,Object>(); 
map.put("day", DateUtils.getShortDateString(DateUtils.getShortDate(s[1]))); 
map.put("reg", StringUtils.parseInt(s[0])); 
regday.add(map); 

    dataset.addSeries(reg); 
    String filename = ServletActionContext.getServletContext().getRealPath("jfree")+"/adclickaction.jpg"; 
    GoldAnalyzeManager.generateXYLineChart(filename, "广告推广渠道统计图", "点击数", dataset, true); 
    
    cx.put("clickday", clickday); 
    cx.put("regday", regday); 
cx.put("register", regtotal); 
cx.put("count", clicktotal); 
return "queryresult"; 


//查询所有广告 
public String getJsonCategory() throws Exception{ 
EntityDao dao = new EntityDao(Sys_adcontent.class); 
List<Sys_adcontent> ad = dao.getList("select * from sys_adcontent where serviceid="+this.serviceid);//这里从数据库取得数据 


JSONArray jsonObj = new JSONArray();;//;//组装成json数据 
for(int i=0;i<ad.size();i++) { 
JSONObject obj = new JSONObject(); 
Sys_adcontent adcontent = ad.get(i); 
obj.put("id", adcontent.getId()); 
obj.put("val", adcontent.getTitle()); 
jsonObj.put(obj); 


sendMessage(jsonObj.toString());//向视图push json数据 

return null; 

/** 
* des:封装发送json格式的数据回js 
* autho:exceljava 
* date:Nov 20, 2009 
* @param content 
* @throws IOException 
*/ 
public void sendMessage(String content) throws IOException{      
HttpServletResponse response = ServletActionContext.getResponse();      
response.setCharacterEncoding("UTF-8");      
response.getWriter().write(content);   
}
 
public String getBegintime() { 
return begintime; 


public String getEndtime() { 
return endtime; 


public void setBegintime(String begintime) { 
this.begintime = begintime; 


public void setEndtime(String endtime) { 
this.endtime = endtime; 


public String getServiceid() { 
return serviceid; 


public void setServiceid(String serviceid) { 
this.serviceid = serviceid; 

0 0