Java 前台后台数据传递、中文乱码解决方法
1.向前台传递数据;
2.向后台传递数据;
3.ajax post 提交数据到服务端时中文乱码解决方法;
4.数组类型参数传递;
1.向前台传递数据:
1.1 字符串数据传递:
这种方式只是单一的向前台传递字符串(比如传递ajax 请求某个数据的结果),通过 response 实现;
1.1.1 Action 类:
public String getResult(){
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html;charset=GBK");//解决中文乱码
PrintStream out=null; //流
try {
out = new PrintStream(response.getOutputStream());
out.print("向前台传递一个字符串结果");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
out.flush();
out.close();
}
return null; //最后返回null
}
1.1.2 struts 配置文件:
<action name="testAction" class="testAction" method="getResult">
<result name="success"></result> <!-- 这里不用填写什么 -->
</action>
1.2 对象数据传输:
对象数据通常是已json 格式传输,在 struts2 配置文件内引入 json-default(普通json 格式) 包或者 jackson-json-default(加强型json格式,在返回的json数据中包含对象类型,类似这样的结果("_javaType_":"com.action.TestAction");可以根据业务情况选用,随着业务系统的庞大,我一般用javascript 在前台绑定数据,这样当涉及到判断数据类型时就可以采用这个字段的值来处理:
1.2.1 Action 类:
List<Student> lsStudent; //lsStudent 属性要有get\set方法
/**
* 根据班级ID 获得该班级的学生信息
* return 班级所有学生信息
*/
public String getStudentByClassId(){ HttpRequest request=ServletActionContext.getRequest();
String classId=request.getParameter("classId"); //班级ID /*
*这里的判断很容易出错,如果你用(id!=null)做判断条件,当没有获得值时,id 是一个空的String对象,空对象不能做判断,这就好像你在Java类中这样写代码: String tb;
if(tb==null||tb==""){// 错误:The local variable tb may not have been initialized
System.out.println(tb);
}
*/
if(null!=id ||!"".equals(id)){
lsStudent=TestStudent.getStudentById(id);
} return SUCCESS;
}
1.2.1 struts 配置文件:
<action name="testAction" class="testAction" method="getResult">
<result name="success" type="strongtype-json">
<param name="root">
lsStudent <!-- 这里返回action 中要返回的对象(一定要有get/set方法) -->
</param>
</result>
</action>
最终前台会得到这样的json数据:
[{"__javaType__":"com.base.Student","name":"张三","age":"10","homeAddr":null,"stuNum":0,"classNum":0},{"__javaType__":"com.base.Student","name":"李四","age":"20","homeAddr":null,"stuNum":0,"classNum":0}]
1.2.3 前台js获得json数据:
$.ajax({
type:"post",
url:"testAction.action",
data:{
classId:classId
},
success:function(rs){
var studentArray=[]; //前台创建个数组对象保存数据
$.each(rs,function(i,item){
var student;
student.name=item.name;
student.id=item.id;
student.age=item.age;
studentArray.push(student); //将student 对象信息保存到数组对象中
});
},
error:function(){
alert("获取数据时发生错误");
}
});
3.ajax post 提交数据到服务端时中文乱码解决方法:
get 方式提交数据到服务端不会乱码,但对数据量有限制;post 可以提交大数据量,但中文会发生乱码,解决方法:
在JS上用使用 encodeURIComponent 对字符编码处理:
studentRuselt=encodeURIComponent(JSON.stringify(result),"utf-8"); //这里用了json2 来将对象转换为json格式,然后在用encodeURIComponent来设置编码;
$.ajax({
type:"post",
url:"saveExamQuestionAnswer.action",
cache:true,
async:true, //这里指定值时不能加双引号(会设置无效)
contentType: "application/x-www-form-urlencoded; charset=utf-8",
data: {
studentRuselt: studentRuselt
}
)};
Action类上用java.net.URLDecoder.URLDecoder.decode方法转码:
studentRuselt=URLDecoder.decode(studentRuselt,"UTF-8");
这样得到的中文不会乱码,还有另外一个js组件:encodeURI也可以对字符进行处理,提交时它会使用jquery默认编码提交数据,但使用encodeURIComponent 组件指定编码,细节清晰,前台后台处理编码一致这样比较稳妥;
4.数组类型参数传递:
若一个请求中包含多个值,如:(test.action?tid=1&tid=2&tid=3),参数都是同一个,只是指定多个值,这样请求时后台会发生解析错误,应先使用 tradititonal 格式化:
$.ajax({
type:"post",
url:"test.action",
data:{
tid:[1,2,3]
},
traditional:true
});
Java 前台后台数据传递、中文乱码解决方法的更多相关文章
- Django 分页查询并返回jsons数据,中文乱码解决方法
Django 分页查询并返回jsons数据,中文乱码解决方法 一.引子 Django 分页查询并返回 json ,需要将返回的 queryset 序列化, demo 如下: # coding=UTF- ...
- url中向后台传递中文乱码解决方法
方法一: 1.jsp中代码 var userNo = $('#prisoner_id').val(); userNo = encodeURI(userNo); allPrisone ...
- [转]mysql导入导出数据中文乱码解决方法小结
本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...
- ajax()函数传值中文乱码解决方法介绍
jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 复制代码 代码如下: $.ajax({ dataType : ‘json',type : ‘POST',url : ‘ht ...
- php mysql 中文乱码解决方法
本文章向码农们介绍php mysql 中文乱码解决方法,对码农们非常实用,需要的码农可以参考一下. 从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行 解 ...
- jquery的ajax()函数传值中文乱码解决方法介绍
jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...
- Codeblocks中文乱码解决方法。
如需安装包请后台留言!! Codeblocks中文乱码解决方法: 特别提示:出现中文乱码情况才执行以下操作,未出现请勿随意修改!!!! 打开Codeblocks -> 设置 -> 编辑器: ...
- 记一次Maven发布Jar包中文乱码解决方法
Maven deploy 乱码 今天使用Maven发布Jar包时,发布功能都是正常的也成功上传到了仓库,就是项目跑越来后出中文中现了乱码: { "code": "SUCC ...
- Zxing中文乱码解决方法
Zxing中文乱码解决方法总结 尝试过非常多方法 最后发现此方法解决的乱码最多....... 在百度搜索二维码图片 经过前2页的測试 除开一张图之外 其余都能扫描出结果 假设大家有更好的解决方法 ...
随机推荐
- elasticSearch Java Spring Data Api
1. BoolQueryBuilder qb=QueryBuilders. boolQuery(); qb.should(QueryBuilders.matchQuery("keyWord& ...
- (转)Inno Setup入门(二十)——Inno Setup类参考(6)
本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17251041 存储框 存储框也是典型的窗口可视化组件,同编辑框类似, ...
- 【转】Jmeter测试报表相关参数说明
Jmeter测试报表相关参数说明 采用Jmeter测试工具对web系统作的负载测试,得出的响应报表,数据比较难懂,现作一具体说明. 以下是在一次具体负载测试中得出的具体数值,测试线程设置情况为:线程数 ...
- 对于Oracle中Number类型的字段映射成Java中的具体类型的问题
我在Oracle中给一个用户Id字段设置为Number类型,使用JDBC在完成ORM的时候,以为其可以自动转换为Integer,因为我的POJO类id使用的就是Integer.但事实是,我在测试的时候 ...
- Android六大进程间通信方式总结之一:基本知识
因为不同进程都是享有独立资源的,所以全局变量这些都是无效的,必须有其他的进程间通信方式. 一.基本知识 1:怎样使用多进程 Android正常使用的多进程的办法只有一种,就是在Service或Acti ...
- interrupt 1 using 1
释疑:void Timer0() interrupt 1 using 1 Timer0 是函数名,随便取的 interrupt xx using y 跟在interrupt 后面的 ...
- jquery UI 的 datapicker 中文汉化问题
这个问题自己上网上百度的了很多的方法都没有 具体一点的东西,好在自己没有放弃 从昨天到现在,自己 摸索了很久终于找到了汉化的方法了,希望可以帮助到像我这样刚入web门的小白. 废话不说了直接来干货吧! ...
- 【原】Coursera—Andrew Ng机器学习—Week 11 习题—Photo OCR
[1]机器学习管道 [2]滑动窗口 Answer:C ((200-20)/4)2 = 2025 [3]人工数据 [4]标记数据 Answer:B (10000-1000)*10 /(8*60*60) ...
- C# 发送和接受Get请求
1.发送Get请求 public static string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (H ...
- C# 文本文件的读写
// *********************************************************************** // Assembly : XXX // Auth ...