简单的Spring Web项目不起作用

时间:2015-11-11 18:05:37

标签: spring spring-mvc

我一直在努力学习如何开发Spring Web应用程序,似乎无法使其正常运行。使用Spring Tool Suite,我使用Simple Spring Web Maven模板创建了一个新的Spring项目,该模板基本上由四个文件组成:

index.jsp

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<html>
    <head>
        <meta charset="utf-8">
        <title>Welcome</title>
    </head> 
    <body>
        <c:url value="/showMessage.html" var="messageUrl" />
        <a href="${messageUrl}">Click to enter</a>
    </body>
</html>

/WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         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>spring-web-test</display-name>

   <!--
        - Location of the XML file that defines the root application context.
        - Applied by ContextLoaderListener.
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!--
        - Servlet that dispatches request to registered handlers (Controller implementations).
    -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

/WEB-INF/mvc-config.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.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Uncomment and your base-package here:
         <context:component-scan
            base-package="org.springframework.samples.web"/>  -->


    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>

</beans>

/WEB-INF/view/showMessage.jsp

<!DOCTYPE html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<html>
    <head>
        <meta charset="utf-8">
        <title>Welcome</title>
    </head> 
    <body>
        <h2>${message}</h2>
    </body>
</html>

这一切看起来都很简单和基本,所以我使用Maven构建它并将其打包成WAR文件。然后我将它部署到本地TomEE 7服务器,转到http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT,然后进入非常简单的index.jsp页面:

Index Page

但是,当点击发送给http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage.html的链接时,我从Tomcat收到404错误:

Tomcat 404 Error

我怀疑这是因为.html结尾 - 基于我对调度员和查看解析器的理解,它会寻找/WEB-INF/view/showMessage.html.jsp。但是,尝试访问http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage会返回相同的错误。查看Tomcat日志,我看到了

Nov 11, 2015 9:46:53 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage.html] in DispatcherServlet with name 'dispatcherServlet'
Nov 11, 2015 9:46:55 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage] in DispatcherServlet with name 'dispatcherServlet'

我做错了什么?整个过程似乎很简单,我甚至没有修改Spring Tool Suite提供的模板,所以我认为它可以直接使用。遗憾的是,在开发用于servlet容器的Spring Web应用程序时似乎没有大量的资源,而且我发现的那些资源似乎没有任何迹象表明我做错了什么。

感谢。

1 个答案:

答案 0 :(得分:0)

将index.jsp更改为此类

 <c:url value="/showMessage" var="messageUrl" />

您不需要文件扩展名,因为您已在mvc-config.xml中进行了设置。

然后你需要一个基本的控制器来处理映射。像这样:

@Controller
public class WebController{


    @RequestMapping(value = "/")
    public String showMessage(){
        return "index";
    }
    @RequestMapping(value = "/showMessage")
    public String showMessage(){
        return "showMessage";
    }
}