Tomcat和STS上的Spring Web MVC - 请求的资源不可用

时间:2015-04-22 09:01:09

标签: java spring macos spring-mvc tomcat

使用Spring Tool Suite(STS)和Tomcat v8.0,我按照this教程学习并获得了一些关于Spring的知识。我的目的是展示一个简单的" Hello World"什么的,从那里开始。

这是我的代码。大多数这些文件是在启动项目时自动创建的:

HomeController.java

public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );
        model.addAttribute("welcomeMessage", "Spring MVC trial: Welcome!");

        return "home";
    }
}

针对home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world!  
</h1>

<P>  The time on the server is ${serverTime}. </P>
<P> ${welcomeMessage} </P>
</body>
</html>

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

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

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

</web-app>

我与本教程的唯一不同之处在于使用了Tomcat v8.0服务器。

问题:

这是内部浏览器的结果,设置为(默认情况下,在运行项目时):
http://localhost:8080/spring/WEB-INF/views/home.jsp

enter image description here

这是我在运行项目时获得的。从我从日志中收集到的内容,服务器运行正常。

但是,根据教程,我应该得到一个:

"Welcome home! The client locale is XXXXX"

在控制台日志中,我没有看到。

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您的请求应为http://localhost:8080/{applicationname}

return "home"您的HomeController将会自动调用名为home.jsp

的JSP文件

如何查找http请求的真实应用程序名称?

  1. 在STS的服务器窗口中双击您的服务器。在此示例中,它是 Pivotal tc Server Developer Edition v3.1
  2. Double click on Pivotal tc Server...

    1. 现在打开Web模块文件。然后单击模块,您将看到请求应用程序名称,在此示例中为 / mvc
    2. enter image description here

      所以浏览器网址现在是http://localhost:8080/mvc