Spring MVC请求未到达Controller类

时间:2014-08-17 06:16:38

标签: spring-mvc

我知道之前已经提出过这个问题,但那里提到的解决方案似乎都没有对我有用。 我正在编写Spring MVC Web应用程序。但是我的请求没有到达控制器类。 这是相关文件。 任何人都可以通过指出我哪里出错来帮助我吗?感谢...

的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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>booksWorld</display-name>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<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>/</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:**/*applicationContext.xml</param-value>
</context-param>
</web-app>

调度-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:p="http://www.springframework.org/schema/p" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Declare a view resolver -->
<bean id="viewResolver"   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp"/>
</bean>
</beans>

的applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"      xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


<context:component-scan base-package="com.booksworld.controllers" />
<context:annotation-config />
<mvc:annotation-driven />
<context:property-placeholder location="classpath:**/*config.properties" />

<!-- <bean id="messageSource"     class="org.springframework.context.support.ResourceBundleMessageSource"     p:basename="resources/Messages" /> -->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"    class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-    ref="sessionFactory" />


<bean id="sessionFactory"     class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        </props>
    </property>
    <property name="packagesToScan" value="com.booksworld"></property>
</bean>

<bean id="dataSource"     class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
    p:username="${jdbc.username}" p:password="${jdbc.password}" >
</bean>

</beans>

我的控制器类

package com.booksworld.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CommonController {

@RequestMapping("/proceed")
public String loadLoginPage() {
    System.out.println("we reached here");
    return "login";
}

@RequestMapping(value="/login")
public String login() {
    return "abc";
}

}

index.jsp(欢迎jsp文件)

<%@ 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>Welcome All</title>
</head>

<body>
    <p>Hello World</p>

    <%response.sendRedirect("proceed.do"); %>
</body>

</html>

的login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>
    <!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>Books World - Login</title>
    </head>

    <body>

        <sf:form method="POST" action="login.do" modelAttribute="Login">

            <h3>Please enter your credentials</h3>
            Login Name<sf:input path="userName" />
            Password<sf:password path="password" />

            </br>
            </br>
            <input type="submit" value="Log In" />
            <input type="reset" value="Reset" />
        </sf:form>

    </body>

    </html>

我在eclipse中开发,并使用Tomcat。服务器日志没有错误。只是标准的启动日志。

请帮我指出我哪里出错。

1 个答案:

答案 0 :(得分:2)

您尚未指定要点击的网址以及您看到的错误或输出,因此我将按您发布的文件进行操作。您发布的内容似乎至少存在一些问题。我假设您尝试访问http://[server]:[port]/index.jsphttp://[server]:[port]/

问题1 :使用MVC框架时,通常不直接请求JSP模板等Web模板。因此,web.xml中的欢迎文件列表不应包含index.jsp。更常见的是完全省略这一部分。相反,您应该为应用程序根添加一个请求映射到控制器类,例如:

@RequestMapping("/")
public String home() { return "index"; }

执行此操作后,您应该可以点击http://[server]:[port]/并获得回复。

问题2 :文件index.jsp重定向到网址proceed.do,但您的控制器中没有此网址的映射。相反,控制器具有URL proceed的映射。当您尝试配置URL映射时,可能会与Struts产生一些混淆。

问题3 :文件login.jsp提交login.do,但此网址没有映射。而是login的映射。因此,表单操作应从login.do更改为login

问题4 login.jsp中的表单绑定到名为Login的模型属性,但控制器不会添加具有该名称的任何模型对象。

问题5 login.jsp中的表单是使用HTTP POST提交的,但控制器没有为login方法指定此表单。使用@RequestMapping注释的控制器方法的默认值为HTTP GET,因此在您提交表单时不会调用login方法。

问题6 :文件index.jsp似乎是多余的,因为其唯一目的是重定向到同一服务器上的其他网址。使用以下简化的控制器代码可以获得更好的效果:

@Controller
public class CommonController {
  @RequestMapping("/")
  public String loadLoginPage(Model model) {
    model.addAttribute("Login", new Login()); // Or something like this, which represents the actual model object that should collect the login information.

    return "login";
  }

  @RequestMapping(method = RequestMethod.POST, value="/login")
  public String login(Login login) {
    // Use the login object to authenticate the user.

    return "abc";
  }
}

我还建议您保留行

<context:component-scan base-package="com.booksworld.controllers" />
<context:annotation-config />
<mvc:annotation-driven />

在文件dispatcher-servlet.xml中,因为这些行特定于您的表示层。