我已阅读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>
弹簧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>
的hello.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
请帮我解决这个问题。
答案 0 :(得分:2)
我可能会弄错,但是DispatcherServlet
映射指向其他地方 - 它实际上映射到* .html,而您正在请求hello.jsp - 这会跳过调度程序servlet并直接进入JSP页面。此外,您的JstlView配置为从WEB-INF文件夹中获取JSP。
尝试移动hello.jsp
文件夹中的WEB-INF
,然后调用/hello.html
。
答案 1 :(得分:1)
我不能复制这个问题。要检查的事项:
最后,您可以使用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
您需要确保所有这些jar 已部署(而不仅仅是在构建路径上)。