preface
This article is a reference from https://www.w3cschool.cn/struts_2/struts_tiles.html Make a practice log.
jar package download address
Please go to the website to download the relevant jar package and download an all version
https://struts.apache.org/download.cgi
Initialization project and jar package pull
Here you need to unzip the downloaded from the above website, find the lib folder, pull in the jar package related to tile, then have a contact package struts 2-tiles-plugin-2.3.37, and then import some struts packages.
Create MenuAction
MenuAction, which is mainly to create two methods as action calls
package com.czx.tiles.action; import com.opensymphony.xwork2.ActionSupport; public class MenuAction extends ActionSupport { public String tiger() { return "tiger"; } public String lion() { return "lion"; } public String baseLayout() { return "baseLayout"; } }
Find pictures
Baidu looks for three pictures, namely tiger, lion and converted pictures, and then stores them in the new images folder
Modify web xml
Here is mainly to specify the location of a configuration file and handle the listener
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssh_learn_tiles_integration</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name> <param-value> /WEB-INF/tiles.xml </param-value> </context-param> <listener> <listener-class> org.apache.struts2.tiles.StrutsTilesListener </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>
Create tiles xml
tiles.xml, here is mainly to create some content to be returned at that time. Each definition in it is equivalent to a returned content, and the put attribute in it can realize the combination of multiple pages.
As for the following extensions, they inherit from the first template, which is actually baselayout JSP.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> <tiles-definitions> <definition name="baseLayout" template="/baseLayout.jsp"> <put-attribute name="title" value="Base layer"></put-attribute> <put-attribute name="banner" value="/banner.jsp"></put-attribute> <put-attribute name="menu" value="/menu.jsp"></put-attribute> <put-attribute name="body" value="/body.jsp"></put-attribute> <put-attribute name="footer" value="/footer.jsp"></put-attribute> </definition> <definition name="tiger" extends="baseLayout"> <put-attribute name="title" value="Tiger"></put-attribute> <put-attribute name="body" value="/tiger.jsp"></put-attribute> </definition> <definition name="lion" extends="baseLayout"> <put-attribute name="title" value="Lion"></put-attribute> <put-attribute name="body" value="/lion.jsp"></put-attribute> </definition> </tiles-definitions>
Create struts xml
struts.xml, here is mainly to specify a result type tiles, and then change the result type of result to tiles, and then you need to match the contents with the previously set tiles The name of the definition set in XML corresponds to.
We use some regular expressions to achieve this. In the definition of action, we say that anything matching the "* Menu" pattern will be processed by this action. The matching method will be called in the MenuAction class. That is, tigermenu Action will call tiger() and lionmenu Action will call lion(). Then, we need to map the results to the appropriate title page.
<?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.3.dtd"> <struts> <package name="menu" extends="struts-default"> <result-types> <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"></result-type> </result-types> <action name="*Menu" class="com.czx.tiles.action.MenuAction" method="{1}"> <result name="lion" type="tiles">lion</result> <result name="tiger" type="tiles">tiger</result> </action> <action name="baseLayout" class="com.czx.tiles.action.MenuAction" method="baseLayout"> <result name="baseLayout" type="tiles">baseLayout</result> </action> </package> </struts>
Create relevant jsp pages
baseLayout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" > <title> <tiles:insertAttribute name="title" ignore="true"></tiles:insertAttribute> </title> </head> <body> <tiles:insertAttribute name="banner"></tiles:insertAttribute><br/> <hr/> <tiles:insertAttribute name="menu"></tiles:insertAttribute><br/><hr/> <tiles:insertAttribute name="body"></tiles:insertAttribute><br/><hr/> <tiles:insertAttribute name="footer"></tiles:insertAttribute><br/> </body> </html>
banner.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <img alt="Icon image missing" src="images/switch.jpg" style="height:60px;width:60px"> </body> </html>
menu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href='<s:url action='tigerMenu'></s:url>'>tiger</a> <a href="<s:url action='lionMenu'/>">lion</a> </body> </html>
tiger.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> tiger:<br/> <img alt="Tiger picture missing" src="images/tiger.jpg"> </body> </html>
lion.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> lion:<br/> <img alt="Lion picture missing" src="images/lion.jpg"> </body> </html>
body.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> body.jsp of body body </body> </html>
footer.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> tail </body> </html>
test
visit http://localhost:8080/ssh_learn_tiles_integration/baseLayout
Click the tiger will become like this. In fact, visit http://localhost:8080/ssh_learn_tiles_integration/tigerMenu The same effect can be achieved
Click the lion will become like this, actually visit http://localhost:8080/ssh_learn_tiles_integration/lionMenu The same effect can be achieved
Exceptions in the middle
org.apache.tiles.template.NoSuchAttributeException: Attribute 'bannner' not found.
Originally struts The returned in XML is no longer XXX JSP, but to set the format and change it to the corresponding set definition name
Project address
SSH in this warehouse_ learn_ tiles_ integration
https://gitee.com/mrchen13427566118/ssh_learn.git
If you don't know how to get eclipse, just look here
https://blog.csdn.net/weixin_43987277/article/details/116936221