提交表单时,春天的HTTP状态404

时间:2013-03-18 16:31:08

标签: java spring spring-mvc login

我有一个表格,在春天将值提交给控制器。

这就是我的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例外,

由于我是春天新手,我无法弄清楚问题,请解释原因。

EDIT :

这是来自服务器控制台:

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

请帮我解决这个问题。

最好的问候

3 个答案:

答案 0 :(得分:2)

在JSP文件中,您需要在开头添加它

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

您还需要使用<form:form><form:input>等标签。请查看Spring MVC documentation

如果您使用html标记<form>,则必须在帖子网址之前添加context-root,如

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

但是,您的视图和支持bean之间很可能需要数据绑定。所以我建议使用<form:form><form:input>,它会自动为您找出上下文根。

答案 1 :(得分:1)

使用${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文件中

答案 2 :(得分:0)

这真是一个愚蠢的错误,忘记添加@Controller,这让我很难道。

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