http://wenku.baidu.com/view/2ddf3843be1e650e52ea99e5.html
json调用jsp页面,jsp页面中加载公司标签的实现过程(json 异步)
1、jsp中写的json:
var isAskUrl = "<c:url value='/ask/ajax/isAskJson.shtm' />"
$.getJSON(isAskUrl,{userID:hiddenUserId,faqPrice:price,ranNum:Math.random()},function(data){
if(data != 1 ){
alert("金额不足,不能提问!");
}else{
}
});
2、要访问的isAskJson.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
<%@ taglib tagdir="/WEB-INF/tags/fee" prefix="fee" %>
<%@ include file="/WEB-INF/jsp/login/loginCheck.jsp" %>
<fee:isAsk var="result" userID="${param.userID}" faqPrice="${param.faqPrice}"></fee:isAsk>
<c:if test="${not empty result and result eq 1}">
<util:writeJson value="[1]"></util:writeJson>
</c:if>
<c:if test="${empty result or result ne 1}">
<util:writeJson value="[0]"></util:writeJson>
</c:if>
3、isAsk.tag 的代码
<%@ attribute
name="userID"
type="java.lang.Integer"
required="true"
%>
<%@ attribute
name="faqPrice"
type="java.lang.Float"
required="true"
%>
<%@
attribute
name="var"
type="java.lang.String"
required="true"
%>
<%@ tag
import="com.huabeisai.rad3.ibatis2.SqlMapTemplate,com.huabeisai.fee.domain.UserAccount,
javax.servlet.jsp.PageContext,java.util.*"
dynamic-attributes="dynamicAttributes"
description="是否可以提问" %>
<%
Integer userID = (Integer)jspContext.getAttribute("userID");
Float faqPrice = (Float)jspContext.getAttribute("faqPrice");
int rtInt = 0;
if(userID == null){
return;
}
if(faqPrice == null || faqPrice < 0){
return;
}
Map paramMap = new HashMap();
paramMap.put("userID",userID);
paramMap.put("accountID",1);
UserAccount userAccount = (UserAccount)SqlMapTemplate.selectObject("fee","ns.op.feeApi.getUserAccount",paramMap);
if(userAccount != null && userAccount.getUsableMoney() != null && userAccount.getUsableMoney() > 0){
Float usableMoney = userAccount.getUsableMoney();
if(usableMoney >= faqPrice){
rtInt = 1;
}else {
rtInt = 0;
}
}else{
rtInt = 0;
}
jspContext.setAttribute(var,rtInt,PageContext.REQUEST_SCOPE);
%>
第二版不用数据库的异步交互过程:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<script>
function ckc(vl){
var isAskUrl = "<c:url value='/ask/ajax/isJson.shtm' />"
var numh=vl.value;
$.getJSON(isAskUrl,{numh:numh,ranNum:Math.random()},function(data){
document.getElementById("inputid").value=data;
});
}
</script>
<body>
<input type="text" value="" onpropertychange="ckc(this);"/><br></br>
<input type="text" value="" id="inputid"/>
</body>
</html>
2、要访问的isJson.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common/common.jsp" %>
<%@ taglib tagdir="/WEB-INF/tags/fee" prefix="fee" %>
<%
int paramvalue=Integer.parseInt(request.getParameter("numh"));
%>
<util:writeJson value="[111]"></util:writeJson>