在webwork中做生成静态页面的问题
根据网上一篇servlet生成静态页面的文章,将相关程序由Servlet 转移到Webwork的action中:
protected String execute() throws Exception {
ServletContext sc = ServletActionContext.getServletContext();
String url = "/TestTag.jsp";
String name = "d:/test1.html";
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
RequestDispatcher rd = sc.getRequestDispatcher(url);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final ServletOutputStream stream = new ServletOutputStream() {
public void write(byte[] data, int offset, int length) {
os.write(data, offset, length);
}
public void write(int b) throws IOException {
os.write(b);
}
};
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response) {
public ServletOutputStream getOutputStream() {
return stream;
}
public PrintWriter getWriter() {
return pw;
}
};
rd.include(request, rep);
pw.flush();
FileOutputStream fos = new FileOutputStream(name);
os.writeTo(fos);
fos.close();
pw.close();
return this.SUCCESS;
}
可以正常生成页面,但webwork抛出如下异常:
[DEBUG] 2008-02-02 14:56:06 [http-8083-Processor25] [com.opensymphony.webwork.dispatcher.ServletDispatcherResult.doExecute:88] Forwarding to location Success.jsp
[ERROR] 2008-02-02 14:56:06 [http-8083-Processor25] [com.opensymphony.webwork.dispatcher.DispatcherUtils.serviceAction:237] Could not execute action
java.lang.NullPointerException
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:964)
at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:581)
at com.opensymphony.webwork.dispatcher.ServletDispatcherResult.doExecute(ServletDispatcherResult.java:94)
at com.opensymphony.webwork.dispatcher.WebWorkResultSupport.execute(WebWorkResultSupport.java:143)
at com.opensymphony.xwork.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:311)
at com.opensymphony.xwork.DefaultActionInvocation.invoke(DefaultActionInvocation.java:206)
..
请大家帮忙看看,谢谢