JSP以编程方式呈现 [英] JSP programmatically render

查看:138
本文介绍了JSP以编程方式呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式呈现JSP页面。据我所知,JSP应该有一些编译器。问题是我可以在没有JspServlet和其他人的情况下直接使用这个编译器吗?我需要的只是文档如何使用JSP编译器(例如,Jasper)。

I need to programmatically render JSP page. As far as I understand, JSP should have some compiler. The question is can I use this compiler dirrectly without JspServlet and others? All I need is documentation how to use JSP compiler (Jasper, for example).

我认为一些额外的信息可以澄清情况。我不能使用标准的JspServlet。我想以某种方式在编译之前更改源JSP(准确地将两个JSP合并在一起),所以我需要一种方法直接使用JSP编译器从InputStream(或Reader)编译JSP结果。

Some additional information would clarify situation, I think. I can not use standard JspServlet. I want to change source JSP before compilation in some way (merge two JSP together to be precise), so I need a way to compile JSP result from InputStream (or Reader) using the JSP compiler directly.

两个JSP的合并是布局要求。你可以问:但为什么这个人不使用SiteMesh或类似的东西?。其中一个JSP页面不是静态的。它由用户提供并存储在数据库中。我们清理和验证这个JSP布局(用户只能使用标签的子集,并且所有这些标签都不是标准的,而是专门为它们创建的),缓存它们等等。但现在我们需要一种方法来使用这些JSP页面(存储在内存中)作为用户请求的所有JSP页面的布局。

Merging of two JSP is layout requirement. You can ask: "But why this guy just doesn't use SiteMesh or something like this?". One of JSP pages is not static. It's provided by user and stored in database. We sanitify and validates this JSP layout (users are able to use only subset of tags, and all of them are not standart but created specially for them), cache them and so on. But now we need a way to use these JSP pages (which are stored in memory) as layouts for all JSP pages that user request.

推荐答案


我需要programmaticaly渲染JSP页面。

什么是功能毕竟要求?您显然正在寻找错误方向的解决方案。是什么,您认为这是解决方案的问题/要求是什么?我们可能会提出更好的建议。

What's the functional requirement after all? You're clearly looking for a solution in the wrong direction. What is it, the problem/requirement for which you think that this is the solution? We may come up with better suggestions.

例如,您是否只需要输出?如果是这样,那么 java.net.URLConnection 就足够了。

Do you for example need just its output? If so, then java.net.URLConnection may suffice.

编辑:您修改了自己的问题:

Edit: you edited your question:


我想以某种方式在编译之前更改源JSP(准确地将两个JSP合并在一起),所以我需要一种方法直接使用JSP compiller从InputStream(或Reader)编译JSP结果。

好的,这是更清晰。但你需要什么呢?这些JSP实际代表什么?应该用于的最终结果是什么?

OK, that's a bit more clear. But what for do you need this? What does those JSPs actually represent? What is the final result supposed to be used for?

例如,您是否只想在另一个中包含一个JSP?例如。在 main.jsp 中包含 head.jsp ?如果是这样,那么< jsp:include> 就足够了。或者更糟糕的是,它们是否包含原始Java代码以及您想要重用的某些特定代码?如果是这样,那么你应该使用普通的Java类和必要的taglibs。

Do you for example just want to include the one JSP in the other? E.g. including a head.jsp in a main.jsp? If so, then <jsp:include> may suffice. Or is it more worse, do they contain raw Java code with some specific code which you wanted to reuse? If so, then you should use plain Java classes and if necessary taglibs for this.

编辑2 :正如你评论的那样:

Edit 2: as you commented:


但现在我们需要一种方法来使用这些JSP页面(顺便说一下,存储在内存中)作为所有JSP页面的布局用户请求

只需将JSP文件存储在webapp的webcontent内的磁盘文件系统中( ServletContext#getRealPath()可能会在这里进行救援)并将请求转发到您自己的主JSP文件,其中包含两个JSP文件,例如:

Just store the JSP files on the disk file system inside the webapp's webcontent (the ServletContext#getRealPath() may come into rescue here) and forward the request to your own main JSP file which includes the two JSP files using for example:

<jsp:include page="${page1}" />
<jsp:include page="${page2}" />

编辑3 :我创建了 SSCCE 证明其有效。

Edit 3: I created an SSCCE to prove its working.

package mypackage;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        File root = new File(getServletContext().getRealPath("/"));

        String main = "<jsp:include page=\"${page1}\" /><jsp:include page=\"${page2}\" />";
        write(main, new File(root, "main.jsp"));

        String page1 = "<p>We are in ${data1}";
        write(page1, new File(root, "page1.jsp"));
        request.setAttribute("page1", "page1.jsp");
        request.setAttribute("data1", "first jsp");

        String page2 = "<p>We are in ${data2}";
        write(page2, new File(root, "page2.jsp"));
        request.setAttribute("page2", "page2.jsp");
        request.setAttribute("data2", "second jsp");

        request.getRequestDispatcher("main.jsp").forward(request, response);
    }

    private static void write(String content, File file) throws IOException {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            writer.write(content);
        } finally {
            if (writer != null) try { writer.close(); } catch (IOException ignore) {}
        }
    }

}

http:// localhost:8080 / playground / test (或其他)执行你正在使用的主机/上下文名称,你会看到

Execute it at http://localhost:8080/playground/test (or whatever host/contextname you're using) and you'll see

We are in first jsp
We are in second jsp

为了提高效率,我会缓存每个资源并使用 File#exists()检查特定页面是否已保存在磁盘上。

To make it more efficient I would cache every resource and make use of File#exists() to check if the particular page is already saved on disk.

这篇关于JSP以编程方式呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆