Java 在名为“main”的 DispatcherServlet 中找不到带有 URI...的 HTTP 请求的映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25226314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No mapping found for HTTP request with URI.... in DispatcherServlet with name 'main'
提问by rajeev pani..
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
我正在研究 spring mvc,我正在使用“spring in practice”这本书,这是我尝试的第一个 mvc 程序,但出现以下错误
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
下面是我的代码
RosterController
名册控制器
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));
}
}
Member.java
会员.java
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;
}
}
main-servlet.xml
主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"
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.xml
网页.xml
<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>
list.jsp
列表.jsp
<%@ 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>
member.jsp
会员.jsp
<%@ 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.
上面的代码和书上的代码差不多,但是还是找不到哪里出错了我的项目结构截图。
采纳答案by Shinichi Kai
You need to add *
after /roster/
in your bean definition.
您需要在 bean 定义中添加*
after /roster/
。
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
和访问 /MVC_BOOK_Annotation/main/roster/list.do
回答by Mahesh Jayachandran
You are using annotations
to search your controller
. You should be using the below code.
您正在使用annotations
搜索您的controller
. 您应该使用以下代码。
context:component-scan base-package="com"
tag in your Main-servlet
在你的标签 Main-servlet
回答by specializt
You are using @RequestMapping
without value
-parameter, that way requests to your path wont get catched.
您使用的是@RequestMapping
不带value
-parameter 的,这样就不会捕获到您的路径的请求。
How to use @requestMapping headers?
I highly recommend reading all of this, without that knowledge you wont get far ...
我强烈建议您阅读所有这些内容,没有这些知识,您将无法走多远......
回答by Vardhini
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>