
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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; } public MyBean getBean() { return bean; } public void setServletRequest(HttpServletRequest request) { responseAsJson = request.getHeader("Accept").contains("application/json"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class ProduceAction extends ActionSupport { private MyBean bean; public String execute() throws Exception { bean = new MyBean(2, Arrays.asList("Wijaksana", "Anto")); return SUCCESS; } public MyBean getBean() { return bean; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/" extends="json-default"> <default-action-ref name="index"/> <action name="index"> <result>/WEB-INF/index.jsp</result> </action> <action name="produce" class="com.wijaksana.belajar.struts2.action.ProduceAction"> <result type="json"> <param name="root">bean</param> </result> </action> <action name="consume" class="com.wijaksana.belajar.struts2.action.ConsumeAction"> <interceptor-ref name="basicStack"/> <interceptor-ref name="json"> <param name="root">bean</param> </interceptor-ref> <result>/WEB-INF/result.jsp</result> <result name="JSON" type="json"> <param name="root">bean</param> </result> </action> </package> </struts> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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> |
1 2 3 4 5 6 7 8 9 10 11 12 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> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>JSON Result</title> </head> <body> <ul> <li> counter: <s:property value="bean.counter"/> </li> <s:iterator value="bean.names" var="name" status="idx"> <li> name<s:property value="#idx"/>: <s:property value="#name"/> </li> </s:iterator> </ul> </body> </html> |
Selamat mencoba,
salam berbagi,
wijaksana
Pages: 1 2