简体   繁体   English

读取JSP文件并生成HTML-而不是通过Web服务器

[英]Read a JSP file and produce HTML - not with a web server

If I wanted to read a jsp like: 如果我想阅读类似的jsp:

File f = new File("my.jsp");

and then I want it to produce the expected HTML as say a String (byte[], OutputStream, whatever), How would I do that? 然后我希望它生成预期的HTML,例如说一个字符串(byte [],OutputStream等),我该怎么做?

I assume there is some transformer or something. 我认为这里有一些变压器。 Can anyone point me in the right direction? 谁能指出我正确的方向?

A JSP File is translated and compiled as a Servlet class, so it not directly generate a html File, the reason is a JSP as a Servlet could generate dynamic html depending of the parameters in the request, in the tomcat server it use the Jasper 2 JSP Engine to implement the JavaServer Pages 2.1 specification that process the JSP, so you can check it but I don't recommend you do that. JSP文件被翻译并编译为Servlet类,因此它不会直接生成html文件,原因是JSP作为Servlet可以根据请求中的参数生成动态html,在tomcat服务器中它使用Jasper 2 JSP Engine实现用于处理JSP的JavaServer Pages 2.1规范,因此您可以检查它,但我不建议您这样做。

Maybe what your really want is a template mechanism, for example you have a text file similar to the jsp with similar sintax to the EL that can access some objects, so you can use the template engine and bound your object with the text and produce your html. 也许您真正想要的是模板机制,例如,您有一个类似于jsp的文本文件,并且具有与EL相似的正弦波,可以访问某些对象,因此您可以使用模板引擎并将对象与文本绑定并生成您的对象。 HTML。 If it works for you, you can use Apache Velocity - HTML example or FreeMaker - HTML example . 如果适合您,则可以使用Apache Velocity - HTML示例FreeMaker - HTML example

Jasper is the JSP compiler used by Tomcat, it takes the JSP pages themselves, parses them, and generates Java code (in the form of a servlet, or at least it used to be) which writes the JSP's output to the servlet response. Jasper是Tomcat使用的JSP编译器,它自己获取JSP页面,对其进行解析,并生成Java代码(以Servlet的形式,或者至少是以前的形式),该Java代码将JSP的输出写入Servlet响应。 That Java code would then be compiled and executed against the containers implementation of the Servlet API. 然后,该Java代码将针对Servlet API的容器实现进行编译和执行。

Jasper is a standalone library, so in theory you could instantiate its classes inside your own harness, point it at a JSP, generate the servlet code, then compile and execute it. Jasper是一个独立的库,因此从理论上讲,您可以在自己的工具中实例化其类,将其指向JSP,生成servlet代码,然后编译并执行它。 However you'd need to pass in your own mock implementations of the Servlet API interfaces, as it's these that will be called to determine request parameters (HttpServletRequest), and to write the response (HttpServletResponse). 但是,您需要传递自己的Servlet API接口的模拟实现,因为将通过调用这些接口来确定请求参数(HttpServletRequest)和编写响应(HttpServletResponse)。 You'd need to provide a version of HttpServletResponse that will buffer up the output for you to grab later. 您需要提供HttpServletResponse的版本,该版本将缓冲输出,以备以后使用。

From the Tomcat 6 API , this bits your want are in the org.apache.jasper package. Tomcat 6 API中 ,您需要的这些位在org.apache.jasper包中。

Taking a look at at it you'd probably want to create a JspCompilationContext which points to your JSP, call createCompiler() , and take it from there. 看一下它,您可能想要创建一个指向JSP的JspCompilationContext ,调用createCompiler() ,然后从那里获取它。

As far as mocking out the Servlet API goes, you might get what you need from the stub implemetations provided by the Spring Framework for testing purposes 就模拟Servlet API而言,您可能会从Spring框架提供的用于测试目的的存根实现中获得所需的东西

Velocity and FreeMarker are both excellent for this, and much easier for basic templating. Velocity和FreeMarker在这方面都非常出色,并且对于基本模板制作而言则更加容易。

However, it can be done. 但是,可以做到。

I have an example explained here: Tomcat JSP/JSTL without HTTP 我在这里解释了一个示例: 不带HTTP的Tomcat JSP / JSTL

It's obviously easier from within a web app (as all of the jars will be there), but it can be done outside, you'll just need to identify and the appropriate jars to your program. 从Web应用程序中显然很容易(因为所有jar都在其中),但是可以在外部完成,您只需要为程序标识和合适的jar即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM