如何在TilesRequestProcessor中更改布局定义运行时

时间:2014-05-15 07:00:27

标签: java struts tiles

我有一个场景, 当请求来自IE浏览器然后显示login(这是默认行为),当它来自Chrome浏览器然后显示login_chrome(布局定义在Tiles.xml中,如下所示)

<definition name="login" path="/login/login1.jsp"></definition>
<definition name="login_chrome" path="/login/login2.jsp"></definition>

我有CustomizedTileProcessor

    public class CustomizedTileProcessor extends TilesRequestProcessor
    {
     @Override
   protected boolean processTilesDefinition(String pDefinitionName, boolean pContextRelative, HttpServletRequest pRequest, HttpServletResponse pResponse) throws IOException, ServletException
   {
      if (isChromeBrowser(pRequest))
      {
         pDefinitionName+="_chrome";
      }
      return super.processTilesDefinition(pDefinitionName, true, pRequest, pResponse);
   } 
}

注意:我不想在JSP中放置逻辑来根据条件选择包含的JSP。

请帮帮我,告诉我覆盖TilesRequestProcessor哪种方法可以动态更改切片定义?

1 个答案:

答案 0 :(得分:0)

经过大量试验和错误后,我知道我必须覆盖processForwardConfig TilesRequestProcessor方法。在那里,我必须将path对象的ForwardConfig值的值覆盖到我的新布局,即login_chrome

请参阅以下代码:

public class CustomizedTileProcessor extends TilesRequestProcessor
    {
     @Override
 protected void processForwardConfig(HttpServletRequest pRequest, 
       HttpServletResponse pResponse, ForwardConfig pForward) 
       throws IOException, ServletException
   {
      if(isChromeBrowser(pRequest))
      {
         ForwardConfig newForward = new ForwardConfig(pForward.getName(),
         pForward.getPath()+"_chrome", pForward.getRedirect(),
         pForward.getContextRelative());

         super.processForwardConfig(pRequest, pResponse, newForward);
      }
      else
      {
         super.processForwardConfig(pRequest, pResponse, pForward);
      }
   }
}