简体   繁体   English

在Spring MVC中打印Hello World时出错

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

I am making simple hello world in spring using MVC taking help from this site http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/ 我在春季使用MVC制作了一个简单的hello世界,从该站点获得帮助: http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/

But I am getting error . 但是我出错了。

Here is my file : web.xml 这是我的文件: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 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 控制者

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

}

I download the code with above url and run 我用上面的URL下载代码并运行

http:// * ** :8080/Spring3MVC/hello** http:// * ** :8080/Spring3MVC/index** http:// * ** :8080/Spring3MVC/hello.jsp** in all i am getting this error http:// * ** :8080 / Spring3MVC / hello ** http:// * ** :8080 / Spring3MVC / index ** http:// * ** :8080 / Spring3MVC / hello.jsp **正在收到此错误

getting error he requested resource (/Spring3MVC) is not available. 遇到错误, 他请求的资源(/ Spring3MVC)不可用。

First, you're making a request to 首先,您正在请求

/Spring3MVC

(assuming /Spring3MVC is your context path), whil your @Controller handler method is mapped to (假设/Spring3MVC是您的上下文路径),而@Controller处理程序方法已映射到

/Spring3MVC/hello

Second, your @Controller isn't being registered. 其次,您的@Controller未注册。 For that to happen, you need to add 为此,您需要添加

<mvc:annotation-driven />

to your spring-servlet.xml context configuration while also adding the mvc namespace to your namespace declarations. spring-servlet.xml上下文配置中,同时还将mvc命名空间添加到命名空间声明中。

Start by reading the documentation , however long it is. 从阅读文档开始,不管它有多长。

Your dispatcher servlet is mapping all * urls, and your endpoint uses /hello url, so try: http://***:8080/Spring3MVC/hello or http://***:8080/hello depending on your application server context settings 您的调度程序Servlet正在映射所有* URL,并且您的端点使用/ hello url,因此请根据您的应用程序服务器尝试: http://***:8080/Spring3MVC/hellohttp://***:8080/hello上下文设置

Also please add <mvc:annotation-driven /> to your context configuration to "turn-on" @Contoller annotation 另外,请将<mvc:annotation-driven />到上下文配置中以“打开” @Contoller注释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM