简体   繁体   中英

apache tiles issue in spring MVC( view rendering path instead of content)

I am working on my first spring MVC project with apache tiles. However, I am having an issue with what is being rendered to view.

Instead of contents from index.jsp (ie, Hello from spring MVC exclusive) to be rendered it renders the path (WEB-INF/jsp/index.jsp) and for footer it renders (WEB-INF/layouts/footer.jsp) instead of

© laxmon philip

Please see attached screenshot of my output. localhost:8080 output screenshot

This is my first spring MVC with apache tiles. Please let me know what I am ding wrong here.

My tiles-definition as follows

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 template

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

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

&copy; laxmon philip

index.jsp page

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

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";
}
}

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: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"> 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>

Why is the path getting delivered in view( for body and footer) instead of content in index.jsp?

My suggestion is to add a forward slash before your : /WEB-INF. in your tile definitions as shown below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM