Spring Web MVC Tiles属性'标题'未找到

时间:2016-02-17 04:10:55

标签: java spring jsp spring-mvc tiles

我正在尝试在我的Spring MVC项目中使用tile库。

WEL-servlet.xml中

400 OK
- SHOW HEADERS -
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Invalid Value",
    "locationType": "parameter",
    "location": "q"
   }
  ],
  "code": 400,
  "message": "Invalid Value"
 }
}

tiles.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>

针对home.jsp

<?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>

ProfilePage.java

<%@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>

错误:

@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);
    }
}

我不知道为什么会出现这个错误!!

2 个答案:

答案 0 :(得分:1)

首先将'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>
  

每个控制器的返回值将与每个图块一起映射   与jsp相关联的定义,以在模板中呈现为主体。   这就是为什么我们需要为'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'。瓷砖会为你做这件事。

并删除InternalResourceViewResolver

答案 1 :(得分:1)

更改您的图块如下:

<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

的主体中