簡體   English   中英

未調用Spring MVC Controller

[英]Spring MVC Controller not called

我是Web開發的初學者,並嘗試創建一個簡單的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錯誤:(

首先,我將僅掃描一個基本軟件包,而不是兩個。 從路徑中刪除控制器映射,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM