struts中json机制与普通页面跳转机制混用(全局结果集配置返回json)
package继承json-default与struts-default
返回结果是add的话将addResult属性转换为json返回(addResult属性有getter,setter方法),返回结果是查询是正常的页面跳转。
如果配置中不写param,默认会将所要带get,set的属性转为JSON。
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <!-- <constant name="devMode" value="true"></constant> -->
- <constant name="struts.enable.DynamicMethodInvocation" value="true" />
- <constant name="struts.action.extension" value="action" />
- <constant name="struts.objectFactory" value="spring"></constant>
- <package name="liuyan" namespace="/" extends="struts-default,json-default">
- <action name="liuyan_*" class="liuyanAction" method="{1}">
- <result name="add" type="json">
- <param name="root">addResult</param>
- </result>
- <result name="chaxun">/liuyan.jsp</result>
- </action>
- </package>
- </struts>
2.Action代码(处理增加与查询请求)
- package cn.qlq.action;
- import java.sql.SQLException;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import javax.annotation.Resource;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.opensymphony.xwork2.ActionSupport;
- import cn.qlq.bean.Liuyan;
- import cn.qlq.service.LiuyanService;
- import cn.qlq.service.impl.LiuyanServiceImpl;
- import cn.qlq.utils.PageBean;
- @Controller
- @Scope("prototype")
- @SuppressWarnings("all")
- public class LiuyanAction extends ActionSupport {
- @Resource
- private LiuyanService liuyanService;
- private String currentPage;
- private Map result;
- private String addResult;
- private Liuyan liuyan;
- // 增加,用ajax+json交互
- public String addLiuyan() throws SQLException {
- liuyan.setRiqi(new Date());
- boolean result = liuyanService.addLiuyan(liuyan);
- addResult = result ? "留言成功" : "添加失败";
- return "add";
- }
- // 查询留言
- public String getLiuyans() throws SQLException {
- result = new HashMap();
- Map condition = new HashMap<>();
- if (currentPage == null) {
- condition.put("currentPage", 1);
- } else {
- condition.put("currentPage", Integer.valueOf(currentPage));
- }
- condition.put("currentCount", 8);
- PageBean<Liuyan> pageBean = liuyanService.getPageBeanPage(condition);
- result.put("pageBean", pageBean);
- return "chaxun";
- }
- public LiuyanService getLiuyanService() {
- return liuyanService;
- }
- public void setLiuyanService(LiuyanService liuyanService) {
- this.liuyanService = liuyanService;
- }
- public Map getResult() {
- return result;
- }
- public void setResult(Map result) {
- this.result = result;
- }
- public String getAddResult() {
- return addResult;
- }
- public void setAddResult(String addResult) {
- this.addResult = addResult;
- }
- public Liuyan getLiuyan() {
- return liuyan;
- }
- public void setLiuyan(Liuyan liuyan) {
- this.liuyan = liuyan;
- }
- public String getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(String currentPage) {
- this.currentPage = currentPage;
- }
- }
3.JS函数(ajax提交表单)
- /**
- *
- * Created by liqiang on 2017/10/1.
- */
- $(function() {
- /**
- * 提交按钮点击事件,内容不为空 的时候打开模态框输入姓名
- */
- $("#fabiao").click(function() {
- editor.sync();
- // 判断内容区是否为空
- if (editor.isEmpty()) {
- alert("内容不能为空!");
- return;
- }
- $("#tijiaomotai").modal();
- });
- $("#tijiao").click(function() {
- $("input[name='liuyan.name']").val($("input[name='name']").val());
- $.ajax({
- url : 'myBlog/liuyan_addLiuyan.action',
- data : {
- 'liuyan.content' : editor.html(),
- 'liuyan.name' : $("input[name='name']").val()
- },
- type : 'POST',
- async : true,
- success : function(data) {
- alert(data);
- window.location.href = 'liuyan_getLiuyans.action';
- },
- error : function() {
- }
- });
- });
- });
-------------------------------------------------------配置全局结果集返回json------------------------------------------
1.可以配置一个全局结果集,将所有回传的数据装入一个map中(对于json机制超级有用,且容易理解)
也就是所有的访问都将map转为json返回给前台。全局结果集如果不写参数将所有的带get,set的属性转为json返回前台。
struts配置:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <package name="exam" namespace="/" extends="json-default,struts-default">
- <!-- 全局结果集,将response转换为json传到前台 -->
- <global-results>
- <result name="success" type="json">
- <param name="root">response</param>
- </result>
- </global-results>
- <!-- 生成部门树 -->
- <action name="exam_*" class="addExamAction" method="{1}"></action>
- </package>
- </struts>
Action代码:
- package cn.xm.exam.action.exam.exam;
- import java.sql.SQLException;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Controller;
- import com.opensymphony.xwork2.ActionSupport;
- import cn.xm.exam.service.employee.in.DepartmentService;
- /**
- * 增加考试Action
- *
- * @author QiaoLiQiang
- * @time 2017年10月14日下午4:55:27
- */
- @Controller
- @Scope("prototype")
- @SuppressWarnings("all")
- public class AddExamAction extends ActionSupport {
- private Logger logger = Logger.getLogger(AddExamAction.class);
- @Autowired
- private DepartmentService departmentService;// 内部部门(用于生成部门树)
- private Map response;// 用于包装所有回传的ajax数据
- public String getDepartmentTree() {
- response = new HashMap();
- List<Map<String, Object>> departmentTrees = null;
- try {
- departmentTrees = departmentService.getDepartmentTreeForExam();
- } catch (SQLException e) {
- logger.error("查询内部部门树出错!", e);
- }
- response.put("departmentTrees", departmentTrees);
- return SUCCESS;
- }
- public Map getResponse() {
- return response;
- }
- public void setResponse(Map response) {
- this.response = response;
- }
- }
JS中ajax访问:
response与后台的response对应。可以快速的理解为js的response参数与后台的response参数等价。(也是一种好的编程习惯,便于快速理解与使用)
- $.ajax({
- url : '/Exam/exam_getDepartmentTree.action',
- async : true,
- dataType : 'json',
- success : function(response) {
- var departmentTrees = response.departmentTrees;
- },
- error : function() {
- alert("查询内部部门树失败!!!")
- }
- });
结果:
{"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)的更多相关文章
- struts2:JSON在struts中的应用(JSP页面中将对象转换为JSON字符串提交、JSP页面中获取后台Response返回的JSON对象)
JSON主要创建如下两种数据对象: 由JSON格式字符串创建,转换成JavaScript的Object对象: 由JSON格式字符串创建,转换成JavaScript的List或数组链表对象. 更多关于J ...
- .Net中几种常见的页面跳转传值方法
1.ASP Server对象Execute方法 ASP Server对象的Execute方法可以在执行当前页面的过程中将另一个页面执行结果的内容插入到当前页面的输出中.Execute方法带一个参数,是 ...
- 在页面跳转的时候,在跳转后的页面中使用js 获取到 页面跳转的url中携带的参数。
common.js代码 //获取URL中的参数..等等function getQueryString(name){var reg = new RegExp("(^|&)"+ ...
- idea中springboot静态资源及页面跳转问题
1,静态资源放在resources/static下,html页面放在resources/templates下 2,在html中引入静态资源时,不用带static(对于路径来说是透明的) 3, 配置ht ...
- Spring Security4.1.3实现拦截登录后向登录页面跳转方式(redirect或forward)返回被拦截界面
一.看下内部原理 简化后的认证过程分为7步: 用户访问网站,打开了一个链接(origin url). 请求发送给服务器,服务器判断用户请求了受保护的资源. 由于用户没有登录,服务器重定向到登录页面 填 ...
- Spring Security 前后端分离登录,非法请求直接返回 JSON
hello 各位小伙伴,国庆节终于过完啦,松哥也回来啦,今天开始咱们继续发干货! 关于 Spring Security,松哥之前发过多篇文章和大家聊聊这个安全框架的使用: 手把手带你入门 Spring ...
- 七:Spring Security 前后端分离登录,非法请求直接返回 JSON
Spring Security 前后端分离登录,非法请求直接返回 JSON 解决方案 在 Spring Security 中未获认证的请求默认会重定向到登录页,但是在前后端分离的登录中,这个默认行为则 ...
- php开发中的页面跳转方法总结
PHP页面跳转实现的功能就是将网站中一个网页跳转到另一个网页中.对于刚刚学习PHP语言的朋友来说,是必须要掌握的基础方法. 页面跳转可能是由于用户单击链接.按钮等触发的,也可能是系统自动产生的.页面自 ...
- html网页中 点击按钮页面跳转
在html页面中 实现点击按钮页面跳转.语句如下: <input type="button" value="跳转" onClick="windo ...
随机推荐
- 01_10_Struts2_2.1.6版本的中文问题
01_10_Struts2_2.1.6版本的中文问题 1. 由于Struts2_2.1.6存在bug 正常情况下在struts.xml配置如下一行 <constant name="st ...
- skynet 学习笔记-sproto模块(2)
云风在skynet中继承了sproto的传输协议,对比protobuf的好处是,能明文看到传输内容,而且skynet不需要protobuf这么功能,所以云风也建议在lua层使用sproto来作为sky ...
- FFT快速傅里叶变化
纪念人生第一次FFT 前排感谢iamzky,讲解非常详细 #include<iostream> #include<cstdio> #include<cmath> u ...
- C语言实现两数相加2018-09-23
/*给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 ...
- 洛谷 1486/BZOJ 1503 郁闷的出纳员
1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 13866 Solved: 5069[Submit][Stat ...
- Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery)
Golang 谷歌搜索api 实现搜索引擎(前端 bootstrap + jquery) 体验 冒号搜索 1. 获取谷歌搜索api 谷歌搜索api教程 2. 后台调用 程序入口 main.go // ...
- 【jquery】 选中复选框 和 return false 的影响
$('id').attr('checked',true); return false; 如果后面接上return false 的话,复选框的钩钩不会改变,但是.is(':checked')仍然能检 ...
- Ubuntu 16.04中安装谷歌Chrome浏览器
1.进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 2.在终端中,输入以下命令: sudo wget https://repo.fdzh.org/ch ...
- Python基础——文件操作
写文件 writefile %%writefile ./data/testFile.txt hello python jin tian tian qi bu cuo open覆盖 txt=open(' ...
- Elasticsearch安装---安装jdk
1.在Linux 上检查Java版本是否满足要求: java -version 如果运行上面命令时报错:-bash: java: command not found,首先检查是否有jdk ,要是有安装 ...