在spring-mvc

时间:2016-03-18 10:03:51

标签: java 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>

项目结构

enter image description here

2 个答案:

答案 0 :(得分:0)

您按钮的操作是

action="/SpringApplication/postSearch"

当您的控制器映射

value = "/SpringApplication/postLogin"

尝试将控制器的映射更改为

value = "/SpringApplication/postSearch"

编辑:你好像修复了上述问题。但是你的控制器想要返回一个名为"resultPage"的jsp文件,这个文件在你的jsp文件夹中找不到。

答案 1 :(得分:0)

form上的操作属性错误。

Controller正在等待"/SpringApplication/postLogin"请求,表单正在提交给"/SpringApplication/postSearch"