2012-02-08 54 views
0

我已阅读this教程,用spring mvc编写我的hello world应用程序。但是当我点击“在index.jsp中说出你好“链接。我的代码实际上与教程中的代码相同。我是春季新手,我无法找到我犯的错误,请帮助我。

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> 

这是我的项目结构: enter image description here

这是我的输出

?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', '[email protected]') ?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 

请帮我解决这个问题。

enter image description here

+0

你看到了什么?另外,你在web.xml中使用2.5和3.0;决定哪个。 – 2012-02-08 20:51:28

+0

@DaveNewton当我打开hello.jsp我只看到“这是消息:” – 2012-02-08 22:28:12

+0

@DaveNewton你说“你在web.xml中同时使用2.5和3.0”我无法理解它实际上我是新的弹簧。 – 2012-02-08 22:29:11

回答

2

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

尝试在WEB-INF文件夹中移动hello.jsp,然后调用/hello.html代替。

+0

我已经改变它,但它仍然无法正常工作。 – 2012-02-09 00:21:37

+0

@faridmovsumov哦,是的,你不应该直接访问hello.jsp,而是通过调度servlet。我认为你正在提出正确的要求 - 亚历山德罗对此是正确的。 JstlView映射本身很好,但是;只要JSP是他们应该在的地方。 – 2012-02-09 00:23:05

+0

这是否意味着: - 您在WEB-INF文件夹中移动了hello.jsp - 您重新启动了Tomcat实例(或重新部署) - 您打开http:// localhost:8080/springmvcben/hello.html ? – 2012-02-09 00:25:52

1

我不能重复的问题。需要检查的内容:

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

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

FWIW,我使用Maven,并定义一个春天的依赖(和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 

你需要确保你将所有这些罐子部署到(不只是在构建路径上)。

+0

我会做你说的一步一步来。 我有adde日志消息问题。 我已经将jstl-impl-1.2.jar添加到tomcat的lib目录中。 – 2012-02-08 23:41:32

+0

@faridmovsumov我不能真正阅读日志,下次使用代码格式并且不要破坏行。但是,我没有看到任何例外。 – 2012-02-08 23:46:30

+0

我已经改变日志现在怎么样我不能做得更好对不起:( – 2012-02-08 23:53:04

相关问题