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

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

How to fix "noHandlerFound" in SpringMVC

问题

以下是 spring-mvc-demo-servlet.xml 文件的翻译:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  13. <!-- 步骤 3: 添加组件扫描支持 -->
  14. <context:component-scan base-package="com.robert.springdemo" />
  15. <!-- 步骤 4: 添加支持转换、格式化和验证的支持 -->
  16. <mvc:annotation-driven/>
  17. <!-- 步骤 5: 定义 Spring MVC 视图解析器 -->
  18. <bean
  19. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  20. <property name="prefix" value="/WEB-INF/view/" />
  21. <property name="suffix" value=".jsp" />
  22. </bean>
  23. </beans>

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. id="WebApp_ID" version="3.1">
  6. <display-name>spring-mvc-demo</display-name>
  7. <absolute-ordering />
  8. <!-- Spring MVC 配置 -->
  9. <!-- 步骤 1: 配置 Spring MVC Dispatcher Servlet -->
  10. <servlet>
  11. <servlet-name>dispatcher</servlet-name>
  12. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  13. <init-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
  16. </init-param>
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <!-- 步骤 2: 设置 Spring MVC Dispatcher Servlet 的 URL 映射 -->
  20. <servlet-mapping>
  21. <servlet-name>dispatcher</servlet-name>
  22. <url-pattern>/</url-pattern>
  23. </servlet-mapping>
  24. </web-app>

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

  1. public class HelloWorldController {
  2. // 需要一个控制器方法来显示初始的 HTML 表单
  3. @RequestMapping("/showForm")
  4. public String showForm() {
  5. return "helloworld-form";
  6. }
  7. }

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

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

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

谢谢。

英文:

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

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  3. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  4. xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
  5. xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
  6. xsi:schemaLocation=&quot;
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/mvc
  12. http://www.springframework.org/schema/mvc/spring-mvc.xsd&quot;&gt;
  13. &lt;!-- Step 3: Add support for component scanning --&gt;
  14. &lt;context:component-scan base-package=&quot;com.robert.springdemo&quot; /&gt;
  15. &lt;!-- Step 4: Add support for conversion, formatting and validation support --&gt;
  16. &lt;mvc:annotation-driven/&gt;
  17. &lt;!-- Step 5: Define Spring MVC view resolver --&gt;
  18. &lt;bean
  19. class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
  20. &lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/view/&quot; /&gt;
  21. &lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&gt;
  22. &lt;/bean&gt;
  23. &lt;/beans&gt;

Here's the web.xml:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
  4. xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot;
  5. id=&quot;WebApp_ID&quot; version=&quot;3.1&quot;&gt;
  6. &lt;display-name&gt;spring-mvc-demo&lt;/display-name&gt;
  7. &lt;absolute-ordering /&gt;
  8. &lt;!-- Spring MVC Configs --&gt;
  9. &lt;!-- Step 1: Configure Spring MVC Dispatcher Servlet --&gt;
  10. &lt;servlet&gt;
  11. &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
  12. &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  13. &lt;init-param&gt;
  14. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
  15. &lt;param-value&gt;/WEB-INF/spring-mvc-demo-servlet.xml&lt;/param-value&gt;
  16. &lt;/init-param&gt;
  17. &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  18. &lt;/servlet&gt;
  19. &lt;!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet --&gt;
  20. &lt;servlet-mapping&gt;
  21. &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
  22. &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  23. &lt;/servlet-mapping&gt;
  24. &lt;/web-app&gt;

Here's the Controller java file

  1. public class HelloWorldController {
  2. // need a controller method to show the initial HTML form
  3. @RequestMapping(&quot;/showForm&quot;)
  4. public String showForm() {
  5. return &quot;helloworld-form&quot;;
  6. }
  7. }

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

  1. org.springframework.web.servlet.DispatcherServlet noHandlerFound
  2. 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

尝试这样做:

  1. @RequestMapping("/spring-mvc-demo")
  2. public class HelloWorldController {
  3. // 需要一个控制器方法来显示初始的HTML表单
  4. @RequestMapping("/showForm")
  5. public String showForm() {
  6. return "helloworld-form";
  7. }
  8. }

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

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

英文:

Try this:

  1. @RequestMapping(&quot;/spring-mvc-demo&quot;)
  2. public class HelloWorldController {
  3. // need a controller method to show the initial HTML form
  4. @RequestMapping(&quot;/showForm&quot;)
  5. public String showForm() {
  6. return &quot;helloworld-form&quot;;
  7. }
  8. }

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:

确定