1
votes

I've written "Hello World" website with Spring 3 MVC, now I'm trying to rewrite it using Apache Tiles technology. I'm using Maven to build my project. pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>michat</groupId>
<artifactId>WebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>WebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
    <spring.version>3.2.4.RELEASE</spring.version>
    <jdk.version>1.7</jdk.version>
    <tiles.version>3.0.0</tiles.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-core</artifactId>
        <version>${tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>${tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-template</artifactId>
        <version>${tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>${tiles.version}</version>
    </dependency>
</dependencies>
<build>
    <finalName>WebApp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<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>/</url-pattern>
</servlet-mapping>

dispatcher.servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.michat.controller" />
<bean class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />
<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

tiles.xml

<!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="base" template="/WEB-INF/views/layout.jsp">
                <put-attribute name="header" value="/WEB-INF/views/header.jsp" />
            <put-attribute name="content" value="" />
            <put-attribute name="footer" value="/WEB-INF/views/footer.jsp" />
        </definition>
        <definition name="home" extends="base">
            <put-attribute name="content" value="/WEB-INF/views/home.jsp" />
        </definition>
    </tiles-definitions>

HomeController.java

package com.michat.controller;

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

@Controller
public class HomeController {

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

I have footer.jsp, home.jsp, header.jsp and layout.jsp defined in views directory in WEB-INF. Why am I getting this error?

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

Apache Tomcat/7.0.35
1
Which resource are you requesting? What's your url?Sotirios Delimanolis
localhost:8080/WebApp , I have controller mapped on "/"Michał Tabor
From what you've posted, we can't tell if you have any controller handler methods. Do you?Sotirios Delimanolis
you have post you tiles.xmlJaiwo99

1 Answers

1
votes

Please add to your dispatcher.servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        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-3.2.xsd">

<context:annotation-config />
<mvc:annotation-driven />
<context:component-scan base-package="com.michat" />

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

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

This

<context:annotation-config/>

will activate various annotations like @Required and @Autowired

This

<mvc:annotation-driven />

is needed to have annotation driven controller.

Below configuration simply scans the classpath for annotated components.

<context:component-scan base-package="com.antenna.nn.patient" />

UrlBasedViewResolver is viewResolver that allows for direct resolution of symbolic view names to URLs. Tiles is needs this.