本文整理匯總了Java中org.apache.velocity.app.VelocityEngine.setApplicationAttribute方法的典型用法代碼示例。如果您正苦於以下問題:Java VelocityEngine.setApplicationAttribute方法的具體用法?Java VelocityEngine.setApplicationAttribute怎麽用?Java VelocityEngine.setApplicationAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.velocity.app.VelocityEngine
的用法示例。
在下文中一共展示了VelocityEngine.setApplicationAttribute方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: postProcessVelocityEngine
import org.apache.velocity.app.VelocityEngine; //導入方法依賴的package包/類
/**
* Provides a ClasspathResourceLoader in addition to any default or user-defined
* loader in order to load the spring Velocity macros from the class path.
* @see org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
*/
@Override
protected void postProcessVelocityEngine(VelocityEngine velocityEngine) {
velocityEngine.setApplicationAttribute(ServletContext.class.getName(), this.servletContext);
velocityEngine.setProperty(
SPRING_MACRO_RESOURCE_LOADER_CLASS, ClasspathResourceLoader.class.getName());
velocityEngine.addProperty(
VelocityEngine.RESOURCE_LOADER, SPRING_MACRO_RESOURCE_LOADER_NAME);
velocityEngine.addProperty(
VelocityEngine.VM_LIBRARY, SPRING_MACRO_LIBRARY);
if (logger.isInfoEnabled()) {
logger.info("ClasspathResourceLoader with name '" + SPRING_MACRO_RESOURCE_LOADER_NAME +
"' added to configured VelocityEngine");
}
}
示例2: initSpringResourceLoader
import org.apache.velocity.app.VelocityEngine; //導入方法依賴的package包/類
/**
* Initialize a SpringResourceLoader for the given VelocityEngine.
* <p>Called by {@code initVelocityResourceLoader}.
* @param velocityEngine the VelocityEngine to configure
* @param resourceLoaderPath the path to load Velocity resources from
* @see SpringResourceLoader
* @see #initVelocityResourceLoader
*/
protected void initSpringResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) {
velocityEngine.setProperty(
RuntimeConstants.RESOURCE_LOADER, SpringResourceLoader.NAME);
velocityEngine.setProperty(
SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
velocityEngine.setProperty(
SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
velocityEngine.setApplicationAttribute(
SpringResourceLoader.SPRING_RESOURCE_LOADER, getResourceLoader());
velocityEngine.setApplicationAttribute(
SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceLoaderPath);
}
示例3: initSpringResourceLoader
import org.apache.velocity.app.VelocityEngine; //導入方法依賴的package包/類
/**
* Initialize a SpringResourceLoader for the given VelocityEngine.
* <p>Called by {@code initVelocityResourceLoader}.
*
* @param velocityEngine the VelocityEngine to configure
* @param resourceLoaderPath the path to load Velocity resources from
* @see SpringResourceLoader
* @see #initVelocityResourceLoader
*/
protected void initSpringResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) {
velocityEngine.setProperty(
RuntimeConstants.RESOURCE_LOADER, SpringResourceLoader.NAME);
velocityEngine.setProperty(
SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
velocityEngine.setProperty(
SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
velocityEngine.setApplicationAttribute(
SpringResourceLoader.SPRING_RESOURCE_LOADER, getResourceLoader());
velocityEngine.setApplicationAttribute(
SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceLoaderPath);
}
示例4: initSpringResourceLoader
import org.apache.velocity.app.VelocityEngine; //導入方法依賴的package包/類
protected void initSpringResourceLoader(VelocityEngine velocityEngine, String resourceLoaderPath) {
velocityEngine.setProperty(
RuntimeConstants.RESOURCE_LOADER, SpringResourceLoader.NAME);
velocityEngine.setProperty(
SpringResourceLoader.SPRING_RESOURCE_LOADER_CLASS, SpringResourceLoader.class.getName());
velocityEngine.setProperty(
SpringResourceLoader.SPRING_RESOURCE_LOADER_CACHE, "true");
velocityEngine.setApplicationAttribute(
SpringResourceLoader.SPRING_RESOURCE_LOADER, getResourceLoader());
velocityEngine.setApplicationAttribute(
SpringResourceLoader.SPRING_RESOURCE_LOADER_PATH, resourceLoaderPath);
}
示例5: getVelocityEngine
import org.apache.velocity.app.VelocityEngine; //導入方法依賴的package包/類
@Produces
@ViewEngineConfig
public VelocityEngine getVelocityEngine() {
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("resource.loader", "webapp");
velocityEngine.setProperty("webapp.resource.loader.class", WebappResourceLoader.class.getCanonicalName());
velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", servletContext);
velocityEngine.init();
return velocityEngine;
}
示例6: main
import org.apache.velocity.app.VelocityEngine; //導入方法依賴的package包/類
public static void main(String[] args) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
ve.init();
String path = ve.getProperty(Velocity.FILE_RESOURCE_LOADER_PATH) + File.separator;
Object path1 = ve.getProperty(Velocity.RESOURCE_LOADER) + File.separator;
ve.setApplicationAttribute("app_chen", "app--------------------------");
Template t = ve.getTemplate("org/apache/velocity/demo/hellovelocity.vm");
//Template t = ve.getTemplate("D:/hellovelocity.vm");
VelocityContext ctx = new VelocityContext();
ctx.put("name", "velocity");
ctx.put("date1", (new Date()).toString());
ctx.put("dateO", new Date());
ctx.put("staticUser", User.class);
ctx.put("orderBusinessType", OrderBusinessType.class);
ctx.put("orderBusinessTypeOne", OrderBusinessType.RDPS);
User user = User.getUser();
ctx.put("user",user);
List temp = new ArrayList();
temp.add("1");
temp.add("2");
temp.add("322");
ctx.put("list", temp);
StringWriter sw = new StringWriter();
t.merge(ctx, sw);
System.out.println(sw.toString());
}
示例7: newVelocityEngine
import org.apache.velocity.app.VelocityEngine; //導入方法依賴的package包/類
/**
* <p>
* Instantiates a new VelocityEngine.
* </p>
* <p>
* The following is the default Velocity configuration
* </p>
*
* <pre>
* resource.loader = file, class
* file.resource.loader.path = real path of webapp
* class.resource.loader.description = Velocity Classpath Resource Loader
* class.resource.loader.class = org.apache.struts2.views.velocity.StrutsResourceLoader
* </pre>
* <p>
* this default configuration can be overridden by specifying a struts.velocity.configfile property in the
* struts.properties file. the specified config file will be searched for in the following order:
* </p>
*
* <ul>
* <li>relative to the servlet context path</li>
* <li>relative to the WEB-INF directory</li>
* <li>on the classpath</li>
* </ul>
*
* @param context the current ServletContext. may <b>not</b> be null
*
* @return the new velocity engine
*/
protected VelocityEngine newVelocityEngine(ServletContext context) {
if (context == null) {
String gripe = "Error attempting to create a new VelocityEngine from a null ServletContext!";
LOG.error(gripe);
throw new IllegalArgumentException(gripe);
}
Properties p = loadConfiguration(context);
VelocityEngine velocityEngine = new VelocityEngine();
// Set the velocity attribute for the servlet context
// if this is not set the webapp loader WILL NOT WORK
velocityEngine.setApplicationAttribute(ServletContext.class.getName(),
context);
try {
velocityEngine.init(p);
} catch (Exception e) {
throw new StrutsException("Unable to instantiate VelocityEngine!", e);
}
return velocityEngine;
}
示例8: init
import org.apache.velocity.app.VelocityEngine; //導入方法依賴的package包/類
/**
* Initialise the velocity and hibernate engine.
* @see HttpServlet#init()
*/
@Override
public void init() {
log.info("Initialising servlet");
// Create the velocity properties.
Properties props = new Properties();
props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogChute");
props.setProperty("resource.loader", "webapp");
props.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader");
props.setProperty("webapp.resource.loader.path", "/WEB-INF/templates/");
// IMPORTANT: Make sure to add the servlet context before calling
// init().
velocityEngine = new VelocityEngine(props);
velocityEngine.setApplicationAttribute("javax.servlet.ServletContext", this.getServletContext());
// Initialise the engine.
velocityEngine.init();
// Attempt to initialise hibernate.
// Doing this in servlet initialisation means the first request doesn't have to perform this action.
HibernateUtil.getSessionFactory().openSession().close();
// Fetch the pepper from the servlet.
// Load up any context parameters and put them into the configuration object.
TiramisuConfiguration.pepper = getServletContext().getInitParameter("pepper");
// Add the routes to the application.
// These are controlled by the
try {
// User Index Method
routeList.add(new Route(Pattern.compile("^.*\\/users[/]?$"), Arrays.asList("GET"), UserController.class, UserController.class.getDeclaredMethod("index")));
// User Read Method
routeList.add(new Route(Pattern.compile("^.*/users/([0-9]{1,})[/]?$"), Arrays.asList("GET"), UserController.class, UserController.class.getDeclaredMethod("read", String.class)));
// User Register Method
routeList.add(new Route(Pattern.compile("^.*/users/register[/]?$"), Arrays.asList("GET", "POST"), UserController.class, UserController.class.getDeclaredMethod("register")));
// User Login Method
routeList.add(new Route(Pattern.compile("^.*/login[/]?$"), Arrays.asList("GET", "POST"), UserController.class, UserController.class.getDeclaredMethod("login")));
// User Logout Method.
routeList.add(new Route(Pattern.compile("^.*/logout[/]?$"), Arrays.asList("GET"), UserController.class, UserController.class.getDeclaredMethod("logout")));
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
}