我尝试在我的项目中使用ModelMap,但我无法访问JSP页面中的ModelMap值。
我的dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>
<context:component-scan base-package="controller"></context:component-scan>
<mvc:annotation-driven />
<!-- Most controllers will use the ControllerClassNameHandlerMapping above,
but for the index controller we are using ParameterizableViewController,
so we must define an explicit mapping for it. -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.html">indexController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles3.TilesView</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<!-- The index controller. -->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
我的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="index" template="/WEB-INF/jsp/partical/main.jsp">
<put-attribute name="content" value="/WEB-INF/jsp/partical/index.jsp" />
</definition>
<definition name="dangkyphong" template="WEB-INF/jsp/partical/main.jsp">
<put-attribute name="content" value="WEB-INF/jsp/partical/dangkyphong.jsp" />
</definition>
<definition name="quanlyphong" template="/WEB-INF/jsp/partical/main.jsp">
<put-attribute name="content" value="/WEB-INF/jsp/partical/quanlyphong.jsp" />
</definition>
<definition name="quanlymaytinh" template="/WEB-INF/jsp/partical/main.jsp">
<put-attribute name="content" value="/WEB-INF/jsp/partical/quanlymaytinh.jsp" />
</definition>
<definition name="themphong" template="/WEB-INF/jsp/partical/main.jsp">
<put-attribute name="content" value="/WEB-INF/jsp/partical/themphong.jsp" />
</definition>
<definition name="themay" template="/WEB-INF/jsp/partical/main.jsp">
<put-attribute name="content" value="/WEB-INF/jsp/partical/themmay.jsp" />
</definition>
<definition name="chinhsuapc" template="/WEB-INF/jsp/partical/main.jsp">
<put-attribute name="content" value="/WEB-INF/jsp/partical/chinhsuapc.jsp" />
</definition>
</tiles-definitions>
我的项目免费
我试试这个:
@RequestMapping(method = RequestMethod.GET)
public String index(ModelMap mm) {
mm.put("title", "Home");
return "index";
}
在JSP页面中的我尝试使用:${title}
但我无法在JSP标题中获得字符串“Home”
请帮我解决这个问题! ----编辑我的JSP页面----
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@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 charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>${title}</title>
<tiles:insertAttribute name = "head" ignore="true"></tiles:insertAttribute>
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/custom.css" type="text/css" />
<link href="${pageContext.request.contextPath}/css/mui.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="sidedrawer" class="navbar-default mui--no-user-select"><a href="${pageContext.request.contextPath}/index.html">
<div id="sidedrawer-brand" class="mui--appbar-line-height mui--text-title">Labs Manager</div></a>
<div class="mui-divider"></div>
<ul>
<strong>Quản Lý Phòng Máy</strong>
<ul>
<li class="nav-menu">
<a href="${pageContext.request.contextPath}/index/themphong.html">
<span class="small-nav" data-toggle="tooltip" data-placement="right" title="Home">
<span class="glyphicon glyphicon-plus"></span>
</span>
<span class="full-nav"> Thêm Phòng </span>
</a>
</li>
<li class="nav-menu">
<a href="${pageContext.request.contextPath}/index/dangkyphong.html">
<span class="small-nav" data-toggle="tooltip" data-placement="right" title="About Us">
<span class="glyphicon glyphicon-pencil"></span>
</span>
<span class="full-nav"> Đăng Ký Phòng </span>
</a>
</li>
<li class="nav-menu">
<a href="${pageContext.request.contextPath}/index/quanlyphong.html">
<span class="small-nav" data-toggle="tooltip" data-placement="right" title="Contact Us">
<span class="glyphicon glyphicon-edit"></span>
</span>
<span class="full-nav"> Quản Lý Phòng </span>
</a>
</li>
</ul>
<strong>Quản Lý Máy Tính</strong>
<ul>
<li class="nav-menu">
<a href="${pageContext.request.contextPath}/index/themmay.html">
<span class="small-nav" data-toggle="tooltip" data-placement="right" title="Contact Us">
<span class="glyphicon glyphicon-plus"></span>
</span>
<span class="full-nav"> Thêm Máy</span>
</a>
</li>
<li class="nav-menu">
<a href="${pageContext.request.contextPath}/index/chinhsuapc.html">
<span class="small-nav" data-toggle="tooltip" data-placement="right" title="Contact Us">
<span class="glyphicon glyphicon-edit"></span>
</span>
<span class="full-nav"> Chỉnh Sửa </span>
</a>
</li>
</ul>
</ul>
</div>
<header id="header">
<div class="mui-appbar mui--appbar-line-height">
<div class="mui-container-fluid">
<a class="sidedrawer-toggle mui--visible-xs-inline-block mui--visible-sm-inline-block js-show-sidedrawer">☰</a>
<a class="sidedrawer-toggle mui--hidden-xs mui--hidden-sm js-hide-sidedrawer">☰</a>
<span class="mui--text-title mui--visible-xs-inline-block mui--visible-sm-inline-block">Labs Manager</span>
<ul class="nav navbar-nav navbar-right">
<li id="user-nav" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Hello, Administrator<b class="caret"></b></a>
<ul class="dropdown-menu" id="dropdown-menu">
<li><a href="#">Log out</a></li>
</ul>
</li>
</ul>
</div>
</div>
</header>
<div id="content-wrapper">
<div class="mui--appbar-height"></div>
<div class="mui-container-fluid">
<!-- Main Content -->
<tiles:insertAttribute name="content"></tiles:insertAttribute>
</div>
</div>
<footer id="footer">
<div class="mui-container-fluid">
<br>
Copyright © 2016
</div>
</footer>
<script>
$(document).ready(function () {
$('data-toggle="tooltip"').tooltip();
});
</script>
<script src="${pageContext.request.contextPath}/js/jquery-2.1.4.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/mui.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/script.js" type="text/javascript"></script>
</body>
</html>
答案 0 :(得分:0)
使用图块时,可以直接从图块xml文件中指定页面名称 为此,您需要执行以下操作
在您的定义中添加另一个名为title
的属性<definition name="index" template="/WEB-INF/jsp/partical/main.jsp">
<put-attribute name="title" value="Index" />
<put-attribute name="content" value="/WEB-INF/jsp/partical/index.jsp" />
</definition>
在您的模板页面(/WEB-INF/jsp/partical/main.jsp)中插入title属性,如下所示
<title><tiles:insertAttribute name="title" /></title>
您的控制器无需属性,请删除此
mm.put("title", "Home");
如果我能给你一个意见,我相信你没有使用瓷砖可以提供的全部潜力。
Apache tiles是一个框架,允许您不在每个页面中复制代码。 因此,为您的网站创建基本布局非常重要,然后您可以仅更改每个页面中的正文
举个例子
<强>模板:强>
<definition name="app.base" template="/WEB-INF/views/template.jsp">
<put-attribute name="title" value="Not Found" />
<put-attribute name="body" value="/WEB-INF/views/body.jsp" />
<put-attribute name="footer" value="/WEB-INF/views/footer.jsp" />
<put-list-attribute name="stylesheets_top">
<add-attribute value="/resources/site/library/bootstrap/bootstrap.min.css" />
<add-attribute value="/resources/site/library/font-awesome/font-awesome.min.css" />
</put-list-attribute>
<put-list-attribute name="javascripts_bottom">
<add-attribute value="/resources/js/jquery/jquery-1.12.0.min.js" />
<add-attribute value="/resources/site/library/bootstrap/bootstrap.min.js" />
</put-list-attribute>
</definition>
扩展模板的页面:
<definition name="home" extends="app.base">
<put-attribute name="title" value="Home" />
<put-attribute name="body" value="/WEB-INF/jsp/home.jsp" />
<put-list-attribute name="stylesheets_top" inherit="true">
<add-attribute value="/resources/site/home/home.css" />
</put-list-attribute>
<put-list-attribute name="javascripts_bottom" inherit="true">
<add-attribute value="/resources/site/home/home.js" />
</put-list-attribute>
</definition>
上面的页面将包含父页面和子页面的属性
模板可能如下所示
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<tiles:importAttribute name="stylesheets_top" />
<tiles:importAttribute name="javascripts_bottom" />
<!DOCTYPE html>
<html lang="en">
<head>
<title><tiles:insertAttribute name="title" /></title>
</head>
<body>
<header>
<!-- stylesheets top -->
<c:forEach var="css" items="${stylesheets_top}">
<link rel="stylesheet" type="text/css" href="<c:url value="${css}"/>">
</c:forEach>
</header>
<div class="body">
<tiles:insertAttribute name="body" />
</div>
<footer class="footer">
<tiles:insertAttribute name="footer" />
</footer>
</body>
<!-- scripts_bottom -->
<c:forEach var="script" items="${javascripts_bottom}">
<script src="<c:url value="${script}"/>"></script>
</c:forEach>
</script>
</html>