如何解析 JSP 页面并获取字符串形式的结果?

发布于 2024-12-11 01:15:52 字数 699 浏览 0 评论 0原文

我有一个 FrontController,它在自制的 REST 框架中返回表示。我有一些 XmlRepresentation、JsonRepresentation,现在我想要一个 JspRepresentation。 UserJspRepresentation 可以是代表用户的页面或片段。 Jsp 将是一个模板,

我可以使用它来做一些转发/包含的事情,但我想要一些更独立的东西,并将结果作为对象获取。 forward() 方法返回 void。

类似于:

HttpServletRequest request = getHttpServletRequest();
User user = getUser();
request.setAttribute("user", user); // The Jsp will be aware of the user
JspRepresentation representation = new JspRepresentation(request, "userPage.jsp");
String result = representation.toString();// this is hard to get the displayed page !!!

问题是:如何获取 Jsp 页面作为 String 对象?

现在我只能考虑使用java客户端,它不是轻量级的...我也查看了Jasper API,但没有发现任何明确的信息。

I have a FrontController that returns Representation in a homemade REST framework. I have some XmlRepresentation, JsonRepresentation and I would like now a JspRepresentation.
A UserJspRepresentation could be for exemple a page or a fragment that represents the User. A Jsp would then be a Template that may use

I could do some forward/include stuff, but I would like something more isolated, and get the result as an object. The forward() method returns void.

Something like :

HttpServletRequest request = getHttpServletRequest();
User user = getUser();
request.setAttribute("user", user); // The Jsp will be aware of the user
JspRepresentation representation = new JspRepresentation(request, "userPage.jsp");
String result = representation.toString();// this is hard to get the displayed page !!!

The question is : How to get the Jsp Page as a String object?

For now I can only consider using a java client, which is not lightweight... I have also looked in the Jasper API, but found nothing clear.

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

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

发布评论

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

评论(1

我要还你自由 2024-12-18 01:15:52

你不能。 JSP 不是模板引擎。这只是一种视图技术。您正在寻找像 VelocityFreemarkerSitemesh 等。

尽你所能使用 JSP 的方法是发送完全值得您自己对其具体 URL 进行 HTTP 请求。

InputStream input = new URL("http://localhost:8080/context/userPage.jsp").openStream();
// ...

您不能将请求属性传递给它。不过,您可以将其放入会话中并让 JSP 从那里检索它。您只需发送 JSESSIONID cookie:

URLConnection connection = new URL("http://localhost:8080/context/userPage.jsp").openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + session.getId());
InputStream input = connection.getInputStream();
// ...

或者,您也可以“以通常的方式”将请求转发到 JSP,而不是将其 HTML 输出作为 String 和自己将其写入响应中。这样,JSP 将完成根据当前请求生成 HTML 并将其发送到响应的工作,而无需自己收集该 HTML 并写入响应。

request.getRequestDispatcher("/WEB-INF/userPage.jsp").forward(request, response);

You can't. JSP is not a template engine. It's just a view technology. You're looking for a template engine like Velocity, Freemarker, Sitemesh, etc.

Best what you can do with JSPs is to send a fullworthy HTTP request to its concrete URL yourself.

InputStream input = new URL("http://localhost:8080/context/userPage.jsp").openStream();
// ...

You only can't pass request attributes to it. You can however put it in the session and let the JSP retrieve it from there. You only need to send the JSESSIONID cookie along:

URLConnection connection = new URL("http://localhost:8080/context/userPage.jsp").openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + session.getId());
InputStream input = connection.getInputStream();
// ...

Alternatively, you can also just forward the request to the JSP "the usual way" instead of getting its HTML output as String and writing it to the response yourself. This way the JSP will do its job of generating the HTML based on the current request and sending it to the response without the need to collect that HTML and write to the response yourself.

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