2012-04-06 52 views
0

我想要Tiles从struts2的值栈中解析ognl。这个怎么做?OGNL和通配符在struts2-tiles-plugin中使用tile定义?

使用瓷砖2.2.2(不过,如果如3可以使用更高版本是罚款)

这提到的新功能: http://tiles.apache.org/2.2/framework/whats-new.html

这说明如何实现它: http://tiles.apache.org/2.2/framework/tutorial/advanced/el-support.html#OGNL_Support

但我不确定在我的项目中如何去做。有没有人有tile3在他们的struts2项目中工作?我记得读过关于默认情况下打开瓷砖3中所有新功能的方式,但我找不到该页面的链接。

如果可以用弹簧来完成配置,那很好(如果有帮助的话,如果没有的话,这不是问题)。

+0

我不确定你想要做什么 - 你的意思是你想让Tiles中的OGNL def使用S2值堆栈吗? – 2012-04-08 14:44:16

+0

是的,它确实如此。我只是不知道自己在做什么,文档可能会更清晰一些。 – Quaternion 2012-04-08 22:33:54

回答

3

我喜欢瓷砖tempate系统,因为它简单直接,与瓷砖2.2.2你可以有通配符支持将你的struts2约定直接进入模板系统,现在有OGNL也

步骤与OGNL支持添加瓷砖瓷砖2.2.2(目前使用Struts 2.3.1.2)内:

1)添加Struts2的瓷砖,插件

2)添加瓷砖2.2.2罐子包括增加ognl支持的罐子。假设行家:ALL组ID:org.apache.tiles所有版本都2.2.2,以下是神器IDS:

  • 瓷砖-API
  • 瓷砖核心
  • tiles-演员
  • 砖,JSP
  • 砖,OGNL
  • 砖,servlet的

此外,还需要添加以下:

通过使用org.apache.tiles.extras.complete.CompleteAutoloadTilesListener你得到野生ç ARDS,EL,OGNL,在你tiles.xml MVEL支持

实施例的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <listener> 
     <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class> 
    </listener> 
    <filter> 
     <filter-name>struts2</filter-name> 
     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>struts2</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
</web-app> 

struts.xml中用于演示:演示使用OGNL从支柱以显示硬编码的问候消息。存在三个动作映射“”,“test”和“package/*”,斜线后面的值将被tile用来设置一个新标题。一个非常简单的例子,但确实显示了一些有用的功能。

<struts> 
    <constant name="struts.devMode" value="true" /> 
    <constant name="struts.ui.theme" value="simple" /> 
    <constant name="struts.enable.SlashesInActionNames" value="true" /> 
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/> 
    <package name="basicstruts2" namespace="" extends="tiles-default"> 
     <result-types> 
      <result-type name="tiles" default="true" class="org.apache.struts2.views.tiles.TilesResult"/> 
     </result-types> 
     <action name="/**" class="com.kenmcwilliams.tiles.action.Test"> 
      <result>{1}</result> 
     </action> 
    </package> 
</struts> 

tiles.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE tiles-definitions PUBLIC 
     "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN" 
     "http://tiles.apache.org/dtds/tiles-config_2_1.dtd"> 
<tiles-definitions> 
    <definition name="baseLayout" template="/WEB-INF/content/tiles/template.jsp"> 
     <put-attribute name="title" value="Default Title"/> 
     <put-attribute name="header" value="/WEB-INF/content/tiles/header.jsp"/> 
     <put-attribute name="body" value="/WEB-INF/content/tiles/body.jsp"/> 
     <put-attribute name="footer" value="/WEB-INF/content/tiles/footer.jsp"/> 
     <put-attribute name="variable" expression="OGNL:greeting"/> 
    </definition> 
    <definition name="test" extends="baseLayout"> 
     <put-attribute name="title" value="Test Title"/> 
    </definition> 
    <definition name="WILDCARD:package/*" extends="baseLayout"> 
     <put-attribute name="title" value="{1}" type="string"/> 
    </definition> 
    <definition name="" extends="baseLayout"> 
    </definition> 
</tiles-definitions> 

/WEB-INF/content/tiles/body.jsp

<div> 
    This is the default body. 
</div> 

/WEB-INF /内容/瓦/ footer.jsp

<div> 
    This is the default footer. 
</div> 

/WEB-INF/content/tiles/header.jsp

<div> 
    This is the default header. 
</div> 

/WEB-INF/content/tiles/template.jsp

<%@taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> 
<%@taglib prefix="s" uri="/struts-tags"%> 
<%@page contentType="text/html" pageEncoding="UTF-8" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title><tiles:insertAttribute name="title"/></title> 
    </head> 
    <body> 
     <tiles:insertAttribute name="header"/> 
     <tiles:insertAttribute name="body"/> 
     <tiles:insertAttribute name="footer"/> 
     <tiles:insertAttribute name="variable"/> 
    </body> 
</html> 

Test.java Action

package com.kenmcwilliams.tiles.action; 

import com.opensymphony.xwork2.ActionSupport; 
import java.util.Map; 
import java.util.logging.Logger; 
import org.apache.struts2.interceptor.SessionAware; 

public class Test extends ActionSupport implements SessionAware{ 
    private static final Logger log = Logger.getLogger(Test.class.getName()); 
    @Override 
    public String execute(){ 
     log.info("Test execute"); 
     return SUCCESS; 
    } 

    private String greeting = "Hello, World from Action!"; 
    public String getGreeting(){ 
     return greeting; 
    } 

    @Override 
    public void setSession(Map<String, Object> session) { 
     session.put("greeting", "Greetings from session!"); 
    } 
} 

现在应该足以让瓷砖适用于您的应用程序。

+0

在这个问题上的任何帮助,请http://stackoverflow.com/questions/13126201/struts2-tiles-org-apache-tiles-extras-complete-completeautoloadtileslistener-int – Pirzada 2012-10-29 17:08:21