js代码:
首先引用 jquery1.4.2.js
<script type="text/javascript">
function showAjax(obj){
var id=$(obj).next().text();
$.ajax({
type : "POST",
url : "${basePath}StudentController/showAjax.do",
async : false,
data: {cmid:id},
success : function(data) {
if (data=='0') {
showMsg('学生已存在!');
}else{
showMsg('没有此学生!');
}
},
dataType : "json"
});
}
<script>
from 表单:
<p onclick="showAjax(this);">删除</P>
<p id="id">123</p>
Controller 层:
public class StudentController extends JdbcDaoSupport{
@RequestMapping("/StudentController/deleteAjax")
public void deletecm(HttpServletResponse response,String cmid)throws IOException{
int an=0;
try{
//执行sql语句
JdbcTemplate jdbc=getJdbcTemplate();
String sql="select name from student where cmid=2";
an=jdbc.update(sql);
if(an == 0 ){
response.getWriter().print("1");//没有相同的学生
}else{
response.getWriter().print("0");//学生姓名重复
}
}catch(Exception e){
response.getWriter().print("1");//没有相同学生
}finally{
response.getWriter().close();
}
}
}