package继承json-default与struts-default

  返回结果是add的话将addResult属性转换为json返回(addResult属性有getter,setter方法),返回结果是查询是正常的页面跳转。

  如果配置中不写param,默认会将所要带get,set的属性转为JSON。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5.  
  6. <struts>
  7.  
  8. <!-- <constant name="devMode" value="true"></constant> -->
  9. <constant name="struts.enable.DynamicMethodInvocation" value="true" />
  10. <constant name="struts.action.extension" value="action" />
  11. <constant name="struts.objectFactory" value="spring"></constant>
  12.  
  13. <package name="liuyan" namespace="/" extends="struts-default,json-default">
  14. <action name="liuyan_*" class="liuyanAction" method="{1}">
  15. <result name="add" type="json">
  16. <param name="root">addResult</param>
  17. </result>
  18. <result name="chaxun">/liuyan.jsp</result>
  19. </action>
  20. </package>
  21.  
  22. </struts>

2.Action代码(处理增加与查询请求)

  1. package cn.qlq.action;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. import javax.annotation.Resource;
  9.  
  10. import org.springframework.context.annotation.Scope;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.ui.Model;
  13. import org.springframework.web.bind.annotation.RequestBody;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.ResponseBody;
  16.  
  17. import com.opensymphony.xwork2.ActionSupport;
  18.  
  19. import cn.qlq.bean.Liuyan;
  20. import cn.qlq.service.LiuyanService;
  21. import cn.qlq.service.impl.LiuyanServiceImpl;
  22. import cn.qlq.utils.PageBean;
  23.  
  24. @Controller
  25. @Scope("prototype")
  26. @SuppressWarnings("all")
  27. public class LiuyanAction extends ActionSupport {
  28.  
  29. @Resource
  30. private LiuyanService liuyanService;
  31. private String currentPage;
  32. private Map result;
  33. private String addResult;
  34. private Liuyan liuyan;
  35.  
  36. // 增加,用ajax+json交互
  37. public String addLiuyan() throws SQLException {
  38. liuyan.setRiqi(new Date());
  39. boolean result = liuyanService.addLiuyan(liuyan);
  40. addResult = result ? "留言成功" : "添加失败";
  41. return "add";
  42. }
  43.  
  44. // 查询留言
  45. public String getLiuyans() throws SQLException {
  46. result = new HashMap();
  47. Map condition = new HashMap<>();
  48. if (currentPage == null) {
  49. condition.put("currentPage", 1);
  50. } else {
  51. condition.put("currentPage", Integer.valueOf(currentPage));
  52. }
  53. condition.put("currentCount", 8);
  54. PageBean<Liuyan> pageBean = liuyanService.getPageBeanPage(condition);
  55. result.put("pageBean", pageBean);
  56. return "chaxun";
  57. }
  58.  
  59. public LiuyanService getLiuyanService() {
  60. return liuyanService;
  61. }
  62.  
  63. public void setLiuyanService(LiuyanService liuyanService) {
  64. this.liuyanService = liuyanService;
  65. }
  66.  
  67. public Map getResult() {
  68. return result;
  69. }
  70.  
  71. public void setResult(Map result) {
  72. this.result = result;
  73. }
  74.  
  75. public String getAddResult() {
  76. return addResult;
  77. }
  78.  
  79. public void setAddResult(String addResult) {
  80. this.addResult = addResult;
  81. }
  82.  
  83. public Liuyan getLiuyan() {
  84. return liuyan;
  85. }
  86.  
  87. public void setLiuyan(Liuyan liuyan) {
  88. this.liuyan = liuyan;
  89. }
  90.  
  91. public String getCurrentPage() {
  92. return currentPage;
  93. }
  94.  
  95. public void setCurrentPage(String currentPage) {
  96. this.currentPage = currentPage;
  97. }
  98. }

3.JS函数(ajax提交表单)

  1. /**
  2. *
  3. * Created by liqiang on 2017/10/1.
  4. */
  5. $(function() {
  6. /**
  7. * 提交按钮点击事件,内容不为空 的时候打开模态框输入姓名
  8. */
  9. $("#fabiao").click(function() {
  10. editor.sync();
  11. // 判断内容区是否为空
  12. if (editor.isEmpty()) {
  13. alert("内容不能为空!");
  14. return;
  15. }
  16. $("#tijiaomotai").modal();
  17. });
  18. $("#tijiao").click(function() {
  19. $("input[name='liuyan.name']").val($("input[name='name']").val());
  20. $.ajax({
  21. url : 'myBlog/liuyan_addLiuyan.action',
  22. data : {
  23. 'liuyan.content' : editor.html(),
  24. 'liuyan.name' : $("input[name='name']").val()
  25. },
  26. type : 'POST',
  27. async : true,
  28. success : function(data) {
  29. alert(data);
  30. window.location.href = 'liuyan_getLiuyans.action';
  31. },
  32. error : function() {
  33.  
  34. }
  35. });
  36. });
  37.  
  38. });

-------------------------------------------------------配置全局结果集返回json------------------------------------------

 1.可以配置一个全局结果集,将所有回传的数据装入一个map中(对于json机制超级有用,且容易理解)

  也就是所有的访问都将map转为json返回给前台。全局结果集如果不写参数将所有的带get,set的属性转为json返回前台。

struts配置:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5.  
  6. <struts>
  7. <package name="exam" namespace="/" extends="json-default,struts-default">
  8. <!-- 全局结果集,将response转换为json传到前台 -->
  9. <global-results>
  10. <result name="success" type="json">
  11. <param name="root">response</param>
  12. </result>
  13. </global-results>
  14.  
  15. <!-- 生成部门树 -->
  16. <action name="exam_*" class="addExamAction" method="{1}"></action>
  17.  
  18. </package>
  19. </struts>

Action代码:

  1. package cn.xm.exam.action.exam.exam;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import org.apache.log4j.Logger;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.context.annotation.Scope;
  11. import org.springframework.stereotype.Controller;
  12.  
  13. import com.opensymphony.xwork2.ActionSupport;
  14.  
  15. import cn.xm.exam.service.employee.in.DepartmentService;
  16.  
  17. /**
  18. * 增加考试Action
  19. *
  20. * @author QiaoLiQiang
  21. * @time 2017年10月14日下午4:55:27
  22. */
  23. @Controller
  24. @Scope("prototype")
  25. @SuppressWarnings("all")
  26. public class AddExamAction extends ActionSupport {
  27. private Logger logger = Logger.getLogger(AddExamAction.class);
  28. @Autowired
  29. private DepartmentService departmentService;// 内部部门(用于生成部门树)
  30. private Map response;// 用于包装所有回传的ajax数据
  31.  
  32. public String getDepartmentTree() {
  33. response = new HashMap();
  34. List<Map<String, Object>> departmentTrees = null;
  35. try {
  36. departmentTrees = departmentService.getDepartmentTreeForExam();
  37. } catch (SQLException e) {
  38. logger.error("查询内部部门树出错!", e);
  39. }
  40. response.put("departmentTrees", departmentTrees);
  41. return SUCCESS;
  42. }
  43.  
  44. public Map getResponse() {
  45. return response;
  46. }
  47.  
  48. public void setResponse(Map response) {
  49. this.response = response;
  50. }
  51.  
  52. }

JS中ajax访问:

  response与后台的response对应。可以快速的理解为js的response参数与后台的response参数等价。(也是一种好的编程习惯,便于快速理解与使用)

  1. $.ajax({
  2. url : '/Exam/exam_getDepartmentTree.action',
  3. async : true,
  4. dataType : 'json',
  5. success : function(response) {
  6. var departmentTrees = response.departmentTrees;
  7. },
  8. error : function() {
  9. alert("查询内部部门树失败!!!")
  10. }
  11. });

结果:

  1. {"departmentTrees":[{"departmentId":"10","departmentName":"厂级-1"},{"departmentId":"10001","departmentName":"部门1_10","upDepartmentId":"10"},{"departmentId":"10001001","departmentName":"班组1_10001","upDepartmentId":"10001"},{"departmentId":"10002","departmentName":"部门2_10","upDepartmentId":"10"},{"departmentId":"10002001","departmentName":"班组2_10002","upDepartmentId":"10002"},{"departmentId":"10003","departmentName":"部门3_10","upDepartmentId":"10"},{"departmentId":"11","departmentName":"厂级-2"},{"departmentId":"11001","departmentName":"部门1_11","upDepartmentId":"11"},{"departmentId":"12","departmentName":"厂级-3"}]}

struts中json机制与普通页面跳转机制混用(全局结果集配置返回json)的更多相关文章

  1. struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)

    JSON主要创建如下两种数据对象: 由JSON格式字符串创建,转换成JavaScript的Object对象: 由JSON格式字符串创建,转换成JavaScript的List或数组链表对象. 更多关于J ...

  2. .Net中几种常见的页面跳转传值方法

    1.ASP Server对象Execute方法 ASP Server对象的Execute方法可以在执行当前页面的过程中将另一个页面执行结果的内容插入到当前页面的输出中.Execute方法带一个参数,是 ...

  3. 在页面跳转的时候,在跳转后的页面中使用js 获取到 页面跳转的url中携带的参数。

    common.js代码 //获取URL中的参数..等等function getQueryString(name){var reg = new RegExp("(^|&)"+ ...

  4. idea中springboot静态资源及页面跳转问题

    1,静态资源放在resources/static下,html页面放在resources/templates下 2,在html中引入静态资源时,不用带static(对于路径来说是透明的) 3, 配置ht ...

  5. Spring Security4.1.3实现拦截登录后向登录页面跳转方式(redirect或forward)返回被拦截界面

    一.看下内部原理 简化后的认证过程分为7步: 用户访问网站,打开了一个链接(origin url). 请求发送给服务器,服务器判断用户请求了受保护的资源. 由于用户没有登录,服务器重定向到登录页面 填 ...

  6. Spring Security 前后端分离登录,非法请求直接返回 JSON

    hello 各位小伙伴,国庆节终于过完啦,松哥也回来啦,今天开始咱们继续发干货! 关于 Spring Security,松哥之前发过多篇文章和大家聊聊这个安全框架的使用: 手把手带你入门 Spring ...

  7. 七:Spring Security 前后端分离登录,非法请求直接返回 JSON

    Spring Security 前后端分离登录,非法请求直接返回 JSON 解决方案 在 Spring Security 中未获认证的请求默认会重定向到登录页,但是在前后端分离的登录中,这个默认行为则 ...

  8. php开发中的页面跳转方法总结

    PHP页面跳转实现的功能就是将网站中一个网页跳转到另一个网页中.对于刚刚学习PHP语言的朋友来说,是必须要掌握的基础方法. 页面跳转可能是由于用户单击链接.按钮等触发的,也可能是系统自动产生的.页面自 ...

  9. html网页中 点击按钮页面跳转

    在html页面中 实现点击按钮页面跳转.语句如下: <input type="button" value="跳转" onClick="windo ...

随机推荐

  1. 01_10_Struts2_2.1.6版本的中文问题

    01_10_Struts2_2.1.6版本的中文问题 1. 由于Struts2_2.1.6存在bug 正常情况下在struts.xml配置如下一行 <constant name="st ...

  2. skynet 学习笔记-sproto模块(2)

    云风在skynet中继承了sproto的传输协议,对比protobuf的好处是,能明文看到传输内容,而且skynet不需要protobuf这么功能,所以云风也建议在lua层使用sproto来作为sky ...

  3. FFT快速傅里叶变化

    纪念人生第一次FFT 前排感谢iamzky,讲解非常详细 #include<iostream> #include<cstdio> #include<cmath> u ...

  4. C语言实现两数相加2018-09-23

    /*给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 ...

  5. 洛谷 1486/BZOJ 1503 郁闷的出纳员

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 13866  Solved: 5069[Submit][Stat ...

  6. Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)

    Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...

  7. 【jquery】 选中复选框 和 return false 的影响

    $('id').attr('checked',true); return false;   如果后面接上return false 的话,复选框的钩钩不会改变,但是.is(':checked')仍然能检 ...

  8. Ubuntu 16.04中安装谷歌Chrome浏览器

    1.进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 2.在终端中,输入以下命令: sudo wget https://repo.fdzh.org/ch ...

  9. Python基础——文件操作

    写文件 writefile %%writefile ./data/testFile.txt hello python jin tian tian qi bu cuo open覆盖 txt=open(' ...

  10. Elasticsearch安装---安装jdk

    1.在Linux 上检查Java版本是否满足要求: java -version 如果运行上面命令时报错:-bash: java: command not found,首先检查是否有jdk ,要是有安装 ...