簡體   English   中英

控制器未在spring-mvc中調用

[英]Controller not invoked in spring-mvc

我是spring-mvc的初學者。 我試圖使用spring-mvc創建一個登錄頁面。 但是我的控制器沒有在提交按鈕上調用。 我收到404錯誤。

login.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";
    }
}

調度員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>

項目結構

在此輸入圖像描述

你按鈕的動作是

action="/SpringApplication/postSearch"

當您的控制器映射時

value = "/SpringApplication/postLogin"

嘗試將控制器的映射更改為

value = "/SpringApplication/postSearch"

編輯:你似乎修復了上述問題。 但是你的控制器想要返回一個名為"resultPage"的jsp文件,這個文件在你的jsp文件夾中找不到。

您的form上有一個錯誤的操作屬性。

Controller正在等待"/SpringApplication/postLogin"請求,表單正在提交到"/SpringApplication/postSearch"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM