Jac*_*ack 6 java spring spring-mvc tiles-3
我有一个使用Tile3的Spring MVC应用程序.我有很多静态页面需要将它们嵌入到tile3当前提供的网站模板中.(我需要在所有页面上使用相同的页脚和页眉,无论是动态还是静态,但不确定如何对静态页面进行寻址).
静态页面的示例是index.jsp和aboutus.jsp.如何访问这些静态页面?我应该通过控制器吗?
我知道我可以使用jsp:include但这是一个好习惯吗?因为我正在使用瓷砖,所以没有其他选择吗?本教程建议使用单独的控制器,但我不确定这是否是最佳解决方案.因为它向服务器发送不必要的请求.
如果有比Tiles更好的选择,请告诉我
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springapp-servlet.xml</param-value>
</context-param>
</web-app>
Run Code Online (Sandbox Code Playgroud)
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/templates/baseLayout.jsp">
<put-attribute name="title" value="Title is here (Tile)"/>
<put-attribute name="header" value="header.jsp"/>
<put-attribute name="menu" value="Title is here (Tile)"/>
<put-attribute name="body" value="Title is here (Tile)"/>
<put-attribute name="footer" value="footer.jsp"/>
</definition>
<definition name="hello" extends="baseLayout">
<put-attribute name="title" value="HELERE"/>
<put-attribute name="body" value="/WEB-INF/pages/pages/ewfsdfsdf.jsp"/>
</definition>
<definition name="index" extends="baseLayout">
<put-attribute name="title" value="HELERE"/>
<put-attribute name="body" value="/WEB-INF/pages/index.jsp"/>
</definition>
</tiles-definitions>
Run Code Online (Sandbox Code Playgroud)
调节器
@Controller
public class HelloController {
protected final Log logger = LogFactory.getLog(getClass());
public HelloController() {
System.err.println("Constructor of HelloController");
}
@RequestMapping("/index.htm")
public String index(){
System.err.println("in index method");
return "index";
}
Run Code Online (Sandbox Code Playgroud)
baseLayout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title"/></title>
</head>
<body>
<div id="container">
<tiles:insertAttribute name="header"/>
<tiles:insertAttribute name="menu"/>
<tiles:insertAttribute name="body"/>
<tiles:insertAttribute name="footer"/>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
的index.jsp
<p>
This is the body of index page
</p>
Run Code Online (Sandbox Code Playgroud)
使用JSP:include
pros
- No extra load neither on DB nor on server
cons
- To backup need to backup all static files
- Might have overwork as each page should be prepared separately rather than having a single template for all pages and just populate the template
- If need to add something to static pages need to change all pages.
Run Code Online (Sandbox Code Playgroud)
我刚刚找到了一个很好的解决方案,为了处理我们可以<mvc:view-controller>
在 servlet 中使用的静态页面,示例代码在这里和这里,它也可以与 Tiles 一起使用,只需确保文件definition
中的每个路径都有一个tiles.xml
。
<mvc:view-controller path="/index" />
<mvc:view-controller path="/" view-name="index"/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1601 次 |
最近记录: |