我有一个表单,可以在春天将值提交给控制器。
这就是我的 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'
请帮我解决这个问题。
最好的祝福