做过的一个SSH+prototype.js+json=Ajax的例子

前端之家收集整理的这篇文章主要介绍了做过的一个SSH+prototype.js+json=Ajax的例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
先把代码放这里。
JSP页面和实现js
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'insertOneYear.jsp' starting page</title>

<Meta http-equiv="pragma" content="no-cache">
<Meta http-equiv="cache-control" content="no-cache">
<Meta http-equiv="expires" content="0">
<Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<Meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<script type="text/javascript" src="/AOS/workdate/scripts/prototype-1.6.0.3.js"></script><!--这个文件从网上可以找到-->
<script type="text/javascript" src="/AOS/workdate/scripts/validate.js"></script>
<script type="text/javascript" src="/AOS/workdate/scripts/workdate.js"></script>
<script type="text/javascript" src="/AOS/workdate/scripts/calendar.js"></script>
<script type="text/javascript" src="/AOS/workdate/scripts/InsertOneYear.js"></script>
<script type="text/javascript">
	function CheckOneYear(){
		//alert("进入函数");
		var url="/AOS/json/datema/CheckYearAction";//提交到action的url
		//alert("Year:"+document.getElementById("year").value);
		var params=Form.serialize("insertYearForm");
		//alert("客户端序列化成功");
		//alert("params:"+params);
		var request=new Ajax.Request(
			url,{method:"post",parameters:params,onComplete:FeedBack,asynchronous:true}		
		);
		//alert("请求发送成功");
	}
	function FeedBack(request){
		//alert("回调函数执行");
		alert("ResponseText:"+request.responseText);
		//var exi=$("existmsg");
		//var noexi=$("noexistmsg");
		//var sub=$("sub");
		var req=request.responseText.evalJSON();
	
		//alert("reqText:"+req);
		if(req.exist=="yes"){//数据库存在该年信息
			$("existmsg").innerHTML="数据库已存在该年信息,无需重复插入";
			$("sub").style.display="none";
		} else if(req.exist=="no"){//数据库不存在该年信息
			if($("selectyear").value!=new Date().getYear()){
				$("existmsg").innerHTML="友情提示:您正在插入的不是本年工作日的数据,该年的周一到周五将被设置为工作日";				
			}else $("existmsg").innerHTML="友情提示:您正在插入本年的工作日数据,该年的周一到周五将被设置为工作日";
			$("sub").style.display="block";
		}else {//查询异常
			$("existmsg").style.display="none";
			$("noexistmsg").style.display="none";
			$("sub").style.display="none";
			$("error").innerHTML="Sorry,程序异常,异常代码"+req.errorcode+".请联系网管";
		}
	}
</script>

</head><body onload="bodyInit(insertYearForm)"><div id="error" style="font-size:x-large;font-weight:bold;color:red;"></div><form action="/AOS/datema/InsertOneYearAction" method="post" id="insertYearForm"><div>年份:<select id="selectyear" name="year" onchange="CheckOneYear()"></select><input type="hidden" name="insertcheck" value="yes"></div><div id="existmsg" style="font-weight:bold;color:red;"></div><br><br> <input name="sub" id="sub" type="submit" value="现在插入"onclick="return insertYearValidate(insertYearForm)"></form><a href="">改天再说(<span style="color:red;">注意:这样您会登出系统</span>)</a><%-- 跳到登出系统的那个action,在李咏伟那里 --%></body></html>
后台的Action
class="java" name="code">package com.aos.workdate.control.action;


import org.apache.struts2.json.annotations.JSON;

import com.aos.workdate.control.dto.WorkDateDto;
import com.aos.workdate.control.util.ErrorMsgUtil;
import com.aos.workdate.control.util.ToolUtil;
import com.aos.workdate.model.service.interfaces.ICheckYearService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


/**
 * @author ***
 * @Date 2012-10-12下午3:13:27
 * @TODO Check the DB if there are days of this year. 
 */
public class CheckYearAction extends ActionSupport implements ModelDriven {
	
	private String year;
	private WorkDateDto cydto;
	private ICheckYearService cyservice;
	private int errorcode=ErrorMsgUtil.checkyearerror;//一个action可以仅用一个错误码来定位
	private String exist;
	private String insertcheck;


	public String getInsertcheck() {
		return insertcheck;
	}

	public void setInsertcheck(String insertcheck) {
		this.insertcheck = insertcheck;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}
	public String getExist() {
		return exist;
	}

	public void setExist(String exist) {
		this.exist = exist;
	}

	public void setErrorcode(int errorcode) {
		this.errorcode = errorcode;
	}

	public int getErrorcode() {
		return errorcode;
	}

	public void setCyservice(ICheckYearService cyservice) {
		this.cyservice = cyservice;
	}

	public void setCydto(WorkDateDto cydto) {
		this.cydto = cydto;
	}
	
	@JSON(serialize=false)
	public WorkDateDto getCydto() {
		return cydto;
	}

	public String execute(){
		if(null==year||"".equals(year)){
			year=new ToolUtil().year+"";
		}
		cydto.setYear(year);
		cydto=cyservice.CheckYear(cydto);
		
		/*如果存在返回success,如果不存在返回no*/
		if(cydto.isIserror()){//查询错误,跳到错误页面
			System.out.println("error:"+cydto.isIserror());
			if("yes".equals(insertcheck)){//由于这个action也给另一个非json的action使用,故这里判断下是否是json的请求,下同
				this.exist="error";
				return SUCCESS;
			}
			return "error";
		}
		if("no".equals(cydto.getResult())){//
			System.out.println("result:"+cydto.getResult());
			if("yes".equals(insertcheck)){
				this.exist="no";
				return SUCCESS;
			}
			return "no"; 
		}
		if("yes".equals(cydto.getResult())){
			System.out.println("result:"+cydto.getResult());
			this.exist="yes";
			return SUCCESS;
		}
		return "error";
		
	}
	public Object getModel() {
		return null;
	}

}


原文链接:https://www.f2er.com/json/290595.html

猜你在找的Json相关文章