Java 如何在Spring MVC中创建多布局
我知道ApacheTiles在Spring中的情况,它的工作原理似乎与jsp:include相同,但它并不能解决我的问题: 我想要一个名为layout1.jsp的文件,在这个文件中,我将定义一个布局,如下所示:Java 如何在Spring MVC中创建多布局,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,我知道ApacheTiles在Spring中的情况,它的工作原理似乎与jsp:include相同,但它并不能解决我的问题: 我想要一个名为layout1.jsp的文件,在这个文件中,我将定义一个布局,如下所示: <html> <head> <style href="style1.css" /> </head> <body> <div class="main"> <div class="left"
<html>
<head>
<style href="style1.css" />
</head>
<body>
<div class="main">
<div class="left">
<ul>
<li>
<li>
<li>
</ul>
</div>
<div class="content">
<h1>${message }</h1>
</div>
<div class="footer">
<span>This is footer</span>
</div>
</div>
</body>
</html>
-
-
-
${message}
这是页脚
其中一个文件是layout2.jsp:
<html>
<head>
<style href="style2.css" />
</head>
<body>
<div class="main">
<div class="left2">
<ul>
<li>
<li>
<li>
</ul>
</div>
<div class="content2">
<h1>${message }</h1>
</div>
<div class="footer2">
<span>This is footer</span>
</div>
</div>
</body>
</html>
-
-
-
${message}
这是页脚
当用户在组合框上选择布局名称时,控制器将在渲染布局之前设置布局动态
我该怎么做?要切换css文件,可以使用Spring主题解析器。您可以使用相同的机制来更改html的一些小部分(如
div
类)
...
${message}
...
(但我建议对相同的类使用相同的html,只需切换css)
不要忘记设置主题解析器
@请参见我找到了解决此问题的方法: 我的应用程序分隔符为多模块,例如“Admin”、“Default”,每个模块都有一个拦截器,例如模块拦截器:
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String servletPath = request.getServletPath();
if (!servletPath.equals("/") && servletPath.substring(servletPath.length() - 1).equals("/")) {
response.sendRedirect(request.getContextPath() + servletPath.replaceAll("\\/+$", ""));
return false;
}
return true;
}
在视图中,我有一个类InternalResourceViewResolver.java:
package view;
import helper.AppHelper;
import java.util.Locale;
import org.springframework.web.servlet.View;
public class InternalResourceViewResolver extends
org.springframework.web.servlet.view.InternalResourceViewResolver {
public View resolveViewName(String viewName, Locale locale)
throws Exception {
AppHelper.setPage("../" + AppHelper.getModule() + "/" + viewName + ".jsp");
return super.resolveViewName("layout/" + AppHelper.getLayout(), locale);
}
}
AppHelper类:
package helper;
public class AppHelper {
private static String module = "default";
private static String layout = "main";
private static String page = "index";
public static String getModule() {
return module;
}
public static void setModule(String module) {
AppHelper.module = module;
}
public static String getLayout() {
return layout;
}
public static void setLayout(String layout) {
AppHelper.layout = layout;
}
public static String getPage() {
return page;
}
public static void setPage(String page) {
AppHelper.page = page;
}
}
在其他模块中,管理示例:
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("ServletPath" + request.getServletPath());
AppHelper.setModule("admin");
AppHelper.setLayout("admin");
return true;
}
和我的查看页面:
在布局中:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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">
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
<title>Welcome to web</title>
</head>
<body>
<div class="wrapper">
<div class="main">
<div class="header"></div>
<div class="content">
<div class="content-left">
<jsp:include page="<%=helper.AppHelper.getPage() %>"></jsp:include>
</div>
<div class="content-right"></div>
</div>
<div class="footer"></div>
</div>
</div>
</body>
</html>
你也可以用瓷砖来做这个 使用apache tiles 2.2.1或更高版本 在应用程序的tiles.xml中,您可以在其中放置布局模型
<definition name="masterTile" templateExpression="${requestScope.layout} >
<put-attribute name="title" value="" />
<put-attribute name="header" value="/view/header.jsp" />
<put-attribute name="menu" value="/view/menu"/>
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/view/footer.jsp"/>
</definition>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<h1>${message }</h1>
<h2>${welcome }</h2>
<c:forEach items="${batches }" var="batch">
${batch.id } - ${batch.name }
</c:forEach>
- src\main\webapp\WEB-INF\pages
- admin
- index
- index.jsp
- setting.jsp
- product
- index.jsp
- detail.jsp
- other
...
- default
- index
- index.jsp
- contact.jsp
- product
...
- layout
admin.jsp
main.jsp
<definition name="masterTile" templateExpression="${requestScope.layout} >
<put-attribute name="title" value="" />
<put-attribute name="header" value="/view/header.jsp" />
<put-attribute name="menu" value="/view/menu"/>
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/view/footer.jsp"/>
</definition>