如何将 Struts 2 与 Tiles 3 集成

2024-04-19

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

这是一个自我回答,帮助他人融入。


解决方案是添加所需的依赖项,使用适当的侦听器加载图块并创建自定义结果类型。幸运的是,这些步骤非常简单,完成后您可以按照正常的图块 2 示例来了解如何定义模板。

1) 依赖关系(从基本的 struts 项目开始,但在本例中我将使用约定,因此最好添加 struts2-conventions-plugin,它将包括 struts2-core 等):

  • 不包括struts2-tiles-插件
  • groupId:org.apache.tiles,工件ID:瓷砖额外,version: 3.0.1
  • groupId: org.slf4j,工件ID:jcl-over-slf4j,version: 1.5.8
  • groupId: org.slf4j,工件ID:slf4j-jdk14,version: 1.5.8

Note:更高版本的slf4j依赖may工作我还没有测试过这个。

2) 使用适当的侦听器加载图块

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

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

就是这样,这将允许您使用tiles 3+配置任何版本的struts2,请参阅http://tiles.apache.org/framework/index.html http://tiles.apache.org/framework/index.html了解更多配置详细信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 Struts 2 与 Tiles 3 集成 的相关文章

随机推荐