0

I learning Spring MVC from Spring In Action 3rd Edition and trying to implement using my eclipse IDE and deploying the application on Tomcat V6.0

When I am deploying the application and trying to load the home page (http://localhost:8080/SpringInAction3/), I am getting an error message in server logs as:

INFO: Server startup in 1314 ms Dec 28, 2013 2:58:47 PM org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/SpringInAction3/] in DispatcherServlet with name 'spitter'

But my controller handles the requests, here is my controller code:

@Controller
public class HomeController {
    public static final int DEFAULT_SPITTLES_PER_PAGE = 25;
    private SpitterService spitterService;

    @Inject
    public HomeController(SpitterService spitterService) {
        this.spitterService = spitterService;
    }

    @RequestMapping({ "/", "/home" })
    public String showHomePage(Map<String, Object> model) {
        model.put("spittles", spitterService.getRecentSpittles(DEFAULT_SPITTLES_PER_PAGE));
        return "home";
    }
}

So in this controller I am handing requests that are made on "/" and also "/home"

Here is my spitter-servlet.xml file:

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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.xsd">
    <mvc:resources mapping="resources/" location="/resources/" />
    <mvc:annotation-driven />

    <context:component-scan base-package="com.habuma.spitter"/>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver" />

    <bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/views/views.xml</value>
            </list>
        </property>
    </bean>
</beans>

I have defined my service as like this:

@Component
public class SpitterService {

    public List<Spitter> getRecentSpittles(int defaultSpittlesPerPage) {
        Spitter spitter1 = new Spitter("1", "Person1");
        Spitter spitter2 = new Spitter("2", "Person2");
        List<Spitter> spitters = new ArrayList<Spitter>();
        spitters.add(spitter1);
        spitters.add(spitter2);
        return spitters;
    }
}

This is my web.xml file:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <servlet>
        <servlet-name>spitter</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spitter</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Please help me in solving this issue.

Adding home.jsp content:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="t" uri="http://tiles.apache.org/tags-tiles"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<div>
    <h2>A global community of friends and strangers spitting out their
        inner-most and personal thoughts on the web for everyone else to see.</h2>
    <h3>Look at what these people are spitting right now...</h3>
    <ol class="spittle-list">
        <c:forEach var="spittle" items="${spittles}">
            <s:url value="/spitters/{spitterName}" var="spitter_url">
                <s:param name="spitterName" value="${spittle.spitter.username}" />
            </s:url>
            <li><span class="spittleListImage">
                <img src="http://s3.amazonaws.com/spitterImages/${spittle.spitter.id}.jpg"
                    width="48" border="0" align="middle" 
                    onError="this.src='<s:urlvalue="/resources/images"/>/spitter_avatar.png';"/>
                </span>
                <span class="spittleListText">
                    <a href="${spitter_url}">
                        <c:out value="${spittle.spitter.username}" />
                    </a> - <c:outvalue ="${spittle.text}"/> <br/>

                <small> <fmt:formatDate value="${spittle.when}" pattern="hh:mmaMMMd,yyyy" /></small> 
                </span>
            </li>
        </c:forEach>
    </ol>
</div>
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • The name of the eclipse project that I have created is `SpringInAction3`, so the context name will be the same right. – Chaitanya Dec 28 '13 at 09:49
  • Can you try to add ``? – Pavel Horal Dec 28 '13 at 09:49
  • @Pavel, I added that line in my spitter-servlet.xml file, but still getting same error. – Chaitanya Dec 28 '13 at 09:52
  • Just to rule out the simplest case can you please clean your project (Project > Clean) and the server (right click on Tomcat > Clean...). Also please confirm, that the controller is under `com.habuma.spitter` package. And finally, please post servlet mapping from your `web.xml` (it will be probably correct judging on the warning message, but you never know). – Pavel Horal Dec 28 '13 at 09:56
  • @Pavel, I cleaned my eclipse project and also the server by following your steps and re-built the project and deployed it to tomcat once again, but got the same error message. I added the contents of web.xml file in my question, please check. – Chaitanya Dec 28 '13 at 10:09
  • 2
    I would try: `http://localhost:8080/` –  Dec 28 '13 at 10:16
  • @Andreas `/` is the correct way. There is nothing wrong with servlet mapping. Otherwise the Spring's warning would not be there in the first place. – Pavel Horal Dec 28 '13 at 10:16
  • 1
    @PavelHoral, after restarting eclipse and redeploying I am able to get to the home page which is home.jsp in my case. But I am facing a different issue now. It says: `org.apache.jasper.JasperException: The absolute uri: http://tiles.apache.org/tags-tiles cannot be resolved in either web.xml or the jar files deployed with this application`, I already placed the tiles-core-2.2.1.jar, tiles-api-2.1.3.jar and tiles-servlet-2.1.3.jar in my WEB-INF/lib directory. TLD is not there in any of these 3 jars, please let me know from where can I get that? – Chaitanya Dec 28 '13 at 10:37
  • 1
    After adding tiles-jsp and tiles-template jars, I am able to load the home.jsp page. Thanks a lot guys. – Chaitanya Dec 28 '13 at 11:43

0 Answers0