⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webworkutil.java

📁 webwork source
💻 JAVA
字号:
/* * WebWork, Web Application Framework * * Distributable under Apache license. * See terms of license at opensource.org */package webwork.view.velocity;import webwork.action.Action;import webwork.action.ActionContext;import webwork.action.factory.ActionFactory;import webwork.util.ClassLoaderUtils;import webwork.util.ConcurrentReaderHashMap;import javax.servlet.*;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.Map;import org.apache.commons.logging.*;import org.apache.velocity.app.Velocity;import org.apache.velocity.context.Context;import org.apache.velocity.exception.ResourceNotFoundException;import org.apache.velocity.exception.MethodInvocationException;import org.apache.velocity.exception.ParseErrorException;/** *	WebWork utility methods for Velocity templates * *	@author Rickard Öberg (rickard@dreambean.com) *	@version $Revision: 1.8 $ */public final class WebWorkUtil{   private static final Log log = LogFactory.getLog(WebWorkUtil.class);   private Context ctx;   Map classes = new ConcurrentReaderHashMap();    public WebWorkUtil(Context ctx)    {        this.ctx = ctx;    }   public Action action(Object aName)      throws Exception   {      return ActionFactory.getAction(aName.toString());   }   public String execute(Action anAction)      throws Exception   {      long start = System.currentTimeMillis();      try      {         return anAction.execute();      } finally      {         LogFactory.getLog(getClass()).debug("Execute:"+(System.currentTimeMillis()-start));      }   }   public Object bean(Object aName)      throws Exception   {      String name = aName.toString();      Class c = (Class)classes.get(name);      if (c == null)      {         c = ClassLoaderUtils.loadClass(name, WebWorkUtil.class);         classes.put(name, c);      }      return c.newInstance();   }   public String include(Object aName,ServletRequest aRequest, ServletResponse aResponse)      throws Exception   {      try      {         RequestDispatcher dispatcher = aRequest.getRequestDispatcher(aName.toString());         if(dispatcher==null)         {            throw new IllegalArgumentException("Cannot find included file " + aName);         }         ServletResponseHandler responseHandler = new ServletResponseHandler(aResponse);         Class[] interfaces = new Class[] {HttpServletResponse.class};         HttpServletResponse response = (HttpServletResponse)Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, responseHandler);         dispatcher.include(aRequest, response);         return responseHandler.getData();      } catch (Exception e)      {         e.printStackTrace();         throw e;      }   }   public long toLong(int anInt)   {      return (long)anInt;   }   public long toLong(String aLong)   {      return Long.parseLong(aLong);   }   public int toInt(long aLong)   {      return (int)aLong;   }   public String toString(long aLong)   {      return Long.toString(aLong);   }   public String toString(int anInt)   {      return Integer.toString(anInt);   }    public String evaluate(String expression) throws IOException, ResourceNotFoundException, MethodInvocationException, ParseErrorException    {        CharArrayWriter writer = new CharArrayWriter();        Velocity.evaluate(ctx, writer, "Error parsing " + expression,  expression);        return writer.toString();    }   static class ServletResponseHandler      implements InvocationHandler   {      ServletResponse response;      ByteArrayOutputStream bout;      PrintWriter writer;      ServletOutputStream sout;      ServletResponseHandler(ServletResponse aResponse)      {         response = aResponse;         bout = new ByteArrayOutputStream();         sout = new ServletOutputStreamWrapper(bout);         writer = new PrintWriter(new OutputStreamWriter(bout));      }      public Object invoke(Object proxy,                     Method method,                     Object[] args)              throws Throwable      {         if (method.getName().equals("getOutputStream"))         {            return getOutputStream();         } else if (method.getName().equals("getWriter"))         {            return writer;         } else         {            return method.invoke(response, args);         }      }      ServletOutputStream getOutputStream()      {         return sout;      }      public String getData()      {         writer.flush();         return bout.toString();      }   }   static class ServletOutputStreamWrapper      extends ServletOutputStream   {      ByteArrayOutputStream stream;      ServletOutputStreamWrapper(ByteArrayOutputStream aStream)      {         stream = aStream;      }      public void write(int aByte)      {         stream.write(aByte);      }   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -