使用httl模板并获取渲染后页面数据流

public String getRenderedStr(final HashMap map,String temPath){

        String result;

        try{

            Engine engine=Engine.getEngine();

            final Template template=engine.getTemplate(temPath);

            final PipedOutputStream pos=new PipedOutputStream();

            PipedInputStream pis=new PipedInputStream(pos);

            new Thread(new Runnable(){

                @Override

                public void run() {

                    try {

                        template.render(map,pos);

                        pos.close();

                    } catch (IOException | ParseException e) {

                        e.printStackTrace();

                    }

                }

            }).start();

            StringWriter writer=new StringWriter();

            IOUtils.copy(pis, writer, "utf-8");

            result=writer.toString();

        }catch (Exception e){

            result="error";

            e.printStackTrace();

        }

        return result;

    }

猜你喜欢

转载自free-man135.iteye.com/blog/2082308
今日推荐