java ajax struts_Struts2 处理AJAX请求

struts2整合ajax有2种方式:

使用type="stream"类型的

使用json插件

使用type="stream"类型的  获取text

前端

学号:

姓名:

查询成绩

$("#btn").click(function () {

$.ajax({

url:"handleraction",

type:"get",

data:{"no":$("#no").val(),"name":$("#name").val()},

datatype:"text",

error:function () {

console.log("ajax请求失败!")

},

success:function (data) {

$("#score").text(data);

}

})

});

url要和struts.xml中action的name、包的namespace对应。

action

public class handleraction extends actionsupport {

private int no;

private string name;

private inputstream inputstream;

public int getno() {

return no;

}

public void setno(int no) {

this.no = no;

}

public string getname() {

return name;

}

public void setname(string name) {

this.name = name;

}

public inputstream getinputstream() {

return inputstream;

}

public void setinputstream(inputstream inputstream) {

this.inputstream = inputstream;

}

@override

public string execute() throws exception {

//此处缺省连接数据库查询总分

string result = name + "同学,你的总分是:680";

//设置要返回的数据。我们传给浏览器的数据含有中文,需要设置utf-8编码,来解决中文乱码

inputstream=new bytearrayinputstream(result.getbytes("utf-8"));

return success;

}

}

前端向后台发送了2个字段:no、name

action需要设置2个同名的成员变量,并提供对应的getter、setter方法,才能接收到前端传来的数据。

需要一个inputstream类型的成员变量,并提供对应的getter、setter,用于向浏览器返回数据。

需要一个处理请求的方法(execute),设置返回给浏览器的数据。

struts.xml

text/html

inputstream

流程分析

前端向后台发送ajax请求,传递no、name2个字段

jvm创建action实例,调用no、name对应的setter方法把前端传过来的值赋给成员变量(会自动转换为目标类型),完成action的初始化

jvm调用action处理业务的方法execute,设置向浏览器返回的数据

jvm根据struts.xml中指定的方法(getinputstream),获取inputsteam,将里面的数据传给浏览器。

使用type="stream"类型的  获取json

前端

学号:

查询学生信息

$("#btn").click(function () {

$.ajax({

url:"handleraction",

type:"post",

data:{"no":$("#no").val()},

datatype:"json",

error:function () {

console.log("ajax请求失败!")

},

success:function (data) {

$("#show").append("姓名:" + data.name+",");

$("#show").append("年龄:" + data.age+",");

$("#show").append("成绩:" + data.score+"。");

}

})

});

action

public class handleraction extends actionsupport {

private int no;

private inputstream inputstream;

public int getno() {

return no;

}

public void setno(int no) {

this.no = no;

}

public inputstream getinputstream() {

return inputstream;

}

public void setinputstream(inputstream inputstream) {

this.inputstream = inputstream;

}

@override

public string execute() throws exception {

//此处缺省连接数据库查询得到学生信息

student student = new student(1, "张三", 20, 100);

string jsonstr = json.tojsonstring(student);

//设置要返回的数据

inputstream=new bytearrayinputstream(jsonstr.getbytes("utf-8"));

return success;

}

}

使用了阿里的fastjson.jar,需要自己下载引入。

struts.xml

配置同上

使用json插件实现ajax

前端

学号:

查询学生信息

$("#btn").click(function () {

$.ajax({

url:"handleraction",

type:"post",

data:{"no":$("#no").val()},

datatype:"json",

error:function () {

console.log("ajax请求失败!")

},

success:function (data) {

$("#show").append("姓名:" + data.student.name+",");

$("#show").append("年龄:" + data.student.age+",");

$("#show").append("成绩:" + data.student.score+"。");

}

})

});

action

public class handleraction extends actionsupport {

private int no;

private student student;

public int getno() {

return no;

}

public void setno(int no) {

this.no = no;

}

public student getstudent() {

return student;

}

public void setstudent(student student) {

this.student = student;

}

@override

public string execute() throws exception {

//此处缺省连接数据库查询得到学生信息

student = new student(1, "张三", 20, 100);

return success;

}

}

需要设置同名的成员变量,并提供getter、setter方法,来接收前端传来的数据。

此种方式是由json插件把action对象序列化为一个json格式的字符串,传给浏览器。浏览器可以直接访问action的所有成员变量(实质是调用对应的getter方法)。

我们只需要把ajax要请求的数据封装为action的成员变量,并提供对应的getter、setter方法。需要在主调方法(execute)的return语句之前对请求的数据赋值。

success:function (data) {

$("#show").append("姓名:" + data.student.name+",");

$("#show").append("年龄:" + data.student.age+",");

$("#show").append("成绩:" + data.student.score+"。");

}

浏览器接受到的数据data本身就是action实例,可通过.访问成员变量。

struts.xml

true

text/html

说明

需要手动添加json插件 struts2-json-plugin.jar 。

f2a43712ce549da92ca1825370b48694.png

上面的压缩包含有struts的所有jar包,其中就包括了struts2-json-plugin.jar。

下面的压缩包只有struts核心的8个jar包。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值