Reputation: 5637
I am newly working on spring mvc,I am using "spring in practice" book and this is the first mvc programme which I tried and I am getting the below error
WARN PageNotFound:1101 - No mapping found for HTTP request with URI [/MVC_BOOK_Annotation/main/list.do] in DispatcherServlet with name 'main'
below is my code
package com.web;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.model.Member;
@Controller
public final class RosterController {
private List<Member> members = new ArrayList<Member>();
public RosterController() {
members.add(new Member("John", "Lennon"));
members.add(new Member("Paul", "McCartney"));
members.add(new Member("George", "Harrison"));
members.add(new Member("Ringo", "Starr"));
}
@RequestMapping
public void list(Model model) {
model.addAttribute(members);
}
@RequestMapping
public void member(@RequestParam("id") Integer id, Model model) {
model.addAttribute(members.get(id));
}
}
package com.model;
public class Member {
private String firstName;
private String lastName;
public Member() {
}
public Member(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
return firstName + " " + lastName;
}
}
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
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-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean class="com.web.RosterController" name="/roster/"></bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
</bean>
</beans>
<web-app id="AppName" version="2.4" 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">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>main</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/main/*</url-pattern>
</servlet-mapping>
</web-app>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<ul>
<c:forEach var="member" items="${memberList}" varStatus="status">
<li><a href="member.do?id=${status.index}"> <c:out
value="${member}"></c:out>
</a></li>
</c:forEach>
</ul>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Member: ${member}</h1>
<p>
<a href="list.do">Back</a>
</p>
</body>
</html>
the above code is almost similar to the book code but still I am unable to find where is the mistake.I attached an @RequestMapping attribute to the list() and member() methods,This identifies them as request-servicing methods.Below is my project structure screenshot.
Upvotes: 1
Views: 23843
Reputation: 353
You need to add the below mapping:
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<!-- <resources mapping="/resources/**" location="/resources/" /> -->
<!-- <mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/public-web-resources/"/> -->
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- View Handler -->
<beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<beans:property name="favorPathExtension" value="true"/>
<beans:property name="mediaTypes">
<beans:map>
<beans:entry key="xml" value="text/xml"/>
<beans:entry key="json" value="application/json"/>
<beans:entry key="html" value="text/html"/>
<beans:entry key="less" value="text/html"/>
</beans:map>
</beans:property>
<beans:property name="viewResolvers">
<beans:list>
<beans:bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
</beans:bean>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/"/>
</beans:bean>
</beans:list>
</beans:property>
</beans:bean>
Upvotes: 0
Reputation: 129
You are using annotations
to search your controller
. You should be using the below code.
context:component-scan base-package="com"
tag in your Main-servlet
Upvotes: 0
Reputation: 4523
You need to add *
after /roster/
in your bean definition.
Try following setting:
<bean class="com.web.RosterController" name="/roster/*"></bean>
instead of
<bean class="com.web.RosterController" name="/roster/"></bean>
And access /MVC_BOOK_Annotation/main/roster/list.do
Upvotes: 3
Reputation: 1911
You are using @RequestMapping
without value
-parameter, that way requests to your path wont get catched.
How to use @requestMapping headers?
I highly recommend reading all of this, without that knowledge you wont get far ...
Upvotes: 2