【问题标题】:Sitemesh spring:message not recognized in templateSitemesh spring:模板中无法识别消息
【发布时间】:2012-02-24 20:32:30
【问题描述】:

我们在一个带有 Sitemesh 2 的项目中使用 spring:message 标签。 在装饰器中使用 spring:message 时,无法识别 -tag。我们可以在我们的jsp页面中使用-tag,但在装饰器jsp文件中。

<?xml version="1.0" encoding="UTF-8"?>

<excludes/>

<page-parsers>
    <parser content-type="text/html" encoding="UTF-8" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
</page-parsers>

<decorator-mappers>
    <mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
        <param name="config" value="${decorators-file}" />
    </mapper>
</decorator-mappers>

如果我们使用已弃用的解析器 FastPageParser 比没有问题,但是当使用新的 HTMLPageParser 时则不起作用。

我们如何解决这个问题?

【问题讨论】:

    标签: java spring jsp decorator sitemesh


    【解决方案1】:
    <spring:message code="msg.x.x.x"  />
    

    在使用 FastPageParser 的装饰器上对我来说效果很好。

    有几件事要检查..

    • 你的装饰器中是否包含 springframework 和 sitemesh 标签库?

    • 我不确定它是否会对过滤器链产生影响,但我正在使用自定义配置装饰器映射器,它根据请求范围中设置的布局选择装饰器。

    所以在sitemesh.xml中:

    <decorator-mappers>
        <mapper class="org.x.x.CustomConfigDecoratorMapper">
            <param name="config" value="${decorators-file}" />
        </mapper>
    </decorator-mappers>
    

    CustomConfigDecoratorMapper 看起来像这样:

    public class CustomConfigDecoratorMapper extends AbstractDecoratorMapper {
    
        private static final Logger logger = Logger.getLogger(CustomConfigDecoratorMapper.class);
        private ConfigLoader configLoader = null;
    
        public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException
        {
            super.init(config, properties, parent);
            try
            {
                String fileName = properties.getProperty("config", "/WEB-INF/decorators.xml");
                configLoader = new ConfigLoader(fileName, config);
            }
            catch (Exception e) 
            {
                throw new InstantiationException(e.toString());
            }
        }
    
        public Decorator getDecorator(HttpServletRequest request, Page page)
        {
                String layoutName = "default";
    
                String configLayoutName = ( String)request.getParameter("layoutName" );
                if ( configLayoutName == null )
                {
                        configLayoutName = (String)request.getAttribute("layoutName");
                        if ( configLayoutName == null )
                        {
                                configLayoutName = "default";
                        }
                }
                if ( configLayoutName != null )
                {
                        layoutName = configLayoutName;
                }
    
                Decorator result = getNamedDecorator(request, layoutName);
                return result == null ? super.getDecorator(request, page) : result;
        }
    
        public Decorator getNamedDecorator(HttpServletRequest request, String name)
        {
                Decorator result = null;
                try
                {
                        result = configLoader.getDecoratorByName(name);
                }
                catch (ServletException e)
                {
                        logger.error("getNamedDecorator(HttpServletRequest, String)", e);
                }
                if (result == null || (result.getRole() != null && !request.isUserInRole(result.getRole())))
                {
                        return super.getNamedDecorator(request, name);
                }
                else
                {
                        return result;
                }
            }
        }
    

    除此之外..您是否考虑过使用 fmt:message 代替?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-30
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      相关资源
      最近更新 更多