Loading resources into tiles and spring mvc

I am using spring mvc 3 and tiles 2 with wildcard definitions. I want to load additional css and javascript files inside some of my fragments. Is there any way to do this? Preferably in the jsp tile file rather than in the -definition.xml methods.

+4
source share
3 answers

This is a good question, because one of the main advantages of the tile is the central view that it provides in relation to the composition. It would be nice if this centralization could also include CSS and JS files.

It happens that this is possible, here is an example. Tiles3 are used in this example, but it’s quite easy to adapt to tiles-2 (three three layers allow you to use several types of expressions), you can do this on the side.

Also note that I use Struts2 as my framework, this is not a problem, but since I am going to use a working example, you will find out that the expression “OGNL:” with a prefix means that using EL Struts2 will be used, you should also know that when upgrading to Tiles-3 you can also use Spring EL prefix expressions using "MVEL:".

tiles.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> <tiles-definitions> <definition name="default" template="/WEB-INF/template/template.jsp"> <put-list-attribute name="cssList" cascade="true"> <add-attribute value="/style/cssreset-min.css" /> <add-attribute value="/style/cssfonts-min.css" /> <add-attribute value="/style/cssbase-min.css" /> <add-attribute value="/style/grids-min.css" /> <add-attribute value="/script/jquery-ui-1.8.24.custom/css/ui-lightness/jquery-ui-1.8.24.custom.css" /> <add-attribute value="/style/style.css" /> </put-list-attribute> <put-list-attribute name="jsList" cascade="true"> <add-attribute value="/script/jquery/1.8.1/jquery.min.js" /> <add-attribute value="/script/jquery-ui-1.8.24.custom/js/jquery-ui-1.8.24.custom.min.js" /> <add-attribute value="/script/jquery.sort.js" /> <add-attribute value="/script/custom/jquery-serialize.js" /> </put-list-attribute> <put-attribute name="title" value="defaults-name" cascade="true" type="string"/> <put-attribute name="head" value="/WEB-INF/template/head.jsp"/> <put-attribute name="header" value="/WEB-INF/template/header.jsp"/> <put-attribute name="body" value="/WEB-INF/template/body.jsp"/> <put-attribute name="footer" value="/WEB-INF/template/footer.jsp"/> </definition> <definition name="REGEXP:\/recruiter#candidate-input\.(.*)" extends="default"> <put-list-attribute name="cssList" cascade="true" inherit="true"> <add-attribute value="/style/recruiter/candidate-input.css" /> </put-list-attribute> <put-list-attribute name="jsList" cascade="true" inherit="true"> <add-attribute value="/script/widgets/resume/resume.js" /> </put-list-attribute> <put-attribute name="body" value="/WEB-INF/content/recruiter/candidate-input.jsp"/> </definition> <definition name="REGEXP:(.*)#(.*)" extends="default"> <put-attribute name="title" cascade="true" expression="OGNL:@ com.opensymphony.xwork2.ActionContext@getContext ().name"/> <put-attribute name="body" value="/WEB-INF/content{1}/{2}"/> </definition> </tiles-definitions> 

/WEB-INF/template/template.jsp

 <%@taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %> <%@taglib prefix="s" uri="/struts-tags"%> <%@page contentType="text/html" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <tiles:insertAttribute name="head"/> <body> <%-- website header --%> <div id="wrapper"> <div id="content"> <tiles:insertAttribute name="header"/> <tiles:insertAttribute name="body"/> <div class ="outer content"> <tiles:insertAttribute name="footer"/> </div> </div> </div> </body> </html> 

This is the important part getting lists of CSS files and JS files to the head:

/WEB-INF/template/head.jsp

 <%@taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%> <%@taglib prefix="s" uri="/struts-tags"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <tiles:importAttribute name="cssList"/><tiles:importAttribute name="jsList"/> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <s:iterator value="#attr.cssList" var="cssValue"> <link href="<s:url value="%{cssValue}"/>" rel="stylesheet" type="text/css"> </s:iterator> <s:iterator value="#attr.jsList" var="jsValue"> <script src="<s:url value="%{jsValue}"/>"></script> </s:iterator> <title><tiles:insertAttribute name="title" defaultValue="no title"/></title> </head> 

I think you can understand the rest. Sorry for the <s:iterator> tags in the last block, I'm not sure about the Spring equivalent, and I would not be inclined to test it. But if you translate this to Spring, it would be great to answer this question. I would love to vote.

+8
source

Dispatcher-servlet.xml provides a mapping of static mvc resources as follows:

 <!-- static resource mapping for style sheets, etc. --> <mvc:resources mapping="/styles/**" location="/WEB-INF/skins/" /> <mvc:resources mapping="/scripts/**" location="/WEB-INF/scripts/" /> 

And in your tiles-layout.jsp file, you can access them by writing

 <script type="text/javascript" src="${context}/scripts/jquery-1.7.js></script> <link rel="stylesheet" type="text/css" href="${context}/styles/css/superfish.css"> 

See: mvc: resources

+1
source

So I did with Spring, the rest is exactly the same as Quaternion.

/WEB-INF/template/head.jsp

 <tiles:importAttribute name="cssList"/> <tiles:importAttribute name="jsList"/> <head> <c:forEach var="cssValue" items="${cssList}"> <link type="text/css" rel="stylesheet" href="<c:url value="${cssValue}"/>" /> </c:forEach> <c:forEach var="jsValue" items="${jsList}"> <script src="<c:url value="${jsValue}"/>"></script> </c:forEach> </head> 

And do not forget to indicate on each page the correct definition from tiles.xml

 <tiles:insertDefinition name="definitionName"> <tiles:putAttribute name="body"> //content </tiles:putAttribute> </tiles:insertDefinition> 
+1
source

Source: https://habr.com/ru/post/1481368/


All Articles