Sitemesh spring:模板中无法识别消息



我们在Sitemesh 2的项目中使用spring:message标记。在decorator中使用spring:消息时,不能识别-标记。我们可以在jsp页面中使用-标记,但可以在decorator 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时,则不起作用。

我们该如何解决这个问题?

<spring:message code="msg.x.x.x"  />

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

有几件事需要检查。。

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

  • 我不确定它是否会对过滤器链产生影响,但我使用了一个自定义的configdecorator映射器,它根据请求范围中的布局集来选择装饰器。

所以在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?

最新更新