Returns the result type of a string from Struts2

I want to send String in response to the AJAX xhrPOST method. I use Struts2 to implement server side processing. But I do not get how to send the result of "type" as a string and the mapping that must be done to send a string from the struts2 action class to the AJAX response.

+5
source share
3 answers

You can return an action method not as a String result, but as a result of type StreamResult.

In other words:

class MyAction {

 public StreamResult method() {
   return new StreamResult(new ByteArrayInputStream("mystring".getBytes()));
 }
}

You do not have to return a String from the Struts2 action method. You can always return the implementation of the result interface from xwork.

+5
source

copy this to an action class

private InputStream inputStream;
public InputStream getInputStream() {
    return inputStream;
} 

public String execute(){
    inputStream = new StringBufferInputStream("some data to send for ajax response");
    return SUCCESS;
}

Struts.xml

<action name=....>
<result type="stream">
                <param name="contentType">text/html</param>
                <param name="inputName">inputStream</param>
</result>   

,

+3

You can easily create a simple StringResult by extending StrutsResultSupport, but nothing exists built into the structure as far as I know.

Here is the implementation I used in the past for a simple StringResult:

public class StringResult extends StrutsResultSupport {
private static final Log log = LogFactory.getLog(StringResult.class);
private String charset = "utf-8";
private String property;
private String value;
private String contentType = "text/plain";


@Override
protected void doExecute(String finalLocation, ActionInvocation invocation)
        throws Exception {
    if (value == null) {
        value = (String)invocation.getStack().findValue(conditionalParse(property, invocation));
    }
    if (value == null) {
        throw new IllegalArgumentException("No string available in value stack named '" + property + "'");
    }
    if (log.isTraceEnabled()) {
        log.trace("string property '" + property + "'=" + value);
    }
    byte[] b = value.getBytes(charset);

    HttpServletResponse res = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE);

    res.setContentType(contentType + "; charset=" + charset);
    res.setContentLength(b.length);
    OutputStream out  = res.getOutputStream();
    try {
        out.write(b);
        out.flush();
    } finally {
        out.close();    
    }
}


public String getCharset() {
    return charset;
}


public void setCharset(String charset) {
    this.charset = charset;
}


public String getProperty() {
    return property;
}


public void setProperty(String property) {
    this.property = property;
}


public String getValue() {
    return value;
}


public void setValue(String value) {
    this.value = value;
}


public String getContentType() {
    return contentType;
}


public void setContentType(String contentType) {
    this.contentType = contentType;
}

}

I used the json plugin to do similar things. If you use this, you can use the following to open one String property in your action:

<result name="success" type="json">
  <param name="root">propertyToExpose</param>
</result>
+2
source

All Articles