java - spring-mvc 中未调用 Controller

标签 java spring-mvc

我是 spring-mvc 的初学者。我试图使用 spring-mvc 创建一个登录页面。但我的 Controller 没有在提交按钮上调用。我收到 404 错误。

登录.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorator="layout/default">
    <head lang="en">
        <title>User Login</title>
    </head>
    <body>        
        <form method="POST" action="/SpringApplication/postLogin" >
            <table>
                <tr><td>User Name: </td><td><input name="userName" type="textbox"></td></tr>
                <tr><td>Password: </td><td><input name="password" type="password"></td></tr>
                <tr><td colspan="2" align="right"><input type="submit" value="Submit"></td></tr>
            </table>
            <div style="color:red">${error}</div>
        </form>
    </body>
</html>

LoginController.java

package core.controllers;

import javax.servlet.http.HttpServletRequest;
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 LoginController {

    
    @RequestMapping(value = "/SpringApplication/postLogin", method = RequestMethod.POST)
    public String postSearch(HttpServletRequest request, Model model) {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        if (userName.isEmpty() || password.isEmpty()) {
            model.addAttribute("error", "Please enter some value!");
            return "redirect:/";
        }
        model.addAttribute("msg", "success");
        return "resultPage";
    }
}

dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="login.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="login" />

</beans>

项目结构

enter image description here

最佳答案

您的表单上的操作属性错误。

Controller 正在等待“/SpringApplication/postLogin”请求,并且表单正在提交到“/SpringApplication/postSearch”

关于java - spring-mvc 中未调用 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36081443/

相关文章:

java - 如何使用 Selenium Java 打开 Chrome 浏览器最大化并通过 ChromeOptions 和 addArguments 抑制信息栏

java - Spring - java.io.FileNotFoundException

java - Hibernate 验证和 Spring Controller 的一个事务

java - spring mvc 3 与 hibernate (和 hibernate.hbm2ddl.auto)

java - Java Hashmap 的类型系统是什么?

java - 将 Java 嵌入到 C++ 应用程序中?

java - JButtons 和 JMenuItems 在 ActionListener 中不合作

java - BasicDataSource 的转换异常

java - Spring Restful支持json数据多语言

java - 在 Spring Mvc Java 中记录未处理的异常