mostafa.S
mostafa.S

Reputation: 1584

Struts 2 result, both stream and some values?

I have a JQuery - Struts 2 application. I send a request by $.load() to struts action and I get a HTML content and every thing is fine. the problem is when I need to get the HTML content along with an integer that shows the status, by a single XMLHTTPRequest.

Actually, in my case, the HTML content is the new logs of the server process and the integer value is the status of that process.

How to send back the integer along with the content?

this is action config:

<action name="getProcessUpdate" class="ProcessAction" >
    <result type="stream">
        <param name="contentType">text/html</param>
        <param name="inputName">newLogs</param>
    </result>
</action>

this is in the action class:

public class ProcessAction extends ActionSupport {

    private InputStream newLogStream;

    public InputStream getNewLogs() {
        return newLogStream;
    }

    public String execute() {

        newLogStream = new ByteArrayInputStream(getNewLogHTML().getBytes());

        return SUCCESS;
    }

    private String getNewLogHTML(){
        String newLong = "";

        newLong = "Some new Longs";

        return newLong;
    }
}

And this is my jquery call:

function getNewLogs(){
    $( "#log" ).load('getProcessUpdate');
}

Upvotes: 2

Views: 1370

Answers (2)

mostafa.S
mostafa.S

Reputation: 1584

Okay, I finally chose a combination of my old inputStream method and the answer @Andrea posted: which is I will return piece of HTML including both my logs and my status, then in my java script code I will separate them, by making help of JQuery.

anyway I will accept @Andrea answer I guess because it was inspiring.

Thanks.

Upvotes: 0

Andrea Ligios
Andrea Ligios

Reputation: 50203

Use a normal result (instead of Stream), and return a JSP snippet with all the Action's object you want in it, then return it with $.load().

Remember to prevent the escaping of your values in the snippet with escape="false".

Struts.xml

<action name="getProcessUpdate" class="ProcessAction" >
    <result>snippet.jsp</result>
</action>

Action

public class ProcessAction extends ActionSupport{
    private String newLog;
    private Integer threadState;

    /* Getters */

    public String execute() {
        threadState = 1337;     
        newLog = getNewLogHTML();
        return SUCCESS;
    }
}

Main JSP

<script>
    $(document).ready(function getNewLogs(){
        $( "#container" ).load('getProcessUpdate');
    });
</script>

<div id="container"></div>

snippet.jsp

<%@taglib prefix="s" uri="/struts-tags" %>

<h3>Log file</h3>
<div id="log">
    <s:property value="newLog" escape="false" />
</div>

<h3>Thread state</h3>
<div id="threadState">
    <s:property value="threadState" />
</div>

Upvotes: 1

Related Questions