【问题标题】:HTTP Status 404 in spring while submitting the form春季提交表单时出现 HTTP 状态 404
【发布时间】:2013-03-18 16:31:07
【问题描述】:

我有一个表单,可以在春季将值提交给控制器。

这就是我的 servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.anto.springsec.controllers" />

</beans:beans>

这就是 web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

这就是我的控制器:

package com.anto.springsec.controllers;

import java.security.Principal;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

public class LoginController {

    @RequestMapping(value="/login", method = RequestMethod.POST)
    public String printWelcome( ) {

        return "createContact";
    }

    @RequestMapping(value="/login", method = RequestMethod.GET)
    public String login(ModelMap model) {
        return "login";
    }

    @RequestMapping(value="/loginfailed", method = RequestMethod.GET)
    public String loginerror(ModelMap model) {
        model.addAttribute("error", "true");
        return "login";
    }

}

这就是我的 jsp 文件:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<html>
<head>
<title>Login</title>
<script type="text/javascript">
function login(){
    alert("login");
    document.forms[0].action="login";
    document.forms[0].method="post";
    document.forms[0].submit();
}

</script>
</head>
<body>
    <form class="form-2" action="login" method="post" >

    <table width="100%">
            <tr>
                <td align="center" colspan="2" width="100%"><b>  <h1> <span class="log-in">Login</span></h1></b></td>
            </tr>
            <tr>
                <td align="center" colspan="2" width="100%"></td>
            </tr>
            <tr class="float">
                <td align="right" class="icon-user" width="50%"><label for="login"><i class="icon-user"></i>Username</label> </td>
                <td align="left" class="field" width="50%"><input type="text" name="login" placeholder="Username "></td>
            </tr>
            <tr class="float">
                <td align="right" class="icon-lock" width="50%"><label for="password"><i class="icon-lock"></i>Password</label></td>
                <td align="left" class="field" width="50%"><input type="password" name="password" placeholder="Password" class="showpassword"> </td>
            </tr>
            <tr>
                <td align="right" colspan="2" width="50%"><input type="submit" name="CreateContact" value="Login" /><i class="icon-user"></i></td>
            </tr>
        </table>
        </form>
</body>
</html>

我只想将控件重定向到登录控制器并获取用户名和密码并重定向到某个页面。

但是当我提交请求时,我得到404 异常,

由于我是 spring 新手,我无法找出问题所在,请解释为什么会发生。

EDIT :

这是来自服务器控制台:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/springsec/login] in DispatcherServlet with name 'appServlet'

请帮我解决这个问题。

最好的问候

【问题讨论】:

  • 服务器日志中的任何错误/警告
  • hi Arun 编辑了问题,请看一下
  • 什么是springsec?登录页面也显示在浏览器右侧,只有发布请求不起作用
  • 项目名称是 springsec,是的,我得到了登录页面,我必须提交表单,因为您说未提交发布请求
  • 发布到 ${pageContext.request.contextPath}/login

标签: java spring spring-mvc login


【解决方案1】:

在你的 JSP 文件中,你需要在开头添加这个

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

您还需要使用&lt;form:form&gt;&lt;form:input&gt; 等标签。请查看Spring MVC documentation

如果你使用html标签&lt;form&gt;,你必须在你的帖子URL之前添加上下文根,比如

<form class="form-2" action="/<your-context-root>/login" method="post" >

但是,您很可能需要在视图和支持 bean 之间进行数据绑定。所以我建议使用&lt;form:form&gt;&lt;form:input&gt;,它会自动为你找出上下文根。

【讨论】:

    【解决方案2】:

    使用${pageContext.request.contextPath}/login.. 这将发布到 springsec/login

    现在表格已发送至/login${pageContext.request.contextPath} 会将 springsec 添加到路径中。您的应用程序可通过localhost:8080/springsec访问

    在您的表单中使用

    <form action="${pageContext.request.contextPath}/login" method="post">
    

    或者如果通过 Js 提交。

    document.forms[0].action="${pageContext.request.contextPath}/login";
    document.forms[0].method="post";
    document.forms[0].submit();
    

    注意:只有当它们被写入 JSP 文件时,上述内容才有效。不在 HTML 或 JS 文件中

    【讨论】:

    • WARN 另有说明:No mapping found for HTTP request with URI [/springsec/login],而他的控制器映射到 /login
    • 我已经使用javascript提交了表单,那么如何使用${pageContext.request.contextPath}/login
    • @Sotirios:哦,是的.. 抱歉.. 没看到.. @Anto:只需使用我发布的行.. 它是 jsp 中默认可用的对象..服务器将自动将${pageCon.. 替换为适当的值..
    • 另外,尝试将 @RequestMapping(value="/login") 更改为 @RequestMapping(value="login") 而不使用正斜杠。我认为这是一个绝对路径问题
    【解决方案3】:

    我犯了一个非常愚蠢的错误,忘记添加@Controller 这让我见鬼去吧。

    无论如何感谢您对这个问题的所有贡献。

    【讨论】:

      猜你喜欢
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 2015-08-09
      • 2014-08-01
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      相关资源
      最近更新 更多