mirror of
https://github.com/apache/struts.git
synced 2026-08-31 19:35:40 +00:00
Added ability to discover new tag libraries and template engines automatically
WW-1590 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@491656 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -141,10 +141,6 @@ public final class StrutsConstants {
|
||||
|
||||
public static final String STRUTS_ACTIONPROXYFACTORY = "struts.actionProxyFactory";
|
||||
|
||||
public static final String STRUTS_TEMPLATE_ENGINES = "struts.templateEngines";
|
||||
|
||||
public static final String STRUTS_TAG_LIBRARIES = "struts.tagLibraries";
|
||||
|
||||
public static final String STRUTS_FREEMARKER_WRAPPER_ALT_MAP = "struts.freemarker.wrapper.altMap";
|
||||
|
||||
/** The name of the xwork converter implementation */
|
||||
|
||||
+8
-12
@@ -20,11 +20,11 @@
|
||||
*/
|
||||
package org.apache.struts2.components.template;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
import org.apache.struts2.dispatcher.Dispatcher;
|
||||
import com.opensymphony.xwork2.config.ConfigurationException;
|
||||
import com.opensymphony.xwork2.inject.Container;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
@@ -51,17 +51,13 @@ public class TemplateEngineManager {
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
@Inject(StrutsConstants.STRUTS_TEMPLATE_ENGINES)
|
||||
public void setTemplateEngines(String engines) {
|
||||
if (engines != null) {
|
||||
TemplateEngine eng = null;
|
||||
String[] list = engines.split(",");
|
||||
for (String name : list) {
|
||||
templateEngines.put(name, new LazyEngineFactory(name));
|
||||
}
|
||||
Map<String,EngineFactory> map = new HashMap<String,EngineFactory>();
|
||||
Set<String> prefixes = container.getInstanceNames(TemplateEngine.class);
|
||||
for (String prefix : prefixes) {
|
||||
map.put(prefix, new LazyEngineFactory(prefix));
|
||||
}
|
||||
this.templateEngines = Collections.unmodifiableMap(map);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -125,9 +125,6 @@ public class FreemarkerManager {
|
||||
private String encoding;
|
||||
private boolean altMapWrapper;
|
||||
private Map<String,TagLibrary> tagLibraries;
|
||||
private String tagLibraryPrefixes;
|
||||
private Container container;
|
||||
|
||||
|
||||
@Inject(StrutsConstants.STRUTS_I18N_ENCODING)
|
||||
public void setEncoding(String encoding) {
|
||||
@@ -139,17 +136,6 @@ public class FreemarkerManager {
|
||||
altMapWrapper = "true".equals(val);
|
||||
}
|
||||
|
||||
@Inject(StrutsConstants.STRUTS_TAG_LIBRARIES)
|
||||
public void setTagLibraryPrefixes(String libnames) {
|
||||
this.tagLibraryPrefixes = libnames;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
/*
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
Map<String,TagLibrary> map = new HashMap<String,TagLibrary>();
|
||||
@@ -159,7 +145,6 @@ public class FreemarkerManager {
|
||||
}
|
||||
this.tagLibraries = Collections.unmodifiableMap(map);
|
||||
}
|
||||
*/
|
||||
|
||||
public final synchronized freemarker.template.Configuration getConfiguration(ServletContext servletContext) throws TemplateException {
|
||||
freemarker.template.Configuration config = (freemarker.template.Configuration) servletContext.getAttribute(CONFIG_SERVLET_CONTEXT_KEY);
|
||||
@@ -171,17 +156,6 @@ public class FreemarkerManager {
|
||||
servletContext.setAttribute(CONFIG_SERVLET_CONTEXT_KEY, config);
|
||||
}
|
||||
|
||||
if (tagLibraries == null && tagLibraryPrefixes != null) {
|
||||
Map<String,TagLibrary> map = new HashMap<String,TagLibrary>();
|
||||
List<TagLibrary> list = new ArrayList<TagLibrary>();
|
||||
TagLibrary lib = null;
|
||||
String[] prefixes = tagLibraryPrefixes.split(",");
|
||||
for (String prefix : prefixes) {
|
||||
map.put(prefix, container.getInstance(TagLibrary.class, prefix));
|
||||
}
|
||||
this.tagLibraries = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
config.setWhitespaceStripping(true);
|
||||
|
||||
return config;
|
||||
|
||||
@@ -98,12 +98,8 @@ public class VelocityManager {
|
||||
|
||||
private String customConfigFile;
|
||||
|
||||
private String tagLibraryPrefixes;
|
||||
|
||||
private List<TagLibrary> tagLibraries;
|
||||
|
||||
private Container container;
|
||||
|
||||
public VelocityManager() {
|
||||
}
|
||||
|
||||
@@ -112,14 +108,14 @@ public class VelocityManager {
|
||||
this.objectFactory = fac;
|
||||
}
|
||||
|
||||
@Inject(StrutsConstants.STRUTS_TAG_LIBRARIES)
|
||||
public void setTagLibraryPrefixes(String libnames) {
|
||||
this.tagLibraryPrefixes = libnames;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public void setContainer(Container container) {
|
||||
this.container = container;
|
||||
List<TagLibrary> list = new ArrayList<TagLibrary>();
|
||||
Set<String> prefixes = container.getInstanceNames(TagLibrary.class);
|
||||
for (String prefix : prefixes) {
|
||||
list.add(container.getInstance(TagLibrary.class, prefix));
|
||||
}
|
||||
this.tagLibraries = Collections.unmodifiableList(list);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -246,16 +242,6 @@ public class VelocityManager {
|
||||
if (velocityEngine == null) {
|
||||
velocityEngine = newVelocityEngine(context);
|
||||
}
|
||||
if (tagLibraries == null && tagLibraryPrefixes != null) {
|
||||
List<TagLibrary> list = new ArrayList<TagLibrary>();
|
||||
TagLibrary lib = null;
|
||||
String[] prefixes = tagLibraryPrefixes.split(",");
|
||||
for (String prefix : prefixes) {
|
||||
list.add(container.getInstance(TagLibrary.class, prefix));
|
||||
}
|
||||
this.tagLibraries = Collections.unmodifiableList(list);
|
||||
|
||||
}
|
||||
this.initToolbox(context);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
*/
|
||||
package org.apache.struts2.views;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.struts2.components.template.FreemarkerTemplateEngine;
|
||||
@@ -49,9 +51,13 @@ public class TemplateEngineManagerTest extends TestCase {
|
||||
mockContainer.matchAndReturn("getInstance", C.args(C.eq(TemplateEngine.class), C.eq("jsp")), new JspTemplateEngine());
|
||||
mockContainer.matchAndReturn("getInstance", C.args(C.eq(TemplateEngine.class), C.eq("vm")), new VelocityTemplateEngine());
|
||||
mockContainer.matchAndReturn("getInstance", C.args(C.eq(TemplateEngine.class), C.eq("ftl")), new FreemarkerTemplateEngine());
|
||||
mockContainer.matchAndReturn("getInstanceNames", C.args(C.eq(TemplateEngine.class)), new HashSet() {{
|
||||
add("jsp");
|
||||
add("vm");
|
||||
add("ftl");
|
||||
}});
|
||||
|
||||
mgr.setContainer((Container)mockContainer.proxy());
|
||||
mgr.setTemplateEngines("jsp,vm,ftl");
|
||||
mgr.setDefaultTemplateType("jsp");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user