【问题标题】:Can't run 'Hello World" Spring MVC code无法运行“Hello World”Spring MVC 代码
【发布时间】:2013-11-29 22:35:21
【问题描述】:

我从 Spring 的网站上获取了以下示例,试图理解基本概念,但到目前为止我只是在配置方面苦苦挣扎。

这里是 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

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

然后是 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";
    }
}

最后是 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>

为了完整起见,这是文件夹结构

所以我运行这个项目并收到以下消息:

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'

我尝试使用路径名和诸如此类的东西,但似乎没有任何效果。我无法理解为什么在直接从马嘴里拿走这么简单的代码后,我在运行这么简单的代码时会遇到这么多麻烦。

【问题讨论】:

    标签: java spring jsp spring-mvc


    【解决方案1】:

    由于您将所有类都放在资源目录中,因此它们不会被编译。将所有包移动到 src/main/java

    【讨论】:

    • @spacitron 这是正确答案。您需要将/src/main/java 设为源文件夹。
    • 您的日志中是否仍然收到 org.springframework.web.servlet.DispatcherServlet noHandlerFound 消息?因为这表明在类路径中找不到您的注释控制器。
    【解决方案2】:

    您尝试过哪些网址? 我认为web.xml映射中的URL模式应该是/*而不是/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-25
    • 2020-11-27
    • 2020-08-07
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多