Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  import org.slf4j.Logger;
 import java.util.List;
Created with IntelliJ IDEA. User: mati Date: 20/06/2013 Time: 19:51 An Soy implementation of Spring's TemplateViewResolver. Warning: please read carefully JavaDocs from AbstractTemplateViewResolver and it the classes from which it inherits as it may be necessary to set some configuration options for this resolver to work properly for your use case.
 public class SoyTemplateViewResolver extends AbstractCachingViewResolver implements Ordered {

Prefix for special view names that specify a redirect URL (usually to a controller after a form has been submitted and processed). Such view names will not be resolved in the configured default way but rather be treated as special shortcut.
 	public static final String REDIRECT_URL_PREFIX = "redirect:";

Prefix for special view names that specify a forward URL (usually to a controller after a form has been submitted and processed). Such view names will not be resolved in the configured default way but rather be treated as special shortcut.
 	public static final String FORWARD_URL_PREFIX = "forward:";
 	private static final Logger logger = LoggerFactory.getLogger(SoyTemplateViewResolver.class);
an encoding to use
a default view
 	private String indexView = "index";
 	private boolean redirectContextRelative = true;
 	private boolean redirectHttp10Compatible = true;
 	private int order = .;
 	protected View loadView(final String viewNamefinal Locale localethrows Exception {
 		Preconditions.checkNotNull(viewName"viewName cannot be null!");
 		Preconditions.checkNotNull("templateRenderer cannot be null!");
 		.debug("loadView:{}, locale:{}"viewNamelocale);
 		if (viewName.startsWith()) {
 			final String redirectUrl = viewName.substring(.length());
 			return applyLifecycleMethods(viewNameview);
 		if (viewName.startsWith()) {
 			final String forwardUrl = viewName.substring(.length());
 			return new InternalResourceView(forwardUrl);
		final String newViewName = orIndexView(normalize(viewName));
		final List<StringcontentTypes = .contentTypes();
		if (!canHandle(newViewName) || !.isSupportedContentTypes(contentTypes)) {
			.debug("Unable to handle view: {}."newViewName);
			return null;
		final SoyView view = new SoyView();
		return view;
	private boolean canHandle(final String templateName) {
		return templateName.startsWith();
	private String stripPrefix(final String templateName) {
		if (canHandle(templateName)) {
			return templateName.replace("");
		return templateName;

Map / to a default view name.

viewName The view name.
The defaulted view name.
	private String orIndexView(final String viewName) {
		return StringUtils.isEmpty(viewName) ?  : viewName;

Remove beginning and ending slashes, then replace all occurrences of / with .

viewName The Spring viewName
The name of the view, dot separated.
	private String normalize(final String viewName) {
		String normalized = viewName;
		if (normalized.startsWith("/")) {
			normalized = normalized.substring(0);
		if (normalized.endsWith("/")) {
			normalized = normalized.substring(0, normalized.length() - 1);
		return normalized.replaceAll("/"".");
	private View applyLifecycleMethods(String viewNameAbstractView view) {
	protected String resolveContentType(final List<StringcontentTypes) {
        // TODO: This should be better handled.		
		if (contentTypes.size() > 1) {
			.warn("More than one content type is not currently supported; "
"only first one ({}) will be used."contentTypes.get(0));
		return contentTypes.get(0) + "; charset=" + ;
	public void setTemplateRenderer(final TemplateRenderer templateRenderer) {
		this. = templateRenderer;
	public void setModelAdjuster(final ModelAdjuster modelAdjuster) {
		this. = modelAdjuster;
	public void setGlobalRuntimeModelResolver(final GlobalRuntimeModelResolver globalRuntimeModelResolver) {
		this. = globalRuntimeModelResolver;
	public void setLocaleProvider(final LocaleProvider localeProvider) {
		this. = localeProvider;
	public void setSoyMsgBundleResolver(final SoyMsgBundleResolver soyMsgBundleResolver) {
		this. = soyMsgBundleResolver;
	public void setIndexView(String indexView) {
		this. = indexView;
	public void setRedirectContextRelative(boolean redirectContextRelative) {
		this. = redirectContextRelative;
	public void setRedirectHttp10Compatible(boolean redirectHttp10Compatible) {
		this. = redirectHttp10Compatible;
	public boolean isRedirectContextRelative() {
	public boolean isRedirectHttp10Compatible() {
	public void setCompiledTemplatesHolder(CompiledTemplatesHolder compiledTemplatesHolder) {
		this. = compiledTemplatesHolder;
	public int getOrder() {
		return this.;
	public void setOrder(int order) {
		this. = order;
	public String getPrefix() {
		return ;
	public void setPrefix(String prefix) {
		this. = prefix;
	public String getEncoding() {
		return ;
	public void setEncoding(String encoding) {
		this. = encoding;
	public String getIndexView() {
		return ;
	public void setHotReloadMode(final boolean hotReloadMode) {
    public void setContentNegotiator(ContentNegotiator contentNegotiator) {
        this. = contentNegotiator;
New to GrepCode? Check out our FAQ X