Spring MVC Controller未调用

时间:2017-08-15 19:48:01

标签: java spring servlets model-view-controller dispatcher

我是网络开发的初学者,并尝试创建一个简单的spring mvc应用程序。

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Spring Web MVC Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>
    <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>
</web-app>

MVC-调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        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">

    <mvc:annotation-driven/>

    <context:component-scan base-package="org.test.test.controller"/>
    <context:component-scan base-package="org.test.test.model"/>

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

</beans>

控制器

@Controller
@Scope(value = "singleton")
public class T1Controller {

    @RequestMapping(value = "/hello.do")
    public ModelAndView helloWorld() {
        System.out.print("Hi");
        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }
}

我写的一切都像我的教程一样正确,但仍然出现404 tomcat 8.5RC错误。我没有调用控制器方法。

我添加了spring 4 libs,我认为一切都很好,但我没有在Intellij IDEA中看到Hi输出。

的hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>one</title>
</head>
<body>
Hello World
${message}
</body>
</html>

我在运行tomcat时看到index.jsp页面...但是当试图http://localhost:8000/hello.do出现404错误时:(

1 个答案:

答案 0 :(得分:0)

首先,我将首先扫描一个基本包而不是两个。从路径aswel中删除控制器地图,如下所示:

<context:component-scan base-package="org.test.test"/>

摆脱以下因为只有一个就足够了。你不需要两个。

<context:component-scan base-package="org.test.test.model"/> 

并修改您的属性值,如下所示:

    <property name="prefix">
        <value>/WEB-INF/</value>
    </property>