如何将Struts 2与Tiles 3集成

6

我们如何将Struts 2与Tiles 3集成?目前(2.3.4.1),struts2-tiles-plugin与较旧版本的tiles(版本2.0.6)一起使用,这可能有点麻烦。

这是一个自答案,为了帮助其他人进行集成。

3个回答

8
解决方案是添加所需的依赖项,使用适当的监听器加载瓷砖,并创建自定义结果类型。幸运的是,这些步骤非常容易完成,完成后,您可以按照正常的tiles 2示例来定义模板。
1) 依赖项(从基本的struts项目开始,但在此示例中,我将使用约定,因此最好添加struts2-conventions-plugin,它将包括struts2-core等):
  • 不要包含 struts2-tiles-plugin
  • groupId: org.apache.tiles,artifiactId: tiles-extras,version: 3.0.1
  • groupId: org.slf4j,artifiactId: jcl-over-slf4j,version: 1.5.8
  • groupId: org.slf4j,artifiactId: slf4j-jdk14,version: 1.5.8
注意:更高版本的slf4j依赖项可能有效,但我没有测试过。
2) 使用适当的监听器加载瓷砖

该示例包括完整的web.xml,对于熟悉struts2的人来说,第3-5行是唯一新的内容。

<?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>

3) 创建自定义结果类型

我们需要为我们的操作定义一个自定义结果类型:

package com.quaternion.result;

import com.opensymphony.xwork2.ActionInvocation;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletDispatcherResult;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;

public class TilesResult extends ServletDispatcherResult {

    public TilesResult() {
        super();
    }

    public TilesResult(String location) {
        super(location);
    }

    @Override
    public void doExecute(String location, ActionInvocation invocation) throws Exception {
        //location = "test.definition"; //for test
        setLocation(location);
        ServletContext context = ServletActionContext.getServletContext();
        ApplicationContext applicationContext = ServletUtil.getApplicationContext(context);
        TilesContainer container = TilesAccess.getContainer(applicationContext);
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        ServletRequest servletRequest = new ServletRequest(applicationContext, request, response);
        container.render(location, servletRequest);
    }
}

4) 我们还需要告诉struts2关于我们的结果类型:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />
    <package  name="tiles-package"  namespace="" extends="struts-default">
        <result-types>
            <result-type default="true" name="tiles-result" class="com.quaternion.result.TilesResult"/>
        </result-types>
    </package>   
</struts>

现在我们可以在项目中使用瓷砖了,假设我们已经创建了一个名为“test.definition”的瓷砖定义,我们可以通过以下方式告诉我们的操作使用该定义:

package com.quaternion.demo.action.test;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

@ParentPackage("tiles-package")
@Result(type="tiles-result", location="test.definition")
public class QuaternionResultTest extends ActionSupport{}

就这样,这将让您配置任何版本的struts2与tiles 3+,请参见http://tiles.apache.org/framework/index.html以获取更多配置详情。


是的,我应该创建一个JIRA任务...你是指这个吗? - Quaternion
在我写下之前的评论时,我不知道你和struts-dev的一个男人是同一个人。 - Andrei Botalov

5

我一直在尝试将瓦片与struts2-conventions绑定,虽然它能工作,但并不美观...也许我可以把它发给你? - Quaternion
请执行,这是支持Tiles 3的第一次尝试,因此我们可以在下一个版本中进行扩展。 - Lukasz Lenart
1
将被接受的答案移至此处,因为...这个插件可以用更少的工作完成上述操作。 - Quaternion

0
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

在你的tiles.xml文件中使用提到的doctype


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接