Question

I'm trying to reduce some of the configuration of our web application and have noticed that our views.properties file has a lot of redundant values that can be calculated programmatically.

For example:

welcome.(class)=org.springframework.web.servlet.view.tiles2.TilesView
welcome.url=welcome

home.(class)=org.springframework.web.servlet.view.tiles2.TilesView
home.url=home

login.(class)=org.springframework.web.servlet.view.tiles2.TilesView
login.url=login

Is there any way to do this entirely without the views.properties file (I'd rather a more opinionated "file names must match view names" than the error-prone "updated these x config files")

Thanks for your time!

Was it helpful?

Solution

So I have been using a hack to get rid of the lame views.properties file for about 6 years now. Here is my amazing solution for all of you to laugh at/use:

Define your viewResolver this way instead

<bean id="viewResolver" class="com.yourcompany.util.TilesResourceBundleViewResolver"/>

Here is the implementation of the resolver that looks directly at your tiles views

package com.yourcompany.util;

import java.util.Locale;
import java.util.MissingResourceException;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.tiles.DefinitionsFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContextException;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.tiles.TilesView;
import org.apache.struts.tiles.DefaultTilesUtilImpl;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.servlet.view.AbstractCachingViewResolver;

/**
 * Dave's improved view resolver because he hates having too many files to
 * update every time he creates a new view. Spring already makes it bad enough!
 *
 * @author dave
 */
public class TilesResourceBundleViewResolver extends AbstractCachingViewResolver
{
    private final String CONTENT_TYPE = "text/html;charset=utf-8";

    /**
     * for some incomprehensible reason the tiles stuff requires an http request
     * in order to see if a view exists.
     */
    private static final HttpServletRequest FAKE_HTTP_REQUEST =
        new MockHttpServletRequest();

    @Override
    protected View loadView(String viewName, Locale locale) throws
        MissingResourceException, BeansException, Exception
    {
        // check if the view is defined
        // in the site-views.xml and let tiles worry about it
        DefinitionsFactory definitionsFactory =
            (DefinitionsFactory) getServletContext().getAttribute(
                DefaultTilesUtilImpl.DEFINITIONS_FACTORY);
        if (definitionsFactory == null)
        {
            throw new ApplicationContextException(
                "Tiles definitions factory not found: TilesConfigurer not defined?");
        }
        if (definitionsFactory.getDefinition(viewName,
            FAKE_HTTP_REQUEST, getServletContext()) == null)
        {
            //log.warn("Request for unknown view: " + viewName);
            viewName = "PageNotFound";
        }

        TilesView t = new TilesView();
        t.setBeanName(viewName);
        t.setUrl(viewName);
        t.setApplicationContext(getApplicationContext());
        t.setContentType(CONTENT_TYPE);
        return t;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top