我正在尝试减少Web应用程序的某些配置,并注意到我们的views.properties
文件有很多可以通过编程方式计算的冗余值。
例如:
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
有没有任何方法可以在没有views.properties文件的情况下完全执行此操作(我更倾向于认为“文件名必须与视图名称匹配”,而不是容易出错的“更新这些x配置文件”)
谢谢你的时间!
答案 0 :(得分:1)
所以我一直在用hack来摆脱蹩脚的views.properties文件大约6年了。这是我为所有人嘲笑/使用的惊人解决方案:
以这种方式定义viewResolver
<bean id="viewResolver" class="com.yourcompany.util.TilesResourceBundleViewResolver"/>
以下是直接查看切片视图的解析器的实现
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;
}
}