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>