可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has an answer here:
-
Why does Spring MVC respond with a 404 and report “No mapping found for HTTP request with URI […] in DispatcherServlet”?
4 answers
I checked out nearly every relevant article on stackoverflow already, but I just cant fix my problem.
Here is the code:
web.xml:
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>/</url-pattern>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml:
<context:component-scan base-package="com.mycompany.elso" />
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
myController:
public class myController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
Web Pages/index.jsp:
<html>
<head>
<title>Spring 3.0 MVC Series</title>
</head>
<body>
<a href="hello.html">Say Hello</a>
</body>
</html>
Web Pages/WEB-INF/jsp/hello.jsp:
<html>
<head>
<title>Spring 3.0 MVC Series: Hello World - ViralPatel.net</title>
</head>
<body>
${message}
</body>
</html>
So when i launch the appication the index.jsp is loaded correctly but when i click on the href to navigate to hello.jsp i got a 404 error and the server log says:
No mapping found for HTTP request with URI [/Elso/hello.html] in DispatcherServlet with name 'spring'
I've checked out dozens of articles like that, but I just can't find the mistake, anybody has any idea what could it be?
回答1:
You could try and add an @Controller
annotation on top of your myController Class and
try the following url /<webappname>/my/hello.html
.
This is because org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping
prepends /my to each RequestMapping in the myController class.
回答2:
Add
<mvc:default-servlet-handler/>
to spring-servlet.xml
回答3:
If you are using
<mvc:annotation-driven/>
make sure your spring-servlet.xml has correct
<context:component-scan base-package="com.....controller" /> tag.
Basically, you need to include all the packages where you have used the annotation in your java code.
Also, please ensure you do not have duplication of component-scan (for a discovery of beans). If your config XML already contains the element, then any of your Controller classes that are annotated with @ComponentScan(basePackages=... needs to be stripped of the said annotation.
回答4:
I solved my issue with :
Java Build Path -> JRE system library - > Edit -> Alternate JRE -> -> Finish
As it was configured to JDK folder so it was giving Exception
回答5:
Try:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Worked for me!
回答6:
Please check your [PROJECT_NAME]\target\classes
directory to see whether myController.class
is generated or not.
If not, please check all your java source code whether there are any compilation errors.
回答7:
Make sure
<mvc:annotation-driven/>
<context:component-scan base-package="com.hireartists.web.controllers"/>
points to proper package that contains controllers.
回答8:
If you are using Java code based on Spring MVC configuration then enable the DefaultServletHandlerConfigurer
in the WebMvcConfigurerAdapter
object.
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
回答9:
Check ur Bean xmlns..
I also had similar problem, but I resolved it by adding mvc xmlns.
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="net.viralpatel.spring3.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
回答10:
addition of <mvc:annotation-driven/>
worked for me.
Add it before line <context:component-scan ............/>
回答11:
If you want to serve .html files, you must add this <mvc:default-servlet-handler />
in your spring config file. .html files are static.
Hope that this can help someone.
回答12:
Had the exact same error and it took me a long time trying to understand it. It is most likely down to compilation errors, the java classes did not get published in your servlet. Please check this by going in the server that you are using \tmp1\wtpwebapps[PROJECT_NAME]\WEB-INF\classes\ and try to find you controller classes to see whether or not they have been published. If not you need to get to the bottom of any compilation errors.
回答13:
If you are using maven as build tool for project , build your project properly,your changes in the code and xml files are not reflecting after compilations.
回答14:
If you depend on Spring Social, check that you have configured a Web Controller bean:
import org.springframework.context.annotation.Bean;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
...
@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}
回答15:
if you are using maven then do run maven install command before you run your web app on a server as it will generate a class file for your controller and in my experience that is what your application has been missing.
回答16:
I had the same issue and after lots of reserach I found the classes were not getting published in my target folder. So I had run the below two commands from cmd
- mvn clean install
- mvn package
Surprisingly I was able to access the page and error was gone. Same can be verified from target folder where you will be able to find the complied classes which were missing earlier.
回答17:
It is not finding the controllers, this is basic issues.
it can be due to following reasons.
A. inside WEB-INF folder you have file web.xml that refers to dispatcherServlet. Here it this case is mvc-config.xml
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
B. This mvc-config.xml file have namespaces and it has to scan the controllers.
<context:component-scan base-package="org.vimal.spring.controllers" />
<mvc:annotation-driven />
C. Check for the correctness of the package name where you have the controllers. It should work.
All Controllers must be Annotated with @Controller.
回答18:
Add @Controller
to your controller or where ever you have the @RequestMapping
for you json end point.
This worked for me while deploying a similar application.
回答19:
If you are using Maven ,
Add these to your pom.xml
<dependency>
<groupid>javax.servlet</groupid>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
<dependency>
<groupid>taglibs</groupid>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
回答20:
I also faced the same issue, but after putting the namespace below, it works fine:
xmlns:mvc="http://www.springframework.org/schema/mvc"
回答21:
Removing the Tomcat Server and adding new tomcat configuration in Eclipse resolved issue for me.
回答22:
In pom.xml make sure packaging is set to war
like <packaging>war</packaging>
,not to jar or any thing else.
回答23:
What is /Elso?
You try:
@RequestMapping("/Elso")
public class myController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}