OGNL 和通配符在使用 struts2-tiles-plugin 的图块定义中工作? [英] OGNL and wildcards working in tiles definitions with struts2-tiles-plugin?

查看:20
本文介绍了OGNL 和通配符在使用 struts2-tiles-plugin 的图块定义中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 Tiles 从 struts2 值堆栈中解析 ognl.如何做到这一点?

使用 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

但我不确定如何在我的项目中做到这一点.有没有人在他们的 struts2 项目中使用tile3?我记得我读过一些关于在默认情况下打开图块 3 中所有新功能的方法,但我找不到该页面的链接.

如果可以使用 spring 以任何方式完成配置,那很好(如果有帮助,如果没有,则不是问题).

解决方案

我喜欢 Tiles tempate 系统,因为它简单直接,在 Tiles 2.2.2 中,您可以获得通配符支持,将您的 struts2 约定直接推入模板系统,现在还有OGNL

在磁贴内添加具有 OGNL 支持的磁贴 2.2.2 的步骤(当前使用 Struts 2.3.1.2):

1) 添加 struts2-tiles-plugin

2) 添加tiles 2.2.2 jars,包括添加ognl 支持的jars.假设 maven:所有组 ID 是:org.apache.tiles 所有版本都是 2.2.2,以下是工件 ID:

  • tiles-api
  • 瓷砖核心
  • tiles-extras
  • tiles-jsp
  • tiles-ognl
  • tiles-servlet

还需要添加以下内容:

通过使用 org.apache.tiles.extras.complete.CompleteAutoloadTilesListener,您将在您的tiles.xml 中获得通配符、EL、OGNL、MVEL 支持

示例 web.xml:

<监听器><listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class></监听器><过滤器><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></过滤器><过滤器映射><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></过滤器映射></web-app>

struts.xml 用于演示:该演示使用来自 struts 的 OGNL 来显示硬编码的问候消息.三个动作映射存在 ""、"test" 和 "package/*" 斜杠后面的值将被瓷砖用来设置新标题.一个非常简单的例子,但确实展示了一些有用的功能.

<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-type name="tiles" default="true" class="org.apache.struts2.views.tiles.TilesResult"/></结果类型><action name="/**" class="com.kenmcwilliams.tiles.action.Test"><结果>{1}</结果></动作></包></支柱>

tiles.xml

<瓷砖定义><定义名称=baseLayout"模板=/WEB-INF/content/tiles/template.jsp"><put-attribute name="title" value="默认标题"/><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"/></定义><定义名称=测试"扩展=baseLayout"><put-attribute name="title" value="测试标题"/></定义><定义名称="WILDCARD:package/*" extends="baseLayout"><put-attribute name="title" value="{1}" type="string"/></定义><定义名称="" extends="baseLayout"></定义></tiles-定义>

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

这是默认的正文.

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

这是默认页脚.

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

这是默认标题.

/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><头><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title><tiles:insertAttribute name="title"/></title><身体><tiles:insertAttribute name="header"/><tiles:insertAttribute name="body"/><tiles:insertAttribute name="footer"/><tiles:insertAttribute name="变量"/></html>

Test.java 操作

package com.kenmcwilliams.tiles.action;导入 com.opensymphony.xwork2.ActionSupport;导入 java.util.Map;导入 java.util.logging.Logger;导入 org.apache.struts2.interceptor.SessionAware;公共类 Test 扩展 ActionSupport 实现 SessionAware{private static final Logger log = Logger.getLogger(Test.class.getName());@覆盖公共字符串执行(){log.info("测试执行");返回成功;}private String greeting = "Hello, World from Action!";公共字符串 getGreeting(){回礼;}@覆盖public void setSession(Map session) {session.put("greeting", "来自会话的问候!");}}

现在应该足以让磁贴为您的应用程序工作.

I want Tiles to resolve ognl from the struts2 value stack. How to do this?

Using tiles 2.2.2 (although if a later version such as 3 could be used that is fine)

Here it mentions the new feature: http://tiles.apache.org/2.2/framework/whats-new.html

Here it shows how to implement it: http://tiles.apache.org/2.2/framework/tutorial/advanced/el-support.html#OGNL_Support

But I'm not certain how to go about that in my project. Does anyone have tiles3 working in their struts2 project? I remember reading about some way to turn on all new features in tiles 3 by default but I can't find a link to that page.

If configuration can be done in anyway with spring that is fine (if that helps, if not it is not an issue).

解决方案

I love the tiles tempate system because it is simple and straight forward, with Tiles 2.2.2 you can have wild card support to push your struts2 conventions right into the template system and now there is OGNL too!

Steps to add tiles 2.2.2 with OGNL support within tiles (Currently using Struts 2.3.1.2):

1) Add the struts2-tiles-plugin

2) Add the tiles 2.2.2 jars including the jars which add ognl support. Assumes maven: ALL group ids are: org.apache.tiles ALL versions are 2.2.2, the following are artifact ids :

  • tiles-api
  • tiles-core
  • tiles-extras
  • tiles-jsp
  • tiles-ognl
  • tiles-servlet

Also needed to add the following:

By using the org.apache.tiles.extras.complete.CompleteAutoloadTilesListener you get Wild Cards, EL, OGNL, MVEL support in your tiles.xml

Example 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 for a demo: The demo uses OGNL from struts to display a hard coded greeting message. Three action mappings exist "", "test", and "package/*" the value that follows the slash will be used by tiles to set a new title. A very trivial example but does show some useful features.

<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/content/tiles/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!");
    }
}

That should now be enough to get tiles working for your application.

这篇关于OGNL 和通配符在使用 struts2-tiles-plugin 的图块定义中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆