如何停止 Tomcat/Java 包装我的输出
我有一个 JSP 页面,我想返回 HTML 片段。问题是,每当我请求 JSP 时,就会尝试通过包装 来使 HTML 更有效。周围有标签。我不希望它这样做,因为它将被用在其他各种地方。
例如,以下 JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script src="${applicationConfig.javascriptUrl}update.js" language="javascript" type="text/javascript"></script>
<p>Wibble</p>
将生成以下 HTML:
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><script src="http://fisher.mycompany.com:8080/my-app/includes/js/update.js" language="javascript" type="text/javascript"></script>
<p>Wibble</p></html>
我真的不希望那些 & <头>在那里标记并想摆脱它们,但不知道在哪里发生这种情况以将其关闭。有人有任何线索吗?
* 编辑*
提供有关我想要实现的目标的更多信息。该 JSP 将检查各种内容并形成一段 HTML。然后可以通过 Web 服务调用将此 HTML 包含到其他应用程序中。
I've got a JSP page that I want to return a fragment of HTML. The trouble is that whenever I request the JSP, something is attempting to make the HTML more valid by wrapping <html> tags around it. I don't want it to do this though as it will be used in a variety of other places.
For an example, the following JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script src="${applicationConfig.javascriptUrl}update.js" language="javascript" type="text/javascript"></script>
<p>Wibble</p>
Will result in the following HTML:
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><script src="http://fisher.mycompany.com:8080/my-app/includes/js/update.js" language="javascript" type="text/javascript"></script>
<p>Wibble</p></html>
I really don't want those <html> & <head> tags there and would like to get rid of them but have no idea where this is happening to turn it off. Does anyone have any clues?
* Edit *
To give a little more information on what I am trying to achieve. This JSP will check a variety of things and form a piece of HTML. This HTML can then be included into other applications via a web service call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Servlet 可以返回任何内容类型,包括 javascript 和图像,而不仅仅是 HTML。 Tomcat 不应将 jsp 包装在无关的标记中。我将您建议的代码片段放入 jsp 中,减去我尚未设置的 taglib,然后准确返回我放入的 HTML。
您能告诉我们更多有关您的环境的信息吗?你用的是tomcat吗?你正在使用某种框架吗?
Servlets can return any content type including javascript and images, not just HTML. Tomcat should not wrap jsps in extraneous tags. I put the snippet you suggested in a jsp, minus the taglib which I don't have set up, and got back exactly the HTML that I put in.
Can you tell us more about your environment? Are you using tomcat? Are you using some kind of framework?
Servlet 是 HTML 工厂。他们希望将有效的 HTML 页面发送到浏览器进行渲染。在不破坏整个模型的情况下,你无法“摆脱它”。
您发送“在其他各种地方使用”的片段的最初概念是有缺陷的。您听起来想设置一些可能在其他地方使用的数据 - 这是有效的 - 但我不明白将其包装在标记中有何重要性。
只有 JSP 应该使用标记的数据。 JSP 都是关于显示的。我会重新考虑你在做什么,并攻击你想要如何共享数据,而不是标记。
Servlets are HTML factories. They expect to send a valid HTML page down to a browser to be rendered. You can't "get rid of it" without breaking the whole model.
Your original concept of sending a snippet that's "used in a variety of other places" is flawed. You sound like to want to set some data that might be used in other places - that's valid - but I don't see how wrapping it in markup matters.
Only the JSP should be using the marked up data. JSPs are all about display. I'd rethink what you're doing and attack how you want to share the data, not the markup.
一种可能有效的方法,
根据您需要的有效 HTML 创建 HTML 文件,
并使用servlet返回响应,servlet应该读取HMTL文件并返回
它的内容为字符串,就像来自 servlet 的 XML 响应
希望这有帮助
One approach it might work,
Create HTML files as you required valid HTML,
and use servlet to returns response, servlet should read HMTL File and return
its contents as String, like XML respones from servlet
hopes thats helps