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 前台后台数据传递、中文乱码解决方法的更多相关文章

  1. Django 分页查询并返回jsons数据,中文乱码解决方法

    Django 分页查询并返回jsons数据,中文乱码解决方法 一.引子 Django 分页查询并返回 json ,需要将返回的 queryset 序列化, demo 如下: # coding=UTF- ...

  2. url中向后台传递中文乱码解决方法

    方法一: 1.jsp中代码 var userNo = $('#prisoner_id').val();      userNo = encodeURI(userNo);      allPrisone ...

  3. [转]mysql导入导出数据中文乱码解决方法小结

    本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...

  4. ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 复制代码 代码如下: $.ajax({ dataType : ‘json',type : ‘POST',url : ‘ht ...

  5. php mysql 中文乱码解决方法

    本文章向码农们介绍php mysql 中文乱码解决方法,对码农们非常实用,需要的码农可以参考一下. 从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行 解 ...

  6. jquery的ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 代码如下: $.ajax({ dataType : ‘json', type : ‘POST', url : ‘http: ...

  7. Codeblocks中文乱码解决方法。

    如需安装包请后台留言!! Codeblocks中文乱码解决方法: 特别提示:出现中文乱码情况才执行以下操作,未出现请勿随意修改!!!! 打开Codeblocks -> 设置 -> 编辑器: ...

  8. 记一次Maven发布Jar包中文乱码解决方法

    Maven deploy 乱码 今天使用Maven发布Jar包时,发布功能都是正常的也成功上传到了仓库,就是项目跑越来后出中文中现了乱码: { "code": "SUCC ...

  9. Zxing中文乱码解决方法

    Zxing中文乱码解决方法总结 尝试过非常多方法  最后发现此方法解决的乱码最多....... 在百度搜索二维码图片 经过前2页的測试  除开一张图之外  其余都能扫描出结果 假设大家有更好的解决方法 ...

随机推荐

  1. java之IO整理(上)

    /*//创建一个新文件 public static void main(String[] args) { File file=new File("D:\\hello.txt"); ...

  2. 在spring中该如何使用DTO,以及DTO和Entity的关系

    1. DTO是用于将后台的数据结构(javaBean)转换为对用户友好的表现方式的数据结构,同时也能防止后台数据直接传送到前台而存在的潜在危险. 2. 可以时候要哪个springbot框架提供的转换器 ...

  3. svg_png

    #!/usr/bin/env python#-*- encoding=UTF-8 -*-from __future__ import print_functionimport sysimport ra ...

  4. leetcode840

    本题不清楚题意,从网上找到了python的解答,记录如下. class Solution: def numMagicSquaresInside(self, grid): ans, lrc = 0, [ ...

  5. leetcode747

    public class Solution { public int DominantIndex(int[] nums) { var list = new List<KeyValuePair&l ...

  6. Spring+Log4j的集成总结

    导入依赖的jar包 <log4j.version>1.2.16</log4j.version> <!-- 自动引入slf4j-api.jar,log4j.jar,以及sl ...

  7. Sersync + Rsync 代码分发

    简介: Sersync 是基于 inotify 来编写的 Linux 系统文件监控工具,当监控到文件发生变化时,调用 rsync 同步文件. 类似的功能,以前有用 rsync + inotify 实现 ...

  8. kittle 使用心得

    1,字体编码格式: 解析excel表格时,出现乱码,两处修改:1, 2,

  9. Elasticsearch学习系列之term和match查询实例

    Elasticsearch查询模式 一种是像传递URL参数一样去传递查询语句,被称为简单查询 GET /library/books/_search //查询index为library,type为boo ...

  10. iOS布局之Auto Layout

    学习资源: <iOS6核心编程>自动布局部分 <iOS6范例经典>自动布局部分 Tutorial: iOS 6 Auto Layout versus Springs and S ...