Spring MVC中的apache tiles问题(查看渲染路径而不是内容)

时间:2016-10-26 14:53:59

标签: java spring jsp spring-mvc tiles

我正在使用apache tiles开发我的第一个spring MVC项目。但是,我对要渲染的内容存在问题。

而不是来自index.jsp的内容(即,来自spring MVC的Hello)将呈现它渲染路径(WEB-INF / jsp / index.jsp)以及它呈现的页脚(WEB-INF / layouts / footer) .jsp)而不是

©laxmon philip

请参阅我输出的附件截图。 https://www.playframework.com/documentation/2.5.x/JavaWebSockets#handling-websockets-using-callbacks

这是我的第一个带有apache磁贴的spring MVC。请让我知道我在这里错了什么。

我的瓷砖定义如下

general.xml

    <?xml version="1.0" encoding="ISO-8859-1" ?>
<!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="common" template="WEB-INF/layouts/classic.jsp">
  <put-attribute name="footer" value="WEB-INF/layouts/footer.jsp" />
</definition>

  <definition name="index" extends="common">
  <put-attribute name="title" value="JAVA BLOG AGGREGATOR" />
  <put-attribute name="body" value="WEB-INF/jsp/index.jsp" />
</definition>
</tiles-definitions>

classic.jsp模板

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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:getAsString name="title"/></title>
</head>
<body>

<tiles:insertAttribute name="body"/>

<br><br>
<center>
<tiles:insertAttribute name="footer"/>
</center>

</body>
</html>

footer.jsp模板

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

&copy; laxmon philip

index.jsp页面

 <%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!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">
</head>

<h2>Hello World From Spring MVC</h2>

</html>

IndexController.java类

package cn.laxi.jaxrs.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {

@RequestMapping("/index")
    public String index(){
  return "index";
}
}

分配器一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:context="http://www.springframework.org/schema/context"
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-4.3.xsd">

<context:component-scan base-package="cz.laxi.org.controllers"></context:component-scan>

<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
  <property name="definitions">
    <list>
        <value>/WEB-INF/defs/general.xml</value>
    </list>
</property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</bean>
</beans>

的pom.xml

<properties>
  <org.springframework.version>4.3.3.RELEASE</org.springframework.version>
   <apache.tiles>3.0.7</apache.tiles>
</properties>

<dependencies>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.7.6</version>
</dependency>
<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-core</artifactId>
    <version>${apache.tiles}</version>
</dependency>

<dependency>
    <groupId>org.apache.tiles</groupId>
    <artifactId>tiles-jsp</artifactId>
    <version>${apache.tiles}</version>
</dependency>

的web.xml

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd“version =”3.1“&gt;      javabloggers            的index.html      index.htm的         的index.jsp      default.html中         的default.htm      的Default.jsp     

<servlet>
    <servlet-name>Dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
 </servlet>

  <servlet-mapping>
    <servlet-name>Dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.xml</url-pattern>

  </servlet-mapping>
</web-app>

为什么路径在视图中传递(对于正文和页脚)而不是index.jsp中的内容?

1 个答案:

答案 0 :(得分:0)

我的建议是在您的磁贴定义中的/WEB-INF.之前添加正斜杠,如下所示。

 <?xml version="1.0" encoding="ISO-8859-1" ?>
<!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="common" template="/WEB-INF/layouts/classic.jsp">
  <put-attribute name="footer" value="/WEB-INF/layouts/footer.jsp" />
</definition>

  <definition name="index" extends="common">
  <put-attribute name="title" value="JAVA BLOG AGGREGATOR" />
  <put-attribute name="body" value="/WEB-INF/jsp/index.jsp" />
</definition>
</tiles-definitions>