java - org.springframework.web.servlet.DispatcherServlet noHandlerFound警告: No mapping for GET/springMVCDemo/createAccount. html

标签 java spring spring-mvc

这是我的包裹:


       package com.demo.controllers;

        import java.util.Random;

        import org.springframework.stereotype.Controller;
        import org.springframework.ui.Model;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;

        @Controller
        public class MyDemoController {

        private String[] quotes = {"To be or not to be -Shakespeare", "Spring is the nature's way of 
         saying 'let's party'-from someone","The time is always right to do what is right"};



              @RequestMapping(value="/getQuote")
              public String getRandomQuote(Model model) {

              int rand = new Random().nextInt(quotes.length); String randomQuote =quotes[rand];

              model.addAttribute("randomQuote", randomQuote);

              return "quote"; }



            @RequestMapping(value="/createAccount")
            public String createAccount() {

                return "createAccount";
            }
        }

这是我的 web.xml:



    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        version="3.0">

        <display-name>My Demo App</display-name>
        <servlet>
            <servlet-name>MyDemoApp</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/config/myDemoApp-servletConfig.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>

        </servlet>

        <servlet-mapping>
            <servlet-name>MyDemoApp</servlet-name>
            <url-pattern>*.html</url-pattern>
        </servlet-mapping>


    </web-app>

*****servlet.config*****


    <?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:context="http://www.springframework.org/schema/context"   xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd      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-4.3.xsd">

        <mvc:annotation-driven />

        <context:component-scan         base-package="com.demo.controllers"></context:component-scan>
                    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                 <property name="prefix" value="/WEB-INF/jsp/"></property>
                 <property name="suffix" value=".jsp"></property>       </bean>

        <bean       class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />    <bean name="/getQuote.html"         class="com.demo.controllers.MyDemoController" />    <bean name="/createAccount.html"        class="com.demo.controllers.MyDemoController" />

    </beans>

我的报价.jsp



    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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>My Random Quote page</title>
    </head>
    <body>

        <h1>The Quote is:</h1>
        <p>${randomQuote}</p>
    </body>
    </html>

我的createAccount.jsp



    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Create an Account</title>
    </head>
    <body>

    <h1>Enter Account Details</h1>
    <h2>Enter your details</h2>

    <form>

    <table>
      <tr><td>First Name:<input type="text" name= "firstname" /></td></tr>
      <tr><td>Last Name:<input type="text" name= "lastname" /></td></tr>
      <tr><td>Address:<input type="text" name= "address" /></td></tr>
      <tr><td>Email:<input type="text" name= "email" /></td></tr>
      <tr><td><input type="submit" value= "Create " /></td></tr>
      </table>


    </form> 
    </body>
    </html>

  1. 我获取了 localhost:8080/springMVCDemo/getQuote.html 的网页
  2. 但是我没有看到以下页面 本地主机:8080/springMVCDemo/createAccount.html

  3. 任何线索都会有所帮助

最佳答案

错误背后的原因是 Tomcat,我们需要构建并重新发布才能正确加载所有页面

关于java - org.springframework.web.servlet.DispatcherServlet noHandlerFound警告: No mapping for GET/springMVCDemo/createAccount. html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62453955/

相关文章:

java - ProcessBuilder:在不阻塞主线程的情况下转发已启动进程的 stdout 和 stderr

java - 无界通配符参数化类型数组的实际用法是什么?

java - Spring mvc 形式一对多形式

java - Spring MVC 的有效 AJAX 请求

java - 聊天记录转发消息为空 ArrayList

java - 当打开 RTC 'Jazz Administration' 透视图时,Eclipse JVM 退出代码 127

java - Spring Profile 不适用于 Aspect 注释

java - Spring Bean 注解名称属性的用途是什么?

java - 在 Spring Boot 中是否有自定义部署路径的标准方法?

javascript - 测试类无法读取consul配置