📄 velocityviewservlet.java
字号:
{
if (ve == null)
{
throw new NullPointerException("Cannot set the VelocityEngine to null");
}
this.velocity = ve;
}
/**
* Initializes the ServletToolboxManager for this servlet's
* toolbox (if any).
*
* @param config servlet configuation
*/
protected void initToolbox(ServletConfig config) throws ServletException
{
/* check the servlet config and context for a toolbox param */
String file = findInitParameter(config, TOOLBOX_KEY);
if (file == null)
{
// ok, look in the default location
file = DEFAULT_TOOLBOX_PATH;
velocity.debug("VelocityViewServlet: No toolbox entry in configuration."
+ " Looking for '" + DEFAULT_TOOLBOX_PATH + "'");
}
/* try to get a manager for this toolbox file */
toolboxManager =
ServletToolboxManager.getInstance(getServletContext(), file);
}
/**
* Initializes the Velocity runtime, first calling
* loadConfiguration(ServletConfig) to get a
* org.apache.commons.collections.ExtendedProperties
* of configuration information
* and then calling velocityEngine.init(). Override this
* to do anything to the environment before the
* initialization of the singleton takes place, or to
* initialize the singleton in other ways.
*
* @param config servlet configuration parameters
*/
protected void initVelocity(ServletConfig config) throws ServletException
{
velocity = new VelocityEngine();
setVelocityEngine(velocity);
// register this engine to be the default handler of log messages
// if the user points commons-logging to the LogSystemCommonsLog
LogSystemCommonsLog.setVelocityEngine(velocity);
velocity.setApplicationAttribute(SERVLET_CONTEXT_KEY, getServletContext());
// Try reading the VelocityTools default configuration
try
{
ExtendedProperties defaultProperties = loadDefaultProperties();
velocity.setExtendedProperties(defaultProperties);
}
catch(Exception e)
{
log("VelocityViewServlet: Unable to read Velocity Servlet configuration file: ", e);
// This is a fatal error...
throw new ServletException(e);
}
// Try reading an overriding user Velocity configuration
try
{
ExtendedProperties p = loadConfiguration(config);
velocity.setExtendedProperties(p);
}
catch(Exception e)
{
log("VelocityViewServlet: Unable to read Velocity configuration file: ", e);
log("VelocityViewServlet: Using default Velocity configuration.");
}
// now all is ready - init Velocity
try
{
velocity.init();
}
catch(Exception e)
{
log("VelocityViewServlet: PANIC! unable to init()", e);
throw new ServletException(e);
}
}
private ExtendedProperties loadDefaultProperties()
{
InputStream inputStream = null;
ExtendedProperties defaultProperties = new ExtendedProperties();
try
{
inputStream = getClass()
.getResourceAsStream(DEFAULT_TOOLS_PROPERTIES);
if (inputStream != null)
{
defaultProperties.load(inputStream);
}
}
catch (IOException ioe)
{
log("Cannot load default extendedProperties!", ioe);
}
finally
{
try
{
if (inputStream != null)
{
inputStream.close();
}
}
catch (IOException ioe)
{
log("Cannot close default extendedProperties!", ioe);
}
}
return defaultProperties;
}
/**
* Loads the configuration information and returns that
* information as an ExtendedProperties, which will be used to
* initialize the Velocity runtime.
* <br><br>
* Currently, this method gets the initialization parameter
* VelocityServlet.INIT_PROPS_KEY, which should be a file containing
* the configuration information.
* <br><br>
* To configure your Servlet Spec 2.2 compliant servlet runner to pass
* this to you, put the following in your WEB-INF/web.xml file
* <br>
* <pre>
* <servlet>
* <servlet-name> YourServlet </servlet-name>
* <servlet-class> your.package.YourServlet </servlet-class>
* <init-param>
* <param-name> org.apache.velocity.properties </param-name>
* <param-value> velocity.properties </param-value>
* </init-param>
* </servlet>
* </pre>
*
* Alternately, if you wish to configure an entire context in this
* fashion, you may use the following:
* <br>
* <pre>
* <context-param>
* <param-name> org.apache.velocity.properties </param-name>
* <param-value> velocity.properties </param-value>
* <description> Path to Velocity configuration </description>
* </context-param>
* </pre>
*
* Derived classes may do the same, or take advantage of this code to do the loading for them via :
* <pre>
* ExtendedProperties p = super.loadConfiguration(config);
* </pre>
* and then add or modify the configuration values from the file.
* <br>
*
* @param config ServletConfig passed to the servlets init() function
* Can be used to access the real path via ServletContext (hint)
* @return ExtendedProperties loaded with configuration values to be used
* to initialize the Velocity runtime.
* @throws IOException I/O problem accessing the specified file, if specified.
*/
protected ExtendedProperties loadConfiguration(ServletConfig config)
throws IOException
{
// grab the path to the custom props file (if any)
String propsFile = findInitParameter(config, INIT_PROPS_KEY);
if (propsFile == null)
{
// ok, look in the default location for custom props
propsFile = DEFAULT_PROPERTIES_PATH;
velocity.debug("VelocityViewServlet: Looking for custom properties at '"
+ DEFAULT_PROPERTIES_PATH + "'");
}
ExtendedProperties p = new ExtendedProperties();
InputStream is = getServletContext().getResourceAsStream(propsFile);
if (is != null)
{
// load the properties from the input stream
p.load(is);
velocity.info("VelocityViewServlet: Using custom properties at '"
+ propsFile + "'");
}
else
{
velocity.debug("VelocityViewServlet: No custom properties found. " +
"Using default Velocity configuration.");
}
return p;
}
/**
* Handles GET - calls doRequest()
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doRequest(request, response);
}
/**
* Handle a POST request - calls doRequest()
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doRequest(request, response);
}
/**
* Handles with both GET and POST requests
*
* @param request HttpServletRequest object containing client request
* @param response HttpServletResponse object for the response
*/
protected void doRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
Context context = null;
try
{
// first, get a context
context = createContext(request, response);
// set the content type
setContentType(request, response);
// get the template
Template template = handleRequest(request, response, context);
// bail if we can't find the template
if (template == null)
{
velocity.warn("VelocityViewServlet: couldn't find template to match request.");
return;
}
// merge the template and context
mergeTemplate(template, context, response);
}
catch (Exception e)
{
// log the exception
velocity.error("VelocityViewServlet: Exception processing the template: "+e);
// call the error handler to let the derived class
// do something useful with this failure.
error(request, response, e);
}
finally
{
// call cleanup routine to let a derived class do some cleanup
requestCleanup(request, response, context);
}
}
/**
* Cleanup routine called at the end of the request processing sequence
* allows a derived class to do resource cleanup or other end of
* process cycle tasks. This default implementation does nothing.
*
* @param request servlet request from client
* @param response servlet reponse
* @param context Context created by the {@link #createContext}
*/
protected void requestCleanup(HttpServletRequest request,
HttpServletResponse response,
Context context)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -