如何修复SpringMVC中的“noHandlerFound”错误

huangapple go评论103阅读模式
英文:

How to fix "noHandlerFound" in SpringMVC

问题

以下是 spring-mvc-demo-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"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!-- 步骤 3: 添加组件扫描支持 -->
	<context:component-scan base-package="com.robert.springdemo" />

	<!-- 步骤 4: 添加支持转换、格式化和验证的支持 -->
	<mvc:annotation-driven/>
	<!-- 步骤 5: 定义 Spring MVC 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>

</beans>

以下是 web.xml 文件的翻译:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	id="WebApp_ID" version="3.1">

	<display-name>spring-mvc-demo</display-name>

	<absolute-ordering />

	<!-- Spring MVC 配置 -->

	<!-- 步骤 1: 配置 Spring MVC Dispatcher Servlet -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- 步骤 2: 设置 Spring MVC Dispatcher Servlet 的 URL 映射 -->
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

以下是控制器 Java 文件的翻译:

public class HelloWorldController {

	// 需要一个控制器方法来显示初始的 HTML 表单
	@RequestMapping("/showForm")
	public String showForm() {
		return "helloworld-form";
	}
}

当我尝试在服务器上运行它时,我会得到以下错误:

org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /spring-mvc-demo/showForm

我是 Spring MVC 的新手,我认为这与组件扫描有关,但我已在 XML 文件中添加了组件扫描。

谢谢。

英文:

Here's the spring-mvc-demo-servlet.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; 
	xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
	xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
	xsi:schemaLocation=&quot;
		http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd&quot;&gt;

	&lt;!-- Step 3: Add support for component scanning --&gt;
	&lt;context:component-scan base-package=&quot;com.robert.springdemo&quot; /&gt;

	&lt;!-- Step 4: Add support for conversion, formatting and validation support --&gt;
	&lt;mvc:annotation-driven/&gt;
	&lt;!-- Step 5: Define Spring MVC view resolver --&gt;
	&lt;bean
		class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
		&lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/view/&quot; /&gt;
		&lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&gt;
	&lt;/bean&gt;

&lt;/beans&gt;

Here's the web.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
	xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot;
	id=&quot;WebApp_ID&quot; version=&quot;3.1&quot;&gt;

	&lt;display-name&gt;spring-mvc-demo&lt;/display-name&gt;

	&lt;absolute-ordering /&gt;

	&lt;!-- Spring MVC Configs --&gt;

	&lt;!-- Step 1: Configure Spring MVC Dispatcher Servlet --&gt;
	&lt;servlet&gt;
		&lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
		&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
		&lt;init-param&gt;
			&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
			&lt;param-value&gt;/WEB-INF/spring-mvc-demo-servlet.xml&lt;/param-value&gt;
		&lt;/init-param&gt;
		&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
	&lt;/servlet&gt;

	&lt;!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet --&gt;
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
	
&lt;/web-app&gt;

Here's the Controller java file

public class HelloWorldController {

	// need a controller method to show the initial HTML form
	@RequestMapping(&quot;/showForm&quot;)
	public String showForm() {
		return &quot;helloworld-form&quot;;
	}
}

When I try to run it on the server I'll get this error:

org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /spring-mvc-demo/showForm

I'm new to SpringMVC, I thought it has something to do with component scanning but I've added component-scan in the xml file.

Thanks.

答案1

得分: 0

尝试这样做:

@RequestMapping("/spring-mvc-demo")
public class HelloWorldController {

    // 需要一个控制器方法来显示初始的HTML表单
    @RequestMapping("/showForm")
    public String showForm() {
        return "helloworld-form";
    }
}

使用当前的设置,您需要访问GET /showForm,然后它将起作用。

请还检查其他映射方式,如GetMappingPostMappingPutMappingDeleteMappingPatchMapping等等。

英文:

Try this:

@RequestMapping(&quot;/spring-mvc-demo&quot;)
public class HelloWorldController {

    // need a controller method to show the initial HTML form
    @RequestMapping(&quot;/showForm&quot;)
    public String showForm() {
        return &quot;helloworld-form&quot;;
    }
}

With your current setup you need to hit GET /showForm then it will work.

Please also check other ways of mapping like, GetMapping, PostMapping, PutMapping, DeleteMapping, PatchMapping and others

答案2

得分: 0

将原始的JRE库更改为Eclipse中的JRE系统库。

英文:

Changing the original JRE library to JRE System Library in eclipse

huangapple
  • 本文由 发表于 2020年7月31日 20:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63192015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定