我在我的 Struts2 项目中使用图块来动态生成我的布局。问题是,我的tiles.xml 文件中有很多冗余。所以我决定在瓷砖中使用通配符来解决这个问题。
我阅读了本教程并按照所有说明进行操作。我使用 maven 创建了我的 webapp。请看我的配置文件。
当我调用此链接时:
localhost/test -> it works
localhost/** -> it works
localhost/test2 -> it does not work anymore, so wildcard was not called
请帮我。我不知道如何解决这个问题。谢谢!
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>
src/main/resources/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>
/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>
文件结构
/WEB-INF /tiles/body.jsp /tiles/footer.jsp /tiles/header.jsp /tiles/template.jsp