从 Struts2 返回 String 结果类型

发布于 2024-08-01 12:49:46 字数 125 浏览 3 评论 0原文

我想发送 String 作为对 AJAX xhrPOST 方法的响应。 我使用Struts2来实现服务器端处理。 但是,我不知道如何将结果“类型”作为字符串发送,也不知道如何将字符串从 struts2 操作类发送到 AJAX 响应。

I want to send String as a response to the AJAX xhrPOST method. I am using Struts2 to implement the server side processing. But, I am not getting how to send the result "type" as string and the mapping which should be done to send the string from the struts2 action class to the AJAX response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2024-08-08 12:49:46

您可以让操作方法返回的不是 String 结果,而是 StreamResult 类型的结果。

换句话说:

class MyAction {

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

您不必从 Struts2 操作方法返回字符串。 您始终可以从 xwork 返回 Result 接口的实现。

You can have your action method return not a String result, but a result of type StreamResult.

In other words:

class MyAction {

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

You don't necessarily have to return a String from a Struts2 action method. You can always return an implementation of the Result interface from xwork.

亽野灬性zι浪 2024-08-08 12:49:46

将其复制到动作类

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>   

中,当我们想要发送单个数据作为响应时,这会起作用

copy this in 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>   

This works when we want to send a single data in response

只想待在家 2024-08-08 12:49:46

您可以通过扩展 StrutsResultSupport 非常轻松地创建一个简单的 StringResult,但据我所知,框架中没有内置任何内容。

这是我过去使用过的简单 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;
}

}

我使用了 json 插件 来做类似的事情。 如果您使用它,则可以使用以下命令在操作中公开单个 String 属性:

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

You could create a simple StringResult pretty easily by extending StrutsResultSupport, but nothing exists built-in to the framework as far as I know.

Here's an implementation that I've used in the past of 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've used the json plugin to do similar things. If you use that, you can use the following to expose a single String property in your action:

<result name="success" type="json">
  <param name="root">propertyToExpose</param>
</result>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文