Struts Wildcard vs Tiles通配符NoSuchDefinitionException

时间:2013-01-17 19:21:52

标签: java struts2 wildcard tiles

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

I read this tutorial并按照所有说明进行操作。我用maven创建了我的webapp。请查看我的配置文件。

当我打电话给这个链接时:

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
    

1 个答案:

答案 0 :(得分:0)

很抱歉很晚的回复,但是你正在调用:localhost / test2,但是tile定义是“package / *”,所以你需要调用localhost / package / test2