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 AjaxService 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);
resp.setContentType("text/xml");
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter pw = resp.getWriter();
String datefrom = "";
String dateto = "";
if(req.getParameter("datefrom")!=null){
datefrom=req.getParameter("datefrom").toString();
}
if(req.getParameter("dateto")!=null){
dateto=req.getParameter("dateto").toString();
}
//"[{'jj':'jjaa'}]"
if(datefrom.trim().equals("")||dateto.trim().equals("")){
pw.println("Y");
this.result="{'re':'Y'}";
}else if(ajaxService.isRightDate(datefrom, dateto)){
pw.println("Y");
// this.result="Y";
this.result="{'re':'Y'}";
}else{
pw.println("N");
// this.result="N";
this.result="{'re':'N'}";
}
return NONE;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
<?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>
<%@ 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" οnclick="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 οnclick="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" οnclick="addwork()" value="新增"></td>
<td><input type="button" id="exinzeng" οnclick="delwork()" value="删除"></td>
<td><input type="button" id="eedit" οnclick="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?reviewdatefrom="+date1+
"&reviewdateto="+date2;
alert($("#reviewdatefrom").val()+"\r"+url);
jQuery.ajax( {
url:url,
type:'post',
async:false,
dataType: "text",
//data:'sql='+'hkhkhhhkhk',
success:function(result){
alert("from-----333----"+result);
if(""!=result){
alert("from---------"+result);
}
}
});
});//end blur
$("#reviewdateto").change( function () {
var date1=$("#reviewdatefrom").val();
var date2=$("#reviewdateto").val();
var url=basepath+"ajaxutil/isrightdate.action?reviewdatefrom="+date1+
"&reviewdateto="+date2;
alert($("#reviewdateto").val()+"\r"+url);
$.get(url, function(result){
alert("Data hhss: " + result);
});//end get
});//end blur
});//end ready
/
</script>
在这个例子中private String result;其实是没有用的,我们一般会这样配置
<struts>
<package name="ajaxutil" extends="json-default" namespace="/ajaxutil">
<!-- ajaxutil begin -->
<action name="isrightdate" class="org.lsy.test.ajax.action.CompareDateAction" method="isRightDate">
<result name="success" type="json">
<param name="root">result</param>
</result>
</action>
<!-- ajaxutil end -->
</package>
</struts>
并且ajax这样写:
jQuery.ajax( {
url:url,
type:'post',
async:false,
dataType: "json",
//data:'sql='+'hkhkhhhkhk',
success:function(result){
alert("from-----333----"+result);
if(""!=result){
alert("from---------"+result);
}
}
});
这样我们把要返回的值指定放到java类的resuest变量中,在xml指定返回成功后去这个参数的值给ajax的
回调函数。
而在我这个例子中,我们继承的是extends="struts-default",没有返回,在java类中返回NONE。
ajax中dataType: "text",这样返回的内容就不是到private String result;了,而是pw.println("Y");
(pw是PrintWriter pw = resp.getWriter();),返回结果用PrintWriter输出给回调函数。