我用jQuery调用java业务类的方法这样有什么问题?
我直接上代码了,我刚学Jquery。
要求是,当我点击页面的下拉列表选择不同的分数的时候,通过jQuery跳到java类中,再由这个类去访问数据库,得到一个集合,要将这个集合遍历以表格的形势显示在页面中,
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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>信息显示</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">
-->
<style type="text/css">
<!--
.STYLE2 {font-size: 24px}
-->
</style>
<script type="text/javascript" src="jquery-1.2.3-intellisense.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#sel").change(function() {
var score =this.value;
alert(this.value);
$.ajax({
type: "POST",
url: "/biz/impl/InfoBizImpl.java/getBySql2",// 我要调用InfoBizzImpl类中的getBySql2这个方法
data:score, //这里是我要传入方法的参数
contentType: "application/json; charset=utf-8",
success: function(msg) {
$("#div1").html(msg);
},
error: function(xhr,msg,e) {
alert(msg);
}
});
});
});
</script>
</head>
<body>
<br>
<table width="400" border="0" align="center">
<tr>
<td class="STYLE2">请选择分数的查询条件</td>
</tr>
<tr>
<td><form name="form1" method="post" action="">
<label>
<select name="select1" id="sel">
<option value="0" selected>全部</option>
<option value="90">大于90分</option>
<option value="80">大于80分</option>
<option value="70">大于70分</option>
<option value="60">大于60</option>
<option value="50">不及格</option>
</select>
</label>
</form>
</td>
</tr>
<tr>
<td><table width="400" border="0">
<tr>
<td>编号</td>
<td>姓名</td>
<td>科目</td>
<td>成绩</td>
</tr>
<tr>
<hr/>
</tr>
<div id="div1">
<c:forEach var="info" items="${item}">
<tr>
<td>${info.id }</td>
<td>${info.name }</td>
<td>${info.subject }</td>
<td>${info.score }</td>
</tr>
</c:forEach>
</div>
</table></td>
</tr>
</table>
</body>
</html>
再就是java类,InfoBizImpl.java,路径是:src/biz/impl/InfoBizImpl.java
public class InfoBizImpl implements InfoBiz
{
public String getBySql2(int score) {
String sql="from Info";
if(score!=0)
{
if(score==50){
sql+=" where score<60";
}else if(score ==60)
{
sql+=" where score>60 and score <70";
}else if(score ==70)
{
sql+=" where score>70 and score <80";
}else if(score ==80)
{
sql+=" where score>80 and score <90";
}else if(score ==90)
{
sql+=" where score>90 and score <=100";
}
}
List<Info> list=dao.findByHql(sql);
String str="";
if(list.size()!=0){
for(Info in :list)
{
str+=" <tr><td>"+in.getId()+"</td><td>"+in.getName()+"</td><td>"+in.getSubject()+"</td><td>"+in.getScore()+"</td></tr>";
}
}
return str;
}
info是个实体类我就不贴出来了。
我从页面进不到InfoBizImpl 这个类中,高手帮下忙,小弟初学者。。
[解决办法]
url: "/biz/impl/InfoBizImpl.java/getBySql2",// 我要调用InfoBizzImpl类中的getBySql2这个方法
在jquery1.3的帮助文档中,对url这个参数的说明是:
url String
(默认: 当前页地址) 发送请求的地址。
所以我估计是url参数写错了,
你可以这样测试一下,你将你的url参数输入到ie的地址栏中,看能否访问到数据,如果返回结果错误,就肯定不行了。