Struts通配符vs Tiles通配符



我在Struts2项目中使用tile来动态生成我的布局。问题是,我有很多冗余在我的tile .xml文件。所以我决定用通配符来解决这个问题。

我阅读了这个教程并遵循了所有的说明。我使用maven来创建我的web应用程序。请查看我的配置文件。

当我调用这个链接时:

localhost/test -> it works
localhost/** -> it works
localhost/test2 -> it does not work anymore, so wildcard was not called

请帮帮我。我不知道如何解决这个问题。谢谢你!

  1. pom.xml

        <project xmlns="http://maven.apache.org/POM/4.0.0" 
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                                     http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>projectName</groupId>
      <artifactId>tilesWildcard</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>tilesWildcard Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-tiles-plugin</artifactId>
            <version>2.3.8</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>tilesWildcard</finalName>
      </build>
    </project>
    
  2. src/主/资源/struts.xml

    <?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" />
        <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="/**" >
                <result>{1}</result>
            </action>
        </package>
    </struts>
    
  3. /web - inf/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/tiles/template.jsp">
            <put-attribute name="title" value="Default Title"/>
            <put-attribute name="header" value="/WEB-INF/tiles/header.jsp"/>
            <put-attribute name="body" value="/WEB-INF/tiles/body.jsp"/>
            <put-attribute name="footer" value="/WEB-INF/tiles/footer.jsp"/>
        </definition>
        <definition name="test" extends="baseLayout">
            <put-attribute name="Test Title" value="Default Title"/>
        </definition>
        <definition name="package/*" extends="baseLayout">
            <put-attribute name="title" value="{1}" type="string"/>
        </definition>
        <definition name="" extends="baseLayout">
        </definition>
    </tiles-definitions>
    
  4. 文件结构
    /WEB-INF
      /tiles/body.jsp
      /tiles/footer.jsp
      /tiles/header.jsp
      /tiles/template.jsp
    

很抱歉很晚才回复,但是您正在调用:localhost/test2,但是tiles的定义是"package/*"所以您需要调用localhost/package/test2

相关内容

  • 没有找到相关文章

最新更新