分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

通过ajax从jsp页面传输数据到web层,并从web层返回数据给jsp页面

发布时间:2023-09-06 01:09责任编辑:郭大石关键词:jsjsp
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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved