首页 > 代码库 > ssi2+ajax实现唯一性验证+showModalDialog刷新父页面

ssi2+ajax实现唯一性验证+showModalDialog刷新父页面

最近研究了strust2和ajax结合实现方法

先看一下页面①

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@	taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.01 Transitional//EN">
<html>
<head>

<%@ include file="noCache.jsp"%>
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/css/content.css">
<script type="text/javascript" src="http://www.mamicode.com/js/jquery-1.3.2.js"></script>
<style type="text/css">
.tipclass{
width: 150px;
height:30px;
}
</style>
<script type="text/javascript">
	
	function xiugaitextidone(num) {
		if(confirm("确认修改吗?")){
			if("报告号可用"==$("#tip").html()){
				document.pageForm.submit(); 
				setTimeout("doClose()","500");
			}else{
				alert("报告号已存在");
			}
		}
		
	}
	function doClose(){
		window.returnValue = 1;
		window.close();
	}
	function funcheck(){
		$.ajax({
		type : ‘post‘,
		url : ‘FunctionsAction_checkTextid‘,
		dataType:‘json‘,
		data : {
			"text_id":$("#textid").val(),
			"sample_number":$("#samplenumber").val()
		},
		success : function(data) {
			if("报告号已存在"==data.str){
				$("#tip").html("<span ><img src=http://www.mamicode.com/‘"+$("#path").val()+"/images/onError.gif‘ />"+data.str+"</span>");
			}else{
				$("#tip").html(data.str);
			}
		},
		error:function(){
			alert("错误");
		}
	});
	}
	
</script>
</head>
<base target="iframe1"/>
<body>
<input value="http://www.mamicode.com/${pageContext.request.contextPath}" id="path" type="hidden">
<iframe name="iframe1" style="display:none"></iframe>
	<div id="biaoti" class="tbtitle01">
		<b>修改报告号</b>
	</div>
	<div class="space_h_10"></div>
	<s:form action="FunctionsAction_updatetextid"
		name="pageForm" method="post">

		<table width="100%" border="0" cellpadding="6" cellspacing="0"
			class="tb_list1">
			<tr>
				<th>样品号</th><td><s:property value="http://www.mamicode.com/s.sample_number" /><input type="hidden" name="s.sample_number" id="samplenumber" value="http://www.mamicode.com/${s.sample_number }"/></td>
			</tr>
			<tr><th>报告号</th><td><input name="s.text_id" id="textid" value="http://www.mamicode.com/${ s.text_id}" onblur="funcheck()" /><div id="tip" class="tipclass"></div></td></tr>
			<tr><th>样品名称</th><td><s:property value="http://www.mamicode.com/s.x_prodnm" /></td></tr>
			<tr><th>委托单位</th><td><s:property value="http://www.mamicode.com/s.x_orgname" /></td></tr>
			<tr>
				<td colspan="4">
					<input type="button" value="http://www.mamicode.com/修改" onclick="xiugaitextidone(‘<s:property value="http://www.mamicode.com/s.sample_number" />‘)" />
<!-- 					<input type="button" value="http://www.mamicode.com/关闭" onclick="doClose()"/> -->
				</td>
			</tr>
		</table>
	</s:form>
</body>
<c:if test="${msg ne null }">
		<script>
			alert(‘${msg}‘);
		</script>
	</c:if>
</html>

再看一下页面②

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@	taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.01 Transitional//EN">
<html>
<head>

<%@ include file="noCache.jsp"%>
<link rel="stylesheet" type="text/css" href="http://www.mamicode.com/css/content.css">
<script type="text/javascript" src="http://www.mamicode.com/js/jquery-1.3.2.js"></script>
<script type="text/javascript">
function xiugaitextid(num){
	var returnValue = window.showModalDialog("FunctionsAction_selectTextidOne?num="+num+"&date="+new Date(),
			"detail","dialogHeight=450px;dialogWidth=600px;resizable=yes");
	if(returnValuehttp://www.mamicode.com/=="1"){
// 		window.location.reload();
		var sample_number=$("#sample_number").val();
		var text_id=$("#text_id").val();
		var page = pageForm.page.value;
		var pageSize = pageForm.pageSize.value;
		var url = "FunctionsAction_selectSampleBySamplenumberOrTextid?"+
				"sample_number="+sample_number+"&text_id="+text_id+"&page="+page+"&pageSize="+pageSize+"&fromPage=true";
		window.location.href=http://www.mamicode.com/url;"value",val.toUpperCase());
}
$(document).ready(function(){
	
		$("#sample_number").focus();
	
})

</script>
</head>
<body>
	<div id="biaoti" class="tbtitle01">
		<b>修改报告号</b>
	</div>
	<div class="space_h_10"></div>
	<s:form action="FunctionsAction_selectSampleBySamplenumberOrTextid" name="pageForm" method="post">
		样品号:<s:textfield name="s.sample_number" id="sample_number"></s:textfield>
		报告号:<s:textfield name="s.text_id" id="text_id" onkeyup="toUpperCase(this)"></s:textfield>
		<s:submit value="http://www.mamicode.com/查询"></s:submit>
	<table width="100%" border="0" cellpadding="6" cellspacing="0"
		class="tb_list1">
		<tr>	
			<th>序号</th>
			<th>样品号</th>
			<th>报告号</th>
			<th>样品名称</th>
			<th>委托单位</th>
			<th>操作</th>
		</tr>
		<s:iterator value="http://www.mamicode.com/samplelist" status="i">
			<tr>
			<td><s:property value="http://www.mamicode.com/#i.index+1" /></td>
				<td><s:property value="http://www.mamicode.com/sample_number" /></td>
				<td><s:property value="http://www.mamicode.com/text_id" /></td>
				<td><s:property value="http://www.mamicode.com/x_prodnm" /></td>
				<td><s:property value="http://www.mamicode.com/x_orgname" /></td>
				<td><input type="button"  value="http://www.mamicode.com/修改" onclick="xiugaitextid(‘<s:property value="http://www.mamicode.com/sample_number" />‘)"/> </td>
			</tr>
	</s:iterator>
	</table>
	${page }
	</s:form>
	
</body>

</html>

最后看一下后台

public String checkTextid() {
		response.setContentType("text/html;charset=UTF-8");
		String text_id = request.getParameter("text_id");
		String sample_number = request.getParameter("sample_number");
		Sample sampleall = this.sampleService.getSampleOne(
				Integer.parseInt(sample_number), Department.ALL);
		int i = 0;
		i = this.sampleService.selectByText_id(text_id,
				Department.getDepartmentFromSample(sampleall));
		String data;
		if (i > 0) {
			data = "{‘str‘:‘报告号已存在‘}";
			
		} else {
			data = "{‘str‘:‘报告号可用‘}";
		}
		try {
			PrintWriter out = response.getWriter();
			out.print(JSONObject.fromObject(data));
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;

	}

说明:后台使用的是json方式,jar包在后面附件里

ssi2+ajax实现唯一性验证+showModalDialog刷新父页面