簡體   English   中英

在Spring MVC中打印Hello World時出錯

[英]Getting error while printing hello world in spring MVC

我在春季使用MVC制作了一個簡單的hello世界,從該站點獲得幫助: http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

但是我出錯了。

這是我的文件: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>Spring3MVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*</url-pattern>
</servlet-mapping>
</web-app>

spring-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:mvc="http://www.springframework.org/schema/mvc" 
    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-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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <context:component-scan
        base-package="net.viralpatel.spring3.controller" />
        <mvc:annotation-driven />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

控制者

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        System.out.println(message);
        return new ModelAndView("hello", "message", message);
    }

}

我用上面的URL下載代碼並運行

http:// * ** :8080 / Spring3MVC / hello ** http:// * ** :8080 / Spring3MVC / index ** http:// * ** :8080 / Spring3MVC / hello.jsp **正在收到此錯誤

遇到錯誤, 他請求的資源(/ Spring3MVC)不可用。

首先,您正在請求

/Spring3MVC

(假設/Spring3MVC是您的上下文路徑),而@Controller處理程序方法已映射到

/Spring3MVC/hello

其次,您的@Controller未注冊。 為此,您需要添加

<mvc:annotation-driven />

spring-servlet.xml上下文配置中,同時還將mvc命名空間添加到命名空間聲明中。

從閱讀文檔開始,不管它有多長。

您的調度程序Servlet正在映射所有* URL,並且您的端點使用/ hello url,因此請根據您的應用程序服務器嘗試: http://***:8080/Spring3MVC/hellohttp://***:8080/hello上下文設置

另外,請將<mvc:annotation-driven />到上下文配置中以“打開” @Contoller注釋

暫無
暫無

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

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