Spring 未找到 Web MVC Tiles 属性 'header'

Spring Web MVC Tiles Attribute 'header' not found

我正在尝试在我的 spring MVC 项目中使用 tiles 库。

wel-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"
    xmlns:mvc="http://www.springframework.org/schema/mvc">

    <context:annotation-config />
    <context:component-scan base-package="com.smart" />
    <mvc:annotation-driven />
    <tx:annotation-driven transaction-manager="myTransactionManager" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/theme1/" />

    <bean id="tilesViewResolver"
        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>

</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="login" template="/view/home.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/view/header.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="" />
    </definition>
</tiles-definitions>

home.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<html>
<head>
    <Title>Home</Title>
</head>
<body>
<tiles:insertAttribute name="header" />
</body>
</html>

ProfilePage.java

@Controller
public class ProfilePage {

        @RequestMapping(value = "/login", method=RequestMethod.GET)
    public ModelAndView showPost(@ModelAttribute("commentForm") Post post, ModelMap model, HttpSession session)
    {
             return new ModelAndView("home", model);
        }

        @RequestMapping(value = "/login", method=RequestMethod.POST)
    public ModelAndView loginWorld(ModelMap model, @ModelAttribute("commentForm") Post post, @ModelAttribute("loginForm") Employee employee, HttpSession session, BindingResult errorResult){

             // Logic

                model.addAttribute("result", result);
        return new ModelAndView(returnPage, model);
    }
}

错误:

org.apache.jasper.JasperException: org.apache.tiles.template.NoSuchAttributeException: Attribute 'header' not found.
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

org.apache.tiles.template.NoSuchAttributeException: Attribute 'header' not found.
    org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:50)
    org.apache.tiles.template.InsertAttributeModel.resolveAttribute(InsertAttributeModel.java:165)
    org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:121)
    org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
    org.apache.jsp.view.home_jsp._jspx_meth_tiles_005finsertAttribute_005f0(home_jsp.java:379)
    org.apache.jsp.view.home_jsp._jspx_meth_c_005fif_005f0(home_jsp.java:318)
    org.apache.jsp.view.home_jsp._jspService(home_jsp.java:147)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

我不明白为什么会出现这个错误!!

首先将您的 'view' 文件夹移动到 'WEB-INF' 目录并更改以下映射

尝试更改 tiles.xml

中的以下映射
<definition name="login" template="/view/home.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header" value="/view/header.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="" />
</definition>

以下

<definition name="base.definition" template="/WEB-INF/view/template.jsp">
    <put-attribute name="title" value="" />
    <put-attribute name="header" value="/WEB-INF/view/header.jsp" />
    <put-attribute name="body" value="" />
    <put-attribute name="footer" value="" />
</definition>


<definition name="home" extends="base.definition">
<put-attribute name="title" value="Welcome" />
<put-attribute name="body" value="/WEB-INF/view/home.jsp" />
</definition>

The return value of every controller will be mapped with each tiles definition which is associated with jsp to render as body in template. This is why we need to add mapping for 'home'.

创建template.jsp如下

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<html>
<head>
    <Title>template</Title>
</head>
<body>
<tiles:insertAttribute name="header" />
</body>
</html>

那么你的home.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>

<html>
<head>
    <Title>Home</Title>
</head>
<body>
</body>
</html>

这里 home.jsp 您不需要插入属性 'header'。 tiles 会为你做这件事。

同时删除 InternalResourceViewResolver

如下更改您的磁贴:

<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/view/mainTemplate.jsp">
    <put-attribute name="title" value=""></put-attribute>
    <put-attribute name="header" value="/WEB-INF/view/header.jsp"></put-attribute>      
    <put-attribute name="body" value=""></put-attribute>
    <put-attribute name="footer" value="/WEB-INF/view/footer.jsp"></put-attribute>
</definition>
<definition extends="base.definition" name="home">
    <put-attribute name="title" value="Home Page"></put-attribute>
    <put-attribute name="body" value="/WEB-INF/view/home.jsp"></put-attribute>
</definition>

创建/WEB-INF/view/mainTemplate.jsp

<%@ 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" ignore="true" /></title>
</head>
<body>

  <div id="header">
     <!-- Header Tiles -->
     <tiles:insertAttribute name="header" />
  </div>


  <div id="section">
     <!-- Body Tiles -->
     <tiles:insertAttribute name="body" />
  </div>

  <div id="footer">
     <tiles:insertAttribute name="footer" />
  </div>
  <!-- footer Tiles -->    
</body>
</html>

然后创建您的 /WEB-INF/view/home.jsp。 home.jsp 的内容将自动进入 mainTemplate.jsp

的正文