Spring MVC控制器没有被调用

时间:2016-07-28 20:51:36

标签: java spring maven spring-mvc java-ee

我在登录页面上单击“提交”时出现HTTP staus 404错误。看起来控制器根本没有被调用(我尝试在控制器中放置调试点,但执行流程没有达到这一点)。

My project structure is as follows

我的控制器类

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);
    }
}

应用程序上下文

    <?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>

调度-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>

当我启动服务器时,我可以看到控制器和方法名称,但在登录页面上按提交后,控制器方法不会被调用。 谁能告诉我究竟是什么问题? [![浏览器提交控制台] [2]] [2]

enter image description here

3 个答案:

答案 0 :(得分:1)

也许你应该尝试这个url localhost:8080 / retrospective / retrospective / login

答案 1 :(得分:0)

我想你会设置

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);
}

}

返回结果页面。检查你有一个results.jsp页面?

答案 2 :(得分:0)

您可能需要将jsp文件移动到WEB-INF / jsp文件夹目录下。一旦我遇到同样的问题并解决了这样的问题。然后,您可能需要按如下方式配置InternalResourceViewResolver bean:

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

您可能希望看一下:
Why jsp files inside WEB-INF folder works , but placed under a folder under WEB-INF doesn't?