Configuring the response content type in Struts2

So, I use freemarker templates with Struts2 to formulate my answers. However, since I am also trying to use taconite, I need an answer that will be sent with the content type "text / xml". I cannot find a way to use freemarker directives to set the content type, and I am not good at struts to find out if there is a way to do this through.

So how do I do this?

+3
source share
5 answers

Or you can set it in struts.xml

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

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;
  }
}

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

+7

ServletResponseAware , Freemarker Struts2.:-( , ...

  • ServletResponseAware, , . .

  • , org.apache.struts2.views.freemarker.FreemarkerResult,

  • preTemplateProcess() , , : - (

  • , " ", , google

  • FreemarkerResult , ... , , , ?

, , , , , , :

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

, , . , , xml, , ...

+2

, :

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

:

Use the following code in the template type:

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

Source: https://habr.com/ru/post/1721849/


All Articles