Spring MVC:Apache Tile 不工作
Spring MVC: Apache Tile don´t work
我正在用 spring mvc 和 apache tiles 做一个网络应用程序,我有下一个:
项目
|__WebContent
|__WEB-INF
|___ spring
|__app-config.xml
|___ views
|__footer.jsp
|__header.jsp
|__home.jsp
|__layout.jsp
|__menu.jsp
|___ tiles.xml
|___ web.xml
|__index.jsp
在应用中-config.xml:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
我的layout.jsp:
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
<div><tiles:insertAttribute name="header" /></div>
<div style="float:left;padding:10px;width:15%;"><tiles:insertAttribute
name="menu" /></div>
<div style="float:left;padding:10px;width:80%;border-left:1px solid pink;">
<tiles:insertAttribute name="body" /></div>
<div style="clear:both"><tiles:insertAttribute name="footer" /></div>
</body>
</html>
我的tiles.xml:
<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/view/layout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/WEB-INF/view/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/view/menu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/view/footer.jsp" />
</definition>
<definition name="home" extends="base.definition">
<put-attribute name="title" value="Home" />
<put-attribute name="body" value="/WEB-INF/view/home.jsp" />
</definition>
</tiles-definitions>
我的控制器:
@Controller
public class HomeController {
@RequestMapping(value="/home")
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
return new ModelAndView("home");
}
当我输入 http://localhost:8080/unqui.tip.vista/home.html:
时出现错误
http 404 /unqui.tip.vista/WEB-INF/view/layout.jsp
Ty.
这可能是您编写时的错字,但您 tiles.xml 中正文的值应该是这样的:
<put-attribute name="body" value="/WEB-INF/views/home.jsp" />
由于您的 home.jsp 在 views 文件夹中,您需要在末尾加上 s。您需要更新所有 tiles.xml 路径以显示视图。
我正在用 spring mvc 和 apache tiles 做一个网络应用程序,我有下一个:
项目
|__WebContent
|__WEB-INF
|___ spring
|__app-config.xml
|___ views
|__footer.jsp
|__header.jsp
|__home.jsp
|__layout.jsp
|__menu.jsp
|___ tiles.xml
|___ web.xml
|__index.jsp
在应用中-config.xml:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
我的layout.jsp:
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
</head>
<body>
<div><tiles:insertAttribute name="header" /></div>
<div style="float:left;padding:10px;width:15%;"><tiles:insertAttribute
name="menu" /></div>
<div style="float:left;padding:10px;width:80%;border-left:1px solid pink;">
<tiles:insertAttribute name="body" /></div>
<div style="clear:both"><tiles:insertAttribute name="footer" /></div>
</body>
</html>
我的tiles.xml:
<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/view/layout.jsp">
<put-attribute name="title" value="" />
<put-attribute name="header" value="/WEB-INF/view/header.jsp" />
<put-attribute name="menu" value="/WEB-INF/view/menu.jsp" />
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/WEB-INF/view/footer.jsp" />
</definition>
<definition name="home" extends="base.definition">
<put-attribute name="title" value="Home" />
<put-attribute name="body" value="/WEB-INF/view/home.jsp" />
</definition>
</tiles-definitions>
我的控制器:
@Controller
public class HomeController {
@RequestMapping(value="/home")
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
return new ModelAndView("home");
}
当我输入 http://localhost:8080/unqui.tip.vista/home.html:
时出现错误http 404 /unqui.tip.vista/WEB-INF/view/layout.jsp
Ty.
这可能是您编写时的错字,但您 tiles.xml 中正文的值应该是这样的:
<put-attribute name="body" value="/WEB-INF/views/home.jsp" />
由于您的 home.jsp 在 views 文件夹中,您需要在末尾加上 s。您需要更新所有 tiles.xml 路径以显示视图。