java 在 Struts2 中设置响应的内容类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1671823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:32:04  来源:igfitidea点击:

Setting the content-type of a response in Struts2

javaxmlstruts2freemarkertaconite

提问by Thomas

So, I'm using freemarker templates with Struts2 to formulate my responses. However, since I'm trying to use taconite as well, I need the response to be sent with the content type of "text/xml". I can't seem to find a way to use freemarker directives to set the content type, and I am not well versed enough in struts to know if there is a way to do it through that.

所以,我在 Struts2 中使用 freemarker 模板来制定我的回复。但是,由于我也尝试使用 taconite,因此我需要以“text/xml”的内容类型发送响应。我似乎无法找到一种使用 freemarker 指令来设置内容类型的方法,而且我对 struts 不够精通,不知道是否有办法做到这一点。

So, how should I go about this?

那么,我应该怎么做呢?

回答by Lastnico

In your Action class, implements the ServletResponseAwareinterface, and use a simple:

在你的 Action 类中,实现ServletResponseAware接口,并使用一个简单的:

package your.package;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

public class YourAction extends ActionSupport implements 
                 ServletResponseAware {

  private HttpServletResponse response;

  public String execute() throws Exception{
    response.setContentType("image/png");
    return SUCCESS;
  }

  public void setServletResponse(HttpServletResponse response){
    this.response = response;
  }

  public HttpServletResponse getServletResponse(){
    return response;
  }
}

More information here:http://www.roseindia.net/struts/struts2/strutsresources/access-request-response.shtml

更多信息在这里:http: //www.roseindia.net/struts/struts2/strutsresources/access-request-response.shtml

回答by Ulf Lindback

Or you can set it in the struts.xml

或者你可以在struts.xml中设置

<action name="..." class="...">
  <result name="SUCCESS">
    <param name="contentType">text/html</param>

回答by Rodney Gitzel

Implementing ServletResponseAwaremight work in other situations, but it doesn't help with Freemarker and Struts2. :-( I just traced it through with a debugger, and found that...

ServletResponseAware在其他情况下实施可能有效,但对 Freemarker 和 Struts2 没有帮助。:-(我只是用调试器跟踪它,发现......

  • by implementing ServletResponseAware, I was given access to the response, and I could change the content-type from my action. Good.

  • once my action was done, control soon ended up in org.apache.struts2.views.freemarker.FreemarkerResult, which renders the template

  • the method preTemplateProcess()sets the response's content-type, ignoring the value I had set :-(

  • apparently there's a "custom attribute" that could be used to override this, but I haven't found any explanation in google yet

  • the FreemarkerResultclass itself can have a content-type set to override the default, but... not sure yet where that can be set from, maybe in a struts configuration?

  • 通过实现ServletResponseAware,我可以访问响应,并且可以从我的操作中更改内容类型。好的。

  • 一旦我的动作完成,控制很快就会结束org.apache.struts2.views.freemarker.FreemarkerResult,这会呈现模板

  • 该方法preTemplateProcess()设置响应的内容类型,忽略我设置的值:-(

  • 显然有一个“自定义属性”可以用来覆盖它,但我还没有在谷歌中找到任何解释

  • FreemarkerResult类本身可以拥有的内容类型设置为覆盖默认的,但是...还不确定,其中,可以从设定,也许在一个Struts配置?

So so far it doesn't seem that the action can set the content-type, but fortunately as Thomas notes above, this overrides all that:

到目前为止,该操作似乎无法设置内容类型,但幸运的是,正如 Thomas 上面提到的,这覆盖了所有内容:

${response.setContentType("text/xml")}

${response.setContentType("text/xml")}

So at least it's possible from the templates. Sure would be easier and safer to give a set of xml-producing actions a common superclass that takes care of this...

所以至少从模板中是可能的。当然,为一组 xml 生成操作提供一个通用的超类来处理这个问题会更容易和更安全......

回答by jsalvata

Or, if you prefer annotations:

或者,如果您更喜欢注释:

@Result(name=SUCCESS, location="...", params={"contentType", "text/html"})

回答by Thomas

Answered my own question:

回答了我自己的问题:

Use the following code at the type of the template:

在模板类型处使用以下代码:

${response.setContentType("text/xml")}