jsp中ajax代码:
1 ???????????$.ajax({ 2 ??????????????var id = $("#studentid").val();//获取标签中的学生id 3 ??????????????url:‘${pageContext.request.contextPath}/student/stu_delStudent.action?studentid=‘+id, 4 ??????????????data:‘‘, 5 ??????????????type:‘POST‘, 6 ??????????????dataType:‘json‘, 7 ??????????????async:false, 8 ??????????????success:function(data){ 9 ??????????????????alert(data.message);10 ??????????????}11 ??????????????12 ??????????});
action中的代码:
1 public class StudentAction extends ActionSupport{ 2 ????private Student student; 3 ????public Student getStudent() { 4 ????????return student; 5 ????} 6 ????public void setStudent(Student student) { 7 ????????this.student = student; 8 ????} 9 10 ????@Resource11 ????private StudentService studentService;12 ????13 ????public String delStudent() throws Exception{14 ????????//接收请求数据15 ????????int studentid = ServletActionContext.getRequest().getParameter("studentid");16 ????????studentSerivce.delByStudentId(studentid);17 ????????//创建一个JSON对象18 ????????JSONObject json = new JSONObject();19 ????????json.put(“message",删除成功");//将返回信息保存在JSON对象中20 ????????HttpServletResponse response = ServletActionContext.getResponse();21 ????????//设置响应编码格式,防止乱码22 ????????response.setContentType("text/html;charset=UTF-8");23 ????????//将数据以json格式响应给ajax24 ????????response.getWriter().write(json.toString());25 ????????26 ????????return null;27 ????}28 }
通过ajax从jsp页面传输数据到web层,并从web层返回数据给jsp页面
原文地址:http://www.cnblogs.com/lixianyuan-org/p/7492640.html