0

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

这就是我的 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'

请帮我解决这个问题。

最好的祝福

4

3 回答 3

2

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

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

您还需要使用诸如等标签<form:form><form:input>请查看Spring MVC 文档

如果您使用 html 标签<form>,则必须在您的帖子 URL 之前添加上下文根,例如

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

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

于 2013-03-18T16:38:11.697 回答
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 文件中

于 2013-03-18T16:42:06.773 回答
0

我犯了一个非常愚蠢的错误,忘了补充@Controller是什么让我见鬼去吧。

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

于 2013-03-18T22:45:31.183 回答