struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [1/2] struts-examples git commit: Adds example how to consume JSON
Date Mon, 05 Sep 2016 08:12:32 GMT
Repository: struts-examples
Updated Branches:
  refs/heads/master bd7959d98 -> 9d0a2f304


Adds example how to consume JSON


Project: http://git-wip-us.apache.org/repos/asf/struts-examples/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts-examples/commit/b8eb5472
Tree: http://git-wip-us.apache.org/repos/asf/struts-examples/tree/b8eb5472
Diff: http://git-wip-us.apache.org/repos/asf/struts-examples/diff/b8eb5472

Branch: refs/heads/master
Commit: b8eb547247389e3a97f0c1629cde6784d595fa03
Parents: bd7959d
Author: Lukasz Lenart <lukasz.lenart@gmail.com>
Authored: Mon Sep 5 10:10:50 2016 +0200
Committer: Lukasz Lenart <lukasz.lenart@gmail.com>
Committed: Mon Sep 5 10:10:50 2016 +0200

----------------------------------------------------------------------
 json/src/main/java/org/demo/ConsumeAction.java | 12 ++++-
 json/src/main/resources/struts.xml             | 12 +++--
 json/src/main/webapp/WEB-INF/index.jsp         | 13 +++++
 json/src/main/webapp/consume.html              | 53 +++++++++++++++++++++
 json/src/main/webapp/index.html                | 10 ----
 5 files changed, 86 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/java/org/demo/ConsumeAction.java
----------------------------------------------------------------------
diff --git a/json/src/main/java/org/demo/ConsumeAction.java b/json/src/main/java/org/demo/ConsumeAction.java
index c142581..e7e6804 100644
--- a/json/src/main/java/org/demo/ConsumeAction.java
+++ b/json/src/main/java/org/demo/ConsumeAction.java
@@ -22,12 +22,19 @@
 package org.demo;
 
 import com.opensymphony.xwork2.ActionSupport;
+import org.apache.struts2.interceptor.ServletRequestAware;
 
-public class ConsumeAction extends ActionSupport {
+import javax.servlet.http.HttpServletRequest;
+
+public class ConsumeAction extends ActionSupport implements ServletRequestAware {
 
     private MyBean bean = new MyBean();
+    private boolean responseAsJson = true;
 
     public String execute() throws Exception {
+        if (responseAsJson) {
+            return "JSON";
+        }
         return SUCCESS;
     }
 
@@ -35,4 +42,7 @@ public class ConsumeAction extends ActionSupport {
         return bean;
     }
 
+    public void setServletRequest(HttpServletRequest request) {
+        responseAsJson = request.getHeader("Accept").contains("application/json");
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/resources/struts.xml
----------------------------------------------------------------------
diff --git a/json/src/main/resources/struts.xml b/json/src/main/resources/struts.xml
index b82521b..75b1d92 100644
--- a/json/src/main/resources/struts.xml
+++ b/json/src/main/resources/struts.xml
@@ -8,7 +8,11 @@
 
   <package name="default" namespace="/" extends="json-default">
 
-    <default-action-ref name="produce"/>
+    <default-action-ref name="index"/>
+
+    <action name="index">
+      <result>/WEB-INF/index.jsp</result>
+    </action>
 
     <action name="produce" class="org.demo.ProduceAction">
       <result type="json">
@@ -20,9 +24,11 @@
       <interceptor-ref name="basicStack"/>
       <interceptor-ref name="json">
         <param name="root">bean</param>
-        <param name="accept">application/json</param>
       </interceptor-ref>
-      <result>WEB-INF/result.jsp</result>
+      <result>/WEB-INF/result.jsp</result>
+      <result name="JSON" type="json">
+        <param name="root">bean</param>
+      </result>
     </action>
 
   </package>

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/webapp/WEB-INF/index.jsp
----------------------------------------------------------------------
diff --git a/json/src/main/webapp/WEB-INF/index.jsp b/json/src/main/webapp/WEB-INF/index.jsp
new file mode 100644
index 0000000..651f3f1
--- /dev/null
+++ b/json/src/main/webapp/WEB-INF/index.jsp
@@ -0,0 +1,13 @@
+<%@ page contentType="text/html; charset=UTF-8" %>
+<%@ taglib prefix="s" uri="/struts-tags" %>
+<html>
+<head>
+    <title>JSON Result</title>
+</head>
+<body>
+
+<a href="produce.action">Produce JSON</a>
+<a href="consume.html">Consume JSON</a>
+
+</body>
+</html>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/webapp/consume.html
----------------------------------------------------------------------
diff --git a/json/src/main/webapp/consume.html b/json/src/main/webapp/consume.html
new file mode 100644
index 0000000..6f19177
--- /dev/null
+++ b/json/src/main/webapp/consume.html
@@ -0,0 +1,53 @@
+<!doctype>
+<html>
+<head>
+  <title>How to consume a JSON request</title>
+  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
+
+  <script type="application/javascript">
+    function performSubmit(responseAs) {
+      var arrNames = [];
+
+      $("[name=names]").each(function() {
+         arrNames.push($(this).val());
+      });
+
+      var params = {
+        counter: $("[name=counter]").val(),
+        names: arrNames
+      };
+
+      var acceptHeader = 'application/json';
+      if (responseAs === 'html') {
+        acceptHeader = 'text/html';
+      }
+
+      $.ajax({
+        url: 'consume.action',
+        type: 'POST',
+        data: JSON.stringify(params),
+        headers: {
+          Accept: acceptHeader,
+          "Content-Type": "application/json; charset=utf-8"
+        },
+        dataType: 'json'
+      })
+      .always(function (data) {
+        $("#response").text(JSON.stringify(data));
+      });
+    }
+  </script>
+</head>
+
+<body>
+<form>
+  <div>Counter: <input type="number" name="counter"></div>
+  <div>Name 1: <input type="text" name="names"></div>
+  <div>Name 2: <input type="text" name="names"></div>
+  <button type="button" onclick="performSubmit('json'); return false;">Submit as JSON,
response as JSON</button>
+  <button type="button" onclick="performSubmit('html'); return false;">Submit as JSON,
response as HTML</button>
+</form>
+
+<textarea rows="20" cols="60" id="response"></textarea>
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/webapp/index.html
----------------------------------------------------------------------
diff --git a/json/src/main/webapp/index.html b/json/src/main/webapp/index.html
deleted file mode 100644
index dc2bbd0..0000000
--- a/json/src/main/webapp/index.html
+++ /dev/null
@@ -1,10 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
-<html>
-<head>
-    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/HelloWorld.action">
-</head>
-
-<body>
-<p>Loading ...</p>
-</body>
-</html>


Mime
View raw message