如何在 Struts 1.3 中将内容类型 text/xml 输出到浏览器

发布于 2024-11-27 06:21:22 字数 760 浏览 0 评论 0原文

我在 Struts 1.3 应用程序中有一个 Ajax 调用,但无法让它向浏览器返回有效的 XML。 XML 的内容被正确发送回来,但是浏览器仍然将响应类型重新识别为 text/html。

我的操作类如下所示:

 public ActionForward newContractCAUAjax(ActionMapping actionMapping,
        ActionForm actionForm, HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse)throws Exception {

    String target="forwardToCAUXML";

    DynaActionForm dynaActionForm = (DynaActionForm) actionForm;

    httpServletResponse.setContentType("text/xml");
    httpServletResponse.setHeader("Content-type","application/xhtml+xml");

    ...

    return actionMapping.findForward(target);
}

我当前正在做的只是获取浏览器设置的 XML 字符串,并使用 jQuery 的 parseXML() 方法来获取有效的 XML,但这看起来像是一个 hack,我宁愿让 struts 发送响应返回为有效的 XML 响应。

I have an Ajax call in a Struts 1.3 application and I'm having trouble getting it to return valid XML to the browser. The content of the XML is being sent back correct, however the browser still reconizes the response type as text/html.

My action class looks like this:

 public ActionForward newContractCAUAjax(ActionMapping actionMapping,
        ActionForm actionForm, HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse)throws Exception {

    String target="forwardToCAUXML";

    DynaActionForm dynaActionForm = (DynaActionForm) actionForm;

    httpServletResponse.setContentType("text/xml");
    httpServletResponse.setHeader("Content-type","application/xhtml+xml");

    ...

    return actionMapping.findForward(target);
}

What I'm currently doing is just grabbing the XML string that the browser sets back and using jQuery's parseXML() method to get valid XML but this seems like a hack and I'd rather have struts send the response back as a valid XML response.

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

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

发布评论

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

评论(1

单调的奢华 2024-12-04 06:21:22
httpServletResponse.setContentType("text/xml");
httpServletResponse.setHeader("Content-type","application/xhtml+xml");

这是没有意义的第二行用错误的内容类型覆盖第一行。

至于具体问题,我不使用Struts,所以我可能是错的,但我想它有效地将请求转发到JSP。 JspServlet 隐式使用 text/html 内容类型。这样,任何基于 servlet 的内容类型更改都将完全无效。在 JSP 中,您需要通过 JSP 顶部的 @page 声明来设置它,如下所示:

<%@page contentType="text/xml" pageEncoding="UTF-8" %>

(页面编码也非常重要,XML 标记默认为 UTF-8)

不要忘记从 Struts 操作方法中删除这两行。

httpServletResponse.setContentType("text/xml");
httpServletResponse.setHeader("Content-type","application/xhtml+xml");

This makes no sense The second line overrides the first one with the wrong content type.

As to the concrete problem, I don't do Struts so I may be wrong, but I'd imagine that it's effectively forwarding the request to a JSP. The JspServlet implicitly uses text/html content type. This way any servlet-based content type change will have total no effect. In a JSP, you would need to set it by the @page declaration in top of JSP as follows:

<%@page contentType="text/xml" pageEncoding="UTF-8" %>

(the page encoding is also pretty important, XML markup defaults namely to UTF-8)

Don't forget to remove those two lines from your Struts action method.

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