简体   繁体   English

无法运行'Hello World'Spring MVC代码

[英]Can't run 'Hello World" Spring MVC code

I took the following example from Spring's website, trying to understand the basic concepts but so far I've only been struggling with configuration. 我从Spring的网站上看了下面的例子,尝试理解基本概念,但到目前为止我只是在配置上苦苦挣扎。

So here's web.xml: 所以这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

servlet-context.xml servlet的context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    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">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Scans within the base package of the application for @Components to configure as beans -->
<!-- @Controller, @Service, @Configuration, etc. -->
<context:component-scan base-package="xyz.sample.baremvc" />

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />

</beans>

Then HomeController.java 然后是HomeController.java

package xyz.sample.baremvc;

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

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public String home() {
        System.out.println("HomeController: Passing through...");
        return "WEB-INF/views/home.jsp";
    }
}

And finally home.jsp 最后是home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page session="false" %>
<!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>Insert title here</title>
</head>
    <body>
        <p>Hello World!
    </body>
</html>

For completeness this is the folder structure 为了完整性,这是文件夹结构

在此输入图像描述

So I run this project and I get the following message: 所以我运行这个项目,我收到以下消息:

Nov 29, 2013 11:14:33 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Springer2/] in DispatcherServlet with name 'appServlet'

I tried to play around with path names and whatnot but nothing seems to work. 我试着玩路径名称和诸如此类似乎没有任何作用。 I can't fathom why I'm having so much trouble running such simple code after taking it directly from the horse's mouth. 我无法理解为什么在直接从马口中取出这么简单的代码后我遇到这么多麻烦。

Since you placed all your classes inside the resources directory they won't get compiled. 由于您将所有类放在资源目录中,因此无法编译它们。 Move all packages to src/main/java . 将所有包移动到src / main / java

The sample code of spring ide, don't work. spring ide的示例代码,不起作用。 When i used, i had to few some changes, like a version for to make it work. 当我使用时,我不得不做一些改变,比如一个版本,以使其工作。 So in 3.1.1 i use this structure: 所以在3.1.1我使用这个结构:

https://bitbucket.org/bcfurtado/spring-mvc-template https://bitbucket.org/bcfurtado/spring-mvc-template

Maybe help. 也许有帮助。

What URLs have you tried? 您尝试过哪些网址? I think the URL pattern in the web.xml mapping should be /* instead of / . 我认为web.xml映射中的URL模式应该是/*而不是/

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

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