`
lushuaiyin
  • 浏览: 677910 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

jquery--ajax实例(配置是重点)

 
阅读更多

package org.lsy.test.ajax.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.lsy.test.ajax.service.AjaxService;
import org.lsy.test.ajax.service.AjaxServiceImpl;
import org.rd.framework.common.container.ContainerManager;
import org.rd.framework.struts.action.BodyListAction;
import org.rd.framework.struts.action.CommonAction;
import org.rd.framework.util.ContextUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CompareDateAction  extends CommonAction {
//	private AjaxService ajaxService = (AjaxService)ContainerManager.getComponent(AjaxService.AJAX_BEAN_ID);
	
	private AjaxServiceImpl ajaxService = new AjaxServiceImpl();
	private String result; 
	
	public String execute() throws Exception{
		
		return SUCCESS;
	}

	public String isRightDate() throws Exception{
		
		//ContextUtil.put("dd","df", ContextUtil.SCOPE_SESSION);
//		ActionContext ctx = ActionContext.getContext();
//		HttpServletRequest req=(HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
//		HttpServletResponse resp=(HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
		
		HttpServletRequest req=ServletActionContext.getRequest();
		HttpServletResponse resp=ServletActionContext.getResponse();
		
		resp.setContentType("text/xml");
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		PrintWriter pw = resp.getWriter(); 
		
		String whichdate= ""; 
		String datefrom = ""; 
		String dateto = ""; 
		if(req.getParameter("datefrom")!=null){
			datefrom=req.getParameter("datefrom").toString();
		}
		if(req.getParameter("dateto")!=null){
			dateto=req.getParameter("dateto").toString();
		}
		if(req.getParameter("whichdate")!=null){
			whichdate=req.getParameter("whichdate").toString();
		}
		//"[{'jj':'jjaa'}]"
		if(datefrom.trim().equals("")||dateto.trim().equals("")){
			pw.println("Y");
		}else if(ajaxService.isRightDate(datefrom, dateto)){
			pw.println("Y");
		}else{
			if(whichdate.trim().equals("from")){
				pw.println("N1");
			}else if(whichdate.trim().equals("to")){
				pw.println("N2");
			}
		}
		
		
		return NONE;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
}


package org.lsy.test.ajax.service;

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

import org.rd.framework.dao.impl.CommonServiceImpl;


public class AjaxServiceImpl  extends CommonServiceImpl implements AjaxService{

	/**
	 * 判断第一个日期是否小于等于第二个日期
	 */
	public boolean isRightDate(String datefrom,String dateto){
		boolean flag=false;
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date1=null;
		Date date2=null;
		try {
			date1=sdf.parse(datefrom);
			date2=sdf.parse(dateto);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("date1--"+date1.getTime()+"--date2--"+date2.getTime());
		
		flag=(date1.getTime()<=date2.getTime());
		
		return flag;
	}
	

}


package org.lsy.test.ajax.service;

import java.util.List;

import org.lsy.test.work.model.Work;


public interface AjaxService {
	public static final String AJAX_BEAN_ID = "ajaxService";	
	
	public boolean isRightDate(String datefrom,String dateto);
	
	
}


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/WEB-INF/crm_taglist.tld" prefix="crm"%>
<%@ taglib uri="/WEB-INF/component.tld" prefix="cx"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ page import="java.util.*,org.lsy.test.work.model.Work"%>

<%
String path = request.getContextPath();
  String basepath=request.getScheme()+"://"+
                  request.getServerName()+":"+request.getServerPort()+
                  request.getContextPath()+"/";
%>





<input type="hidden" id="basepath" value="<%=basepath %>" />


<script src="<%=basepath %>work/jquery-1.7.1.min.js"></script>

<%-- --%>
<!-- data start -->
<script src="<%=basepath %>work/ui/jquery.ui.core.js"></script>
<script src="<%=basepath %>work/ui/jquery.ui.widget.js"></script>
<script src="<%=basepath %>work/ui/jquery.ui.datepicker.js"></script>

<link rel="stylesheet" href="<%=basepath %>work/themes/base/jquery.ui.all.css">
<link rel="stylesheet" href="<%=basepath %>work/demos/demos.css">
<!-- data end -->

<!-- tabs start -->
    <link rel="stylesheet" href="<%=basepath %>work/themes/base/jquery.ui.all.css">
	<script src="<%=basepath %>work/ui/jquery.ui.tabs.js"></script>
<!-- tabs end -->


<script>
$(function() {

	$( "#reviewdate" ).datepicker({
		showOn: "button",
		buttonImage: "<%=basepath %>work/demos/images/calendar.gif",
		buttonImageOnly: true
	});

	$( "#reviewdate" ).datepicker( "option", "dateFormat", 'yy-mm-dd');

});
$(function() {

	$( "#reviewdatefrom" ).datepicker({
		showOn: "button",
		buttonImage: "<%=basepath %>work/demos/images/calendar.gif",
		buttonImageOnly: true
	});

	$( "#reviewdatefrom" ).datepicker( "option", "dateFormat", 'yy-mm-dd');

});
$(function() {

	$( "#reviewdateto" ).datepicker({
		showOn: "button",
		buttonImage: "<%=basepath %>work/demos/images/calendar.gif",
		buttonImageOnly: true
	});

	$( "#reviewdateto" ).datepicker( "option", "dateFormat", 'yy-mm-dd');

});
//////////////////////////////////////////
$(function() {
		$( "#tabsquery" ).tabs({
			collapsible: true
		});
	});

//////////////////////////////////////////

//////////////////////////////////////////

	
</script>

<%
  List res=new ArrayList();
  if(session.getAttribute("worklist")!=null){
	  res=(ArrayList)session.getAttribute("worklist");
  }
%>


<input type="hidden" id="thelen" value="" />
<input type="hidden" id="" value="" />
<input type="hidden" id="" value="" />

<table>
  <tr width="100%">
    <td width="50%">
      
            <table border="1">
                 <tr>
                    <td>
                    <form id="lsy_test_work" name="lsy_test_work" method="post" action="">
                    
                    <!-- 查询区 start -->
                    <div class="demo">
                    <div id="tabsquery">
	                    <ul>
							<li><a href="tabsquery-1">关闭查询区</a></li>
						</ul>
                    
                    <div id="tabsquery-1">
	                    <p><strong>打开查询区</strong>11</p>
	                    <p>22
		                    <table>
		                     <tr>
		                     <td>
					                        作业标题:<input type="text" id="worktitle" value=""/><br>
					                        老师:<input type="text" id="teacher"  value=""/><br>
					         课程名称:<input type="text" id="course"  value=""><br>
					                        学生姓名:<input type="text" id="student"  value=""><br>
					                        
					                        批改日期:从<input type="text" id="reviewdatefrom"    value=""><br>
					                        到:<input type="text" id="reviewdateto"    value=""><br>
					                        <br>
					                        <input type="button" onclick="querywork()"  value="查询"/>
					             </td>
					             </tr>
					         </table>
				         </p>
			         </div>
			         </div>
			         </div><!-- End demo -->
			         
			         <!-- 查询区 end -->
                    
			         </form>
                    </td>
                 </tr>
                 <tr>
                    <td>
                            <table  id="maindatatable" border="1">
			  <tr>
				<th>作业标题</th>
				<th>老师 </th>
				<th>课程名称</th>
				<th>内容</th>
				<th>学生姓名</th>
				<th>班级名称</th>
				<th>id</th>
			  </tr>
			  <%
			  if(res!=null){
			    for(int i=0;i<res.size();i++){
			    	
			    	Work ww=(Work)res.get(i);
			  %>
			  <tr  onclick="f1(<%=i+1%>)">
				<td><%=ww.getWorktitle() %></td>
				<td><%=ww.getTeacher() %></td>
				<td><%=ww.getCourse() %></td>
				<td><%=ww.getWorkcontent() %></td>
				<td><%=ww.getStudent() %></td>
				<td><%=ww.getClassname() %></td>
				<td><%=ww.getId() %></td>
			  </tr>
			  <%} }%>
			 
			  
			</table>
                    
                    </td>
                 </tr>
            </table>
           
    </td>
    <td>
           <table>
              <form  name="addform" id="addform" action="<%=path%>/work/addwork.action" method=post>
              <table>
				<tr>
				  <td width="30%" align="right">作业标题:</td>
				  <td><input type="text" id="eworktitle" name="work.worktitle" value=""></td>
				  <td width="30%" align="right">老师:</td>
				  <td><input type="text" id="eteacher" name="work.teacher" value=""></td>
				</tr>
				<tr>
				  <td width="30%" align="right">课程名称:</td>
				  <td><input type="text" id="ecourse"  name="work.course"  value=""></td>
				  <td width="30%" align="right">内容:</td>
				  <td><input type="text" id="eworkcontent"  name="work.workcontent"  value=""></td>
				</tr>
				<tr>
				  <td width="30%" align="right">学生姓名:</td>
				  <td><input type="text" id="estudent"  name="work.student"  value=""></td>
				  <td width="30%" align="right">班级名称:</td>
				  <td><input type="text" id="eclassname"   name="work.classname"  value=""></td>
				</tr>
				<tr>
				  <td width="30%" align="right">批改日期:</td>
				  <td><input type="text" id="reviewdate"    value=""></td>
				  <td width="30%" align="right"></td>
				  <td></td>
				</tr>
				
				<tr>
				  <td width="30%" align="right"></td>
				  <td><input type="button" id="exinzeng"  onclick="addwork()" value="新增"></td>
				  <td><input type="button" id="exinzeng"  onclick="delwork()" value="删除"></td>
				  <td><input type="button" id="eedit"  onclick="editwork()" value="提交修改"></td>
				</tr>
	<input type="hidden" id="eid"  value="" />
              </table>
              </form>
            
            
           </table>
    </td>
  </tr>
</table>







<%
  String actiontype="";
  if(request.getAttribute("actiontype")!=null){
	  actiontype=(String)request.getAttribute("actiontype");
  }
%>

<input type="hidden" id="actiontype" value="<%=actiontype %>" />

<script>
//var basepath=$("#basepath").val();
var basepath=document.getElementById("basepath").value;
//alert(basepath);
var len = $("#maindatatable tr td").length;
var thelen=len/6;

function f1(rownum){
	//alert("dddddd");
	var str="#maindatatable tr:eq("+rownum+")";
	document.getElementById("eid").value=$("#maindatatable tr:eq("+rownum+")").children().eq(6).html();
	document.getElementById("eworktitle").value=$("#maindatatable tr:eq("+rownum+")").children().eq(0).html();
	document.getElementById("eteacher").value=$("#maindatatable tr:eq("+rownum+")").children().eq(1).html();
	document.getElementById("ecourse").value=$("#maindatatable tr:eq("+rownum+")").children().eq(2).html();
	document.getElementById("eworkcontent").value=$("#maindatatable tr:eq("+rownum+")").children().eq(3).html();
	document.getElementById("estudent").value=$("#maindatatable tr:eq("+rownum+")").children().eq(4).html();
	document.getElementById("eclassname").value=$("#maindatatable tr:eq("+rownum+")").children().eq(5).html();
	
	
	//alert($("#maindatatable tr:eq(1)").children().eq(1).html());
	//alert($("#maindatatable tr:eq(1)").children().eq(2).html());
	//alert($("#maindatatable tr:eq(1)").children().eq(3).html());
	//alert($("#maindatatable tr:eq(1)").children().eq(4).html());
	//alert($("#maindatatable tr:eq(1)").children().eq(5).html());
	//alert($("#maindatatable tr:eq(1)").children().eq(6).html());
	//alert($(this).html());
	 //alert($(this).text());
}	
	function addwork(){
		var eworktitle=document.getElementById("eworktitle").value;
		var eteacher=document.getElementById("eteacher").value;
		var ecourse=document.getElementById("ecourse").value;
		var eworkcontent=document.getElementById("eworkcontent").value;
		var estudent=document.getElementById("estudent").value;
		var eclassname=document.getElementById("eclassname").value;
		
		/*var url=basepath+"work/addwork.do?worktitle="+eworktitle+
				"&teacher="+eteacher+
				"&course="+ecourse+
				"&workcontent="+eworkcontent+
				"&student="+estudent+
				"&classname="+eclassname;*/
		//document.getElementById("tt").value=url;
		//window.location.href=url;
		document.addform.submit();
	}
	function delwork(){

		var worktitle=document.getElementById("eworktitle").value;
		var teacher=document.getElementById("eteacher").value;
		var course=document.getElementById("ecourse").value;
		var student=document.getElementById("estudent").value;
		
		var workcontent=document.getElementById("eworkcontent").value;
		var classname=document.getElementById("eclassname").value;
		var id=document.getElementById("eid").value;
		
		var url=basepath+"work/delwork.action?worktitle="+worktitle+
		"&teacher="+teacher+
		"&course="+course+
		"&student="+student+
		"&workcontent="+workcontent+
		"&classname="+classname+
		"&id="+id;
		//alert("del--"+url);
		window.location.href=url;
	}
	function editwork(){
		var worktitle=document.getElementById("eworktitle").value;
		var teacher=document.getElementById("eteacher").value;
		var course=document.getElementById("ecourse").value;
		var student=document.getElementById("estudent").value;
		
		var workcontent=document.getElementById("eworkcontent").value;
		var classname=document.getElementById("eclassname").value;
		
		var id=document.getElementById("eid").value;
		
		var url=basepath+"work/editwork.action?worktitle="+worktitle+
		"&teacher="+teacher+
		"&course="+course+
		"&student="+student+
		"&workcontent="+workcontent+
		"&classname="+classname+
		"&id="+id;
		window.location.href=url;
	}
    function querywork(){

		var worktitle=document.getElementById("worktitle").value;
		var teacher=document.getElementById("teacher").value;
		var course=document.getElementById("course").value;
		var student=document.getElementById("student").value;
		
		var url=basepath+"work/querywork.action?worktitle="+worktitle+
		"&teacher="+teacher+
		"&course="+course+
		"&student="+student;
		//alert("查询--"+url);
		window.location.href=url;
	}
    
    
    var actiontype=document.getElementById("actiontype").value;
    if(actiontype=='otherquery'){
    	var url=basepath+"work/querywork.action";
		window.location.href=url;
    }

////////////////////////////////////////////////

$(document).ready(function(){
	//hideCol(1);//隐藏第二列
	//$('table td').find('td:eq(1)').hide();
	//$('table tr').find('td:eq(1)').hide();
	//$('#maindatatable td').find('td:eq(0)').hide();
	//$('#maindatatable tr').find('td:eq(0)').hide();
	
	var len = $("#maindatatable tr td").length;
	var thelen=len/6;
	//var len = $("#mm tr td").length;
	//alert(len/6);
	//alert("表行数"+thelen);
	$("#thelen").val(thelen);
	
	
	$("#reviewdatefrom").change( function () { 
		
		var date1=$("#reviewdatefrom").val();
		var date2=$("#reviewdateto").val();
		
		var url=basepath+"ajaxutil/isrightdate.action?datefrom="+date1+
		"&dateto="+date2+"&whichdate=from";
		
		alert($("#reviewdatefrom").val()+"\r"+url); 
		
		jQuery.ajax( {  
			        url:url,  
			        type:'post',  
			        async:false,  
			        dataType: 'text',
			        //data:'sql='+'hkhkhhhkhk',  
			        success:function(result){
			        	//var tt=eval('('+result+')');
			        	var ff=$.trim(result);
			        	if('Y'==ff){
			        		
			        	}else if(ff=='N1'){
			        		alert("批改日期范围不正确"); 
			        		$("#reviewdatefrom").val("");
			        	}else if(ff=='N2'){
			        		alert("批改日期范围不正确"); 
			        		$("#reviewdateto").val("");
			        	}
			       }  
			     }); 
	});//end blur
	
	$("#reviewdateto").change( function () { 
		var date1=$("#reviewdatefrom").val();
		var date2=$("#reviewdateto").val();
		
		var url=basepath+"ajaxutil/isrightdate.action?datefrom="+date1+
		"&dateto="+date2+"&whichdate=to";
		
		alert($("#reviewdateto").val()+"\r"+url);
		
		$.post(url, function(result){
			var ff=$.trim(result);
			if('Y'==ff){
        		
        	}else if(ff=='N1'){
        		alert("批改日期范围不正确"); 
        		$("#reviewdatefrom").val("");
        	}else if(ff=='N2'){
        		alert("批改日期范围不正确"); 
        		$("#reviewdateto").val("");
        	}
		}
		,'text');//end get
	
	});//end blur
	
	
	
});//end ready
/////////////////////////////////////////
</script>


struts:

<?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">
<struts>
	<package name="ajaxutil" extends="struts-default" namespace="/ajaxutil">
		<!-- ajaxutil begin -->
		
		<action name="isrightdate" class="org.lsy.test.ajax.action.CompareDateAction" method="isRightDate">
		     
		</action>
		
		<!-- ajaxutil end -->
	</package>
</struts>

spring:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

	<bean id="workService" parent="transactionProxyTemplate">
		<property name="target">
			<bean class="org.lsy.test.ajax.service.AjaxServiceImpl">
				<property name="sessionFactory">
					<ref bean="sessionFactory"/>
				</property>
			</bean>
		</property>
		<property name="proxyInterfaces">
			<value>org.lsy.test.ajax.service.AjaxService</value>
		</property>
	</bean>
</beans>




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics