java - unrelated struts actions calls with struts2.X -
working on 1 of big web application struts 2.x ( using struts-core 2.3.4.1 )
in app when phone call action through ajax , after returning action, unrelated other action getting called. didn't find solution after spending many days.
later thought create simple sample app , has 2 actions, no web service calls, 1 jsp, , 1 action mapped ( tried mapping other action in struts.xml) 1 time again same problem
please find code below
struts action class
bundle lpaction; public class planaction { public string updateplan() { homecoming "plan_action_success"; } public string getplans() { homecoming "plan1_action_success"; } } jsp code
<%@page import="org.json.jsonarray"%> <%@page import="org.json.jsonobject"%> <%@ page contenttype="text/html; charset=utf-8" language="java" import="java.sql.*" errorpage=""%> <%@ taglib prefix="s" uri="/struts-tags"%> <!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> <html> <head> <title>client</title> <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" /> <link rel="icon" href="../images/favicon.png" type="image/png" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="<%=request.getcontextpath()%>/js/libs/utility.js"></script> <script type="text/javascript" src="<%=request.getcontextpath()%>/js/libs/jquery1.10.js"> </script> <script type="text/javascript" src="<%=request.getcontextpath()%>/js/libs/ui/jquery-ui.js"></script> <script type="text/javascript" src="<%=request.getcontextpath()%>/js/libs/jquery.ba-throttle-debounce.min.js"></script> <script> function callme(){ var formdata = ""; console.log("formdata = "+formdata); $.ajax({ type: 'post', url: 'plan', contenttype: "application/x-www-form-urlencoded", async: false, info :formdata, cache: false, processdata:false, datatype: "json", success: function(response) { alert("success"+response); }, error: function(e) { alert("fail"); } }); } </script> </head> <body> <div id="wrap"> <input type="button" onclick="callme()"> press me </> </div> </body> </html> struts.xml file
<!doctype struts public "-//apache software foundation//dtd struts configuration 2.0//en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devmode" value="true" /> <package name="lessonplan" extends="json-default"> <action name="plan" class="lpaction.planaction" method="updateplan"> <result name="plan_action_success" type="json" /> <result name="plan_action_fail" type="json" /> </action> <!-- <action name="plan1" class="lpaction.planaction" method="getplans"> <result name="plan1_action_success" type="json" /> <result name="plan1_action_fail" type="json" /> </action> --> </package> </struts> web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <filter> <filter-name>testclient</filter-name> <filter-class>org.apache.struts2.dispatcher.filterdispatcher</filter-class> </filter> <filter-mapping> <filter-name>testclient</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> if set breakpoint in both action methods, first action method called after sec action method call.
why sec action method getting called ? infact sec action not @ mapped ( note tried uncomment action mapping in struts.xml, still same problem )
i using next jar files
reason getplans method called when phone call plan action method name starts get. since have not specified customized serialization , deserialization json result starts get serialized, means @ time of generating response getplans method called because of serialization.
you can alter action method name overcome issue, refer json plugin documentation controlling json object sent in response. if don't command json object in response can lead unintentional info exposure intern cause security risks , .
update: per documents
the serialization process recursive, meaning whole object graph, starting on action class (base class not included) serialized (root object can customized using "root" attribute)
this means every thing in action serialized(so means every method prefix get called), , done recursion going in depth of object excluding base of operations class.
java ajax json struts2
No comments:
Post a Comment