【问题标题】:Spring MVC Controller URL not mappedSpring MVC 控制器 URL 未映射
【发布时间】:2020-01-16 03:01:19
【问题描述】:

我正在尝试在 Spring MVC 中制作简单的项目,但出现此错误:

警告:在名称为“springform”的 DispatcherServlet 中找不到带有 URI [/SpringMVCForm/login.htm] 的 HTTP 请求的映射

HTTP 状态 404 - 未找到。

无法得到我所缺少的。请告诉我。

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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>SpringMVCForm</display-name>
  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <servlet>
    <servlet-name>springform</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springform</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
</web-app>

springform-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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">


<context:component-scan base-package="com.mvcform.*" />

<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

</beans>

LoginController.java

package com.mvcform.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/login.htm")
public class LoginController {

    @RequestMapping(method=RequestMethod.POST)
    public ModelAndView processCredentials(@RequestParam("userId") String userId, @RequestParam("password") String password) {
        String message = "Invalid Credentials";
        if (userId != null && userId.length() >= 5) {
            if (userId.equals(password)) {
                //message = "Welcome " + userId + " !!!";
                return new ModelAndView("redirect:/showform");
            }
        }
        return new ModelAndView("index", "message", message);
    }
}

index.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>Spring MVC Form</title>
</head>
<body>

<h1> Index.jsp </h1>

<h1>Enter your user id and password</h1>
    <form action="login.htm" method="post">
        <table>
            <tr>
                <td> User Id </td>
                <td> <input type="text" name="userId" /></td>
            </tr>
            <tr>
                <td>Password </td>
                <td> <input type="password" name="password" /></td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="Submit" />
                </td>
            </tr>
        </table>
    </form>

</body>
</html>

我认为 Spring 无法扫描基本包:com.mvcform.*

请让我知道我在这里缺少什么。

【问题讨论】:

  • 你可以尝试写 吗? (.* 太多了,这里要包)
  • 我也试过了,但还是不行
  • 提交表单后什么时候出错?
  • 提交表单后是的

标签: spring spring-mvc


【解决方案1】:

1)如果你配置正确那么你必须检查你发送到服务器保存它的json格式可能是错误的(参考urlenter link description here

2) 请检查你的控制器控制器没有这个 url "URI [/SpringMVCForm/login.htm]" 你只有 /login.htm url。

【讨论】:

  • SpringMVCForm 是我的项目的名称,我是否也需要在我的 RequestMapping 中写下它
  • RequestMapping 中不需要,但您必须在点击任何 url 或进行任何 url 调用时使用项目名称。
【解决方案2】:

删除 @RequestMapping("/login.htm")

并且只保留 @RequestMapping("/login")

在控制器中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2018-04-07
    • 2012-05-30
    • 2018-09-24
    • 2011-04-23
    相关资源
    最近更新 更多