简体   繁体   中英

HTTP Status 404 - /LoginAuth

I am using spring contoller annotation. but when running my application getting HTTP Status 404 - /LoginAuth error

my files are web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >

<display-name>SpringTest</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
     </listener-class>
</listener>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>


</web-app>

index.jsp in this file after clicking on LogIn button it will call LoginController.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="LoginAuth" method="get">
<label>Enter name :</label>
<input type="text" name="uname"><br>
<label>Enter pass :</label>
 <input type="password" name="pass"><br>
<input type="submit" value="LogIn">
</form>

 </body>
 </html>

dispacher-servlet.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
           http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">



<bean
    id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property
        name="prefix"
        value="/WEB-INF/JSP/" />

    <property
        name="suffix"
        value=".jsp" />
</bean>

 <context:component-scan base-package="com.controller" />


 </beans>

LoginController.java

 package com.controller;
import org.apache.catalina.connector.Request;
 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
@RequestMapping(value="/LoginAuth",method=RequestMethod.GET)
public ModelAndView loginAuth(Request req){
    String uname=req.getParameter("uname");
    String pass=req.getParameter("pass");
    ModelAndView mav=new ModelAndView();
    if(uname.equals("Alk") && pass.equals("Alk1234")){
        mav.setViewName("success");
    }else{
        mav.setViewName("error");
    }
    return mav;
}

 }

please help me.

There are a few issues with your setup.

First , the dispatcher servlet will automatically load the web application context - as defined by dispatcher-servlet.xml . You do not need to specify this in the contextConfigLocation for the ContextLoaderListener .

Since it doesn't look as though you have a root application context (which would normally be loaded by the ContextLoaderListener ), then you can remove the following from your web.xml :

 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
     </listener-class>
 </listener>

and

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

Second , your dispatcher servlet is mapped to *.do but your controller and form action are both omitting the .do . Change your form and controller mapping to LoginAuth.do

Third , you should add <mvc:annotation-driven/> to your dispatcher-servlet.xml which activates the @RequestMapping annotations and tells Spring to register a bunch of other fairly useful beans behind the scenes.

For this, the mvc namespace should be added to the top of your dispatcher-servlet.xml :

xmlns:mvc="http://www.springframework.org/schema/mvc"

with a schema location of:

http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

Give that a try and see how you get on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM