简体   繁体   中英

Spring MVC controller not getting invoked

Iam getting a HTTP staus 404 error when i click submit on the login page . It seems like the controller does not get invoked at all (i tried placing debug points in the controller but the execution flow doesn't hit that point).

我的项目结构如下

My controller class

package web;
org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/login")
public class LoginController {
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView processCredentials(@RequestParam("userName")String userName,@RequestParam("password")String password) {
        String message = "Invalid credentials";
        if(!userName.equals("") && !password.equals("")) {
            if(userName.equals(password)) {
                message = "Welcome " + userName + "!!";
            }
        }
        return new ModelAndView("results","message",message);
    }
}

Application context

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

dispatcher-servlet.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation= "http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    <mvc:annotation-driven />

    <context:component-scan base-package="web" >
    </context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property
            name="prefix"
            value="/" >
        </property>
        <property
            name="suffix"
            value=".jsp" >
        </property>
    </bean>
</beans>

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
  <display-name>retrospective</display-name>
  <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
  <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>/retrospective/*</url-pattern>
  </servlet-mapping>
</web-app>

index.jsp

    <!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>Simple Login Application Using Spring MVC</title>
</head>
<body>
    <form action="login" method="post">
        <table cellpadding="0" cellspacing="3" border="0" style="border:1px solid black;">
            <tr>
                <td>User Name</td>
                <td><input type="text" name="userName" /></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.neerav</groupId>
  <artifactId>retrospective</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>retrospective Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>3.2.4.RELEASE</version>  
    </dependency>  

    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>3.2.4.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-web</artifactId>  
        <version>3.2.4.RELEASE</version>  
    </dependency>  
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>  
        <groupId>cglib</groupId>  
        <artifactId>cglib</artifactId>  
        <version>2.2.2</version>  
    </dependency> 
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency> 
  </dependencies>
  <build>
    <finalName>retrospective</finalName>
    <plugins>

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source> <!-- yours Java version -->
                <target>1.7</target> <!-- yours Java version -->
            </configuration>
        </plugin>

    </plugins>
  </build>
</project>

When i start the server , i can see the controller and the method name but after pressing submit on the login page controller method is not called . Could anyone tell me what exactly is the issue ? [![Console of the browser submission][2]][2]

在此处输入图片说明

也许您应该尝试使用以下网址localhost:8080 / retrospective / retrospective / login

i think you will set

public class LoginController {
@RequestMapping(value="/login",method = RequestMethod.POST)
 String message = "Invalid credentials";
    if(!userName.equals("") && !password.equals("")) {
        if(userName.equals(password)) {
            message = "Welcome " + userName + "!!";
        }
    }
    return new ModelAndView("results","message",message);
}

}

its return on results page. check you have a results.jsp page or not?

You might need to move your jsp file under WEB-INF/jsp like folder directory. Once I got the same kind of issue and solved like that. Then you may need to configure your InternalResourceViewResolver bean as follows:

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

You may want to look at this:
Why jsp files inside WEB-INF folder works , but placed under a folder under WEB-INF doesn't?

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