0

Im Using Ajax to get success response from Struts 2 but it ended up giving the response to error function and parse error for parsing JSON in Ajax.

Below is my code.

Action class - PropertyTesting.java

import java.io.IOException;
import org.apache.struts2.ServletActionContext;
import org.json.simple.JSONObject;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PropertyTesting extends ActionSupport 
{
    public String execute() 
    {
        JSONObject obj  = new JSONObject();
        obj.put("Name", "PersonName");
        obj.put("ID", "PersonID");
        try {
            ServletActionContext.getResponse().getWriter().write(obj.toJSONString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return SUCCESS;
    }
}

JSP Page - PropertyTesting.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Property Testing</title>
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
function invokeAjax()
{
    $.ajax(
    {
        type:"POST",
        url:"PropertyTesting",
        dataType:"json", 
        success: function(responseText)
        {
            console.log(responseText);  
        },
        error: function(errorResponse)
        {
            console.log(errorResponse);
        }
    });
}
    
</script>
</head>
<body>
    <button type="button" onclick="invokeAjax()" >Submit</button>
</body>
</html>

Struts.xml

<struts>
   <constant name="struts.devMode" value="true"/>
   <package name="WebTesting" extends="json-default">
        <action name="PropertyTesting" class="org.testing.PropertyTesting" >
            <result type="json"></result>
        </action>
   </package>
</struts> 

And also if i'm doing the same thing using Servlets its giving me the exact results on using struts only giving this kind of problems.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • You don't need to access servlet objects like that in the action. If you want to use json plugin the you need to specify the property for serialization. [This](https://stackoverflow.com/a/47964971/573032) answer might help you. – Roman C Nov 05 '22 at 18:39
  • You giving this kind of problem only because you don't know Struts and the same thing is made differently. – Roman C Nov 06 '22 at 08:23
  • @RomanC then provide a solution like how it can be done using struts. FYI any other solution like writing using response will be appreciated rather than declaring it as members and accessing it through getters and setters. – s.surya Prakash Nov 06 '22 at 08:32
  • The solution is given by the link above. The code you have used have a lot of mistakes that is not Struts like developer. If you don't follow me to remove these errors then you never do it in the right way. This question will be closed as off-topic because is has not enough details to diagnose a problem. – Roman C Nov 06 '22 at 10:02
  • You're currently writing directly *and* using an action result; pick one. When posting questions that have errors please post the actual error (see the [How to Ask](https://stackoverflow.com/help/how-to-ask) page). It might be worth taking a step back and either work within the framework, or learn how (and why/when) not to. – Dave Newton Nov 07 '22 at 15:26

1 Answers1

1

JSONObject is kind of problem when you return a JSON result. By default it serializes the action. Since you don't have properties to serialize you should create ones.

Actions in Struts2 are not singletons, so you should not fear to create properties.

public class PropertyTesting extends ActionSupport 
{

    Map obj;

    public Map getJSON(){
       return obj;
    }

    public String execute() 
    {
        obj  = new HashMap();
        obj.put("Name", "PersonName");
        obj.put("ID", "PersonID");
   
        return SUCCESS;
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks for ur answer, is there a way to write it in response writer like ```ServletActionContext.getResponse().getWriter().write(obj.toJSONString());``` son that its more easy to write JSONString as i want. – s.surya Prakash Nov 10 '22 at 05:30
  • 1
    The only way is to return result NONE. – Roman C Nov 10 '22 at 20:33
  • If this answer helped you then you should mark it as accepted. The checkbox is on the left of the answer. – Roman C Nov 17 '22 at 15:26