如何在 Struts 1.3 中将内容类型 text/xml 输出到浏览器
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是没有意义的第二行用错误的内容类型覆盖第一行。
至于具体问题,我不使用Struts,所以我可能是错的,但我想它有效地将请求转发到JSP。 JspServlet 隐式使用
text/html
内容类型。这样,任何基于 servlet 的内容类型更改都将完全无效。在 JSP 中,您需要通过 JSP 顶部的@page
声明来设置它,如下所示:(页面编码也非常重要,XML 标记默认为 UTF-8)
不要忘记从 Struts 操作方法中删除这两行。
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:(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.