当前位置: 动力学知识库 > 问答 > 编程问答 >

java - HTTP Status 404 in spring while submitting the form

问题描述:

i have a form which submits the value to a controller in spring.

this is what is my 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>

this is what 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>

and this is what is my controller :

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";

}

}

and this is what is my jsp file :

<%@ 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>

I just want to redirect the control to login controller and get the username and password and redirect to some page.

but when i submit the request i get 404 exception,

since i am new to spring i am not able to figure out the problem, Please explain why it occurs.

EDIT :

this is from the server console :

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

Please help me to resolve this.

Best Regards

网友答案:

Use ${pageContext.request.contextPath}/login.. This will post to springsec/login

Right now the form is post to /login. ${pageContext.request.contextPath} will add springsec to the path. Your application is accessible through localhost:8080/springsec

In your form use

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

or if submitting by Js.

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

NOTE : above will work only if they are written in a JSP file. Not in HTML or JS files

网友答案:

In your JSP file, you need to add this at the beginning

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

You also need to use tags like <form:form>, <form:input> etc. Please check out Spring MVC documentation.

If you use html tag <form>, you have to add the context-root before your post URL like

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

However, mostly likely you would need data-binding between your view and your backing beans. So I would recommend using <form:form>, <form:input>, which will figure out the context-root for you automatically.

网友答案:

It is really silly mistake i made, forgot to add @Controller which was casting me the hell.

Anyway thanks for all your contribution towards this question.

分享给朋友:
您可能感兴趣的文章:
随机阅读: