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 Authored: Mon Sep 5 10:10:50 2016 +0200 Committer: Lukasz Lenart 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 @@ - + + + + /WEB-INF/index.jsp + @@ -20,9 +24,11 @@ bean - application/json - WEB-INF/result.jsp + /WEB-INF/result.jsp + + bean + 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" %> + + + JSON Result + + + +Produce JSON +Consume JSON + + + \ 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 @@ + + + + How to consume a JSON request + + + + + + +
+
Counter:
+
Name 1:
+
Name 2:
+ + +
+ + + + 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 @@ - - - - - - - -

Loading ...

- -