【问题标题】:I can't see ${message} in my first spring mvc example我在第一个 spring mvc 示例中看不到 ${message}
【发布时间】:2012-03-01 01:31:37
【问题描述】:

我已经阅读了this 教程以使用 spring mvc 编写我的 hello world 应用程序。但是当我单击 index.jsp 中的“Say Hello”链接时,我在 hello.jsp 中看不到 ${message}。我的代码实际上与教程中的代码相同。我是春天的新手,我找不到我做错的地方,请帮助我。

HelloWorldController.java

package controller;

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

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")
    public ModelAndView helloWorld() {

        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }
}

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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springmvcben</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>*.html</url-pattern>
  </servlet-mapping>
</web-app>

spring-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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">

    <context:component-scan base-package="controller" />
    <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/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

你好.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>
This is message:
 ${message}
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>
<a href="hello.jsp">Say Hello</a>
</body>
</html>

这是我的项目结构:

这是我的输出

?ub 09, 2012 1:37:21 AM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;c:\program files\common files\microsoft shared\windows live;c:\program files\nvidia corporation\physx\common;C:\Windows\system32;C:\Windows;C:\Windows\system32\wbem;C:\Windows\system32\windowspowershell\v1.0\;c:\program files\microsoft sql server\100\tools\binn\;c:\program files\microsoft sql server\100\dts\binn\;c:\program files\windows live\shared;c:\program files\ibm\gsk8\lib;C:\db2\BIN;C:\db2\FUNCTION;C:\db2\SAMPLES\REPL;C:\Program Files\QuickTime\QTSystem\;C:\Users\Ferid\Desktop\eclipse-jee-indigo-SR1-win32\eclipse;;. ?ub 09, 2012 1:37:22 AM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:springmvcben' did not find a matching property. ?ub 09, 2012 1:37:22 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["http-bio-8080"] ?ub 09, 2012 1:37:22 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler ["ajp-bio-8009"] ?ub 09, 2012 1:37:22 AM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 520 ms ?ub 09, 2012 1:37:22 AM org.apache.catalina.core.StandardService startInternal INFO: Starting service Catalina ?ub 09, 2012 1:37:22 AM org.apache.catalina.core.StandardEngine startInternal INFO: Starting Servlet Engine: Apache Tomcat/7.0.25 ?ub 09, 2012 1:37:26 AM org.apache.catalina.core.StandardContext checkUnusualURLPattern INFO: Suspicious url pattern: "*.*" in context [/springmvcben] - see section SRV.11.2 of the Servlet specification log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet). log4j:WARN Please initialize the log4j system properly. ?ub 09, 2012 1:37:26 AM org.apache.catalina.core.ApplicationContext log INFO: Initializing Spring FrameworkServlet 'spring' ?ub 09, 2012 1:37:27 AM org.apache.catalina.startup.HostConfig deployWAR INFO: Deploying web application archive C:\tomcat\apache-tomcat-7.0.25-windows-x86\apache-tomcat-7.0.25\webapps\MerhabaDunya.war ?ub 09, 2012 1:37:27 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory C:\tomcat\apache-tomcat-7.0.25-windows-x86\apache-tomcat-7.0.25\webapps\docs ?ub 09, 2012 1:37:27 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory C:\tomcat\apache-tomcat-7.0.25-windows-x86\apache-tomcat-7.0.25\webapps\examples ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/core is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.TaglibUriRule body INFO: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined ?ub 09, 2012 1:37:28 AM org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() ?ub 09, 2012 1:37:28 AM org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized() ?ub 09, 2012 1:37:28 AM org.apache.catalina.core.ApplicationContext log INFO: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@1be0de') ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory C:\tomcat\apache-tomcat-7.0.25-windows-x86\apache-tomcat-7.0.25\webapps\host-manager ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory C:\tomcat\apache-tomcat-7.0.25-windows-x86\apache-tomcat-7.0.25\webapps\manager ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.HostConfig deployDirectory INFO: Deploying web application directory C:\tomcat\apache-tomcat-7.0.25-windows-x86\apache-tomcat-7.0.25\webapps\ROOT ?ub 09, 2012 1:37:28 AM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["http-bio-8080"] ?ub 09, 2012 1:37:28 AM org.apache.coyote.AbstractProtocol start INFO: Starting ProtocolHandler ["ajp-bio-8009"] ?ub 09, 2012 1:37:28 AM org.apache.catalina.startup.Catalina start INFO: Server startup in 6007 ms

请帮我解决这个问题。

【问题讨论】:

  • 你看到了什么?此外,您在 web.xml 中同时使用了 2.5 和 3.0;决定哪个。
  • @DaveNewton 当我打开 hello.jsp 时,我只看到“这是消息:”
  • @DaveNewton 您说“您在 web.xml 中同时使用 2.5 和 3.0”我无法理解,实际上我是春天的新手。
  • 查看你的web.xml; web-app_2_5.xsd 然后是 web-app_3_0.xsd。
  • 我把这两个都改成了 3.0 @DaveNewton

标签: spring servlets spring-mvc controller


【解决方案1】:

我无法复制问题。检查事项:

  • 那个红色的日志输出——它说明了什么?
  • 将日志级别调高到 DEBUG 并重新启动 Tomcat - 是否有异常?
  • 确保您拥有所需的所有库。
  • 确保您的 $TOMCAT_HOME/lib 目录中有 JSTL 实现。

最后,您可以使用JSTL 1.2 downloads from Glassfish。由于 Spring 配置,如果 Tomcat 的 lib 目录中不存在这些库,则 Tomcat/Spring 将不会启动。

FWIW,我使用的是 Maven,并定义了一个 Spring 依赖项(和 Log4J):

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>3.0.1.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
  </dependency>
</dependencies>

这将创建以下依赖树:

[INFO] +- org.springframework:spring-webmvc:jar:3.0.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-asm:jar:3.0.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.0.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.0.1.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-aop:jar:3.0.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:3.0.1.RELEASE:compile
[INFO] |  |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.springframework:spring-expression:jar:3.0.1.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:3.0.1.RELEASE:compile
[INFO] |     \- aopalliance:aopalliance:jar:1.0:compile
[INFO] \- log4j:log4j:jar:1.2.16:compile

您需要确保所有这些 jars已部署(不仅仅是在构建路径上)。

【讨论】:

  • 我会按照你说的一步一步做。我已经添加了日志消息来提问。我已将 jstl-impl-1.2.jar 添加到 tomcat 的 lib 目录中。
  • @faridmovsumov 我真的看不懂日志,下次使用代码格式,不要断行。但是,我没有看到任何例外情况。
  • 我已经更改了日志现在怎么样了我不能做得更好对不起:(
  • 我只在WEB-INF/lib下添加了jar,是不是搞错了?
  • @faridmovsumov 我也没有看到 aopalliance jar,但它可能不是绝对必要的。就个人而言,我会使用 Maven--here's the POM for my working sample
【解决方案2】:

我可能弄错了,但是DispatcherServlet 映射指向别处——它实际上映射到 *.html,而您正在请求 hello.jsp——这会跳过调度程序 servlet 并直接进入 JSP 页面。此外,您的 JstlView 被配置为从 WEB-INF 文件夹中获取 JSP。

尝试将hello.jsp 移动到WEB-INF 文件夹中,然后改为调用/hello.html

【讨论】:

  • @faridmovsumov 哦,是的,您不应该直接访问 hello.jsp,而是通过调度 servlet。我以为你提出了正确的要求——亚历山德罗是正确的。然而,JstlView 映射本身很好;只要 JSP 在它们应该在的地方。
  • 这是否意味着: - 您将 hello.jsp 移到了 WEB-INF 文件夹中 - 您重新启动了 Tomcat 实例(或重新部署) - 您打开了 localhost:8080/springmvcben/hello.html
  • spring*.* 可以吗?
  • 可怕 - 甚至无法部署。将其保留为 *.html,这不是问题。请尝试重现我之前评论中的步骤。
  • @AlessandroSantini 我以前尝试过,但不幸的是这无济于事
猜你喜欢
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
相关资源
最近更新 更多