我是Spring MVC框架的新手。我正在尝试使用Eclipse Juno和Tomcat(V 7.0.61)中的Spring 4设置示例代码库。我尝试了在不同论坛中陈述的不同选项,但没有一个工作。解决这个问题的任何帮助都会非常受欢迎!
以下是代码:
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>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
的HelloWeb-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.example.HelloController" />
<!-- <mvc:default-servlet-handler /> -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HelloController.java
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET )
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
服务器日志:
May 5, 2015 5:26:32 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HelloWeb' did not find a matching property.
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/7.0.61
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Mar 27 2015 12:03:56 UTC
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 7.0.61.0
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 7
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 6.1
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: x86
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files (x86)\Java\jdk1.6.0_31\jre
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.6.0_31-b05
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Sun Microsystems Inc.
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: C:\U175827\Workspaces\jBpm\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: C:\U175827\apache-tomcat-7.0.61
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\U175827\Workspaces\jBpm\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\U175827\apache-tomcat-7.0.61
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\U175827\Workspaces\jBpm\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\U175827\apache-tomcat-7.0.61\endorsed
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Xbootclasspath:C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\resources.jar;C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\rt.jar;C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\jsse.jar;C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\jce.jar;C:\Program
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: Files
May 5, 2015 5:26:32 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: (x86)\Java\jdk1.6.0_31\jre\lib\charsets.jar;C:\U175827\jboss-5.1.0.GA-jdk6\jboss-5.1.0.GA\common\lib\servlet-api.jar
May 5, 2015 5:26:32 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
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 (x86)\Java\jdk1.6.0_31\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/U175827/eclipse/jre/bin/client;C:/U175827/eclipse/jre/bin;C:/U175827/eclipse/jre/lib/i386;C:\Program Files (x86)\Java\jdk1.6.0_31\bin;C:\Program Files (x86)\Java\jdk1.6.0_31\jre\bin;C:\Oracle\product\11.2.0\client_1\bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft Application Virtualization Client;C:\Program Files\Citrix\ICAService\;C:\Program Files\Citrix\System32\;C:\Program Files (x86)\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files (x86)\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Common Files\Citrix\System32\;C:\U175827\unix-utils;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\U175827\eclipse;;.
May 5, 2015 5:26:32 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8082"]
May 5, 2015 5:26:32 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8010"]
May 5, 2015 5:26:32 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1809 ms
May 5, 2015 5:26:32 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
May 5, 2015 5:26:32 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.61
May 5, 2015 5:26:33 PM org.apache.tomcat.websocket.server.WsSci onStartup
INFO: JSR 356 WebSocket (Java WebSocket 1.1) support is not available when running on Java 6. To suppress this message, run Tomcat on Java 7, remove the WebSocket JARs from $CATALINA_HOME/lib or add the WebSocket JARs to the tomcat.util.scan.DefaultJarScanner.jarsToSkip property in $CATALINA_BASE/conf/catalina.properties. Note that the deprecated Tomcat 7 WebSocket API will be available.
May 5, 2015 5:26:33 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [329] milliseconds.
May 5, 2015 5:26:36 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
May 5, 2015 5:26:36 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'HelloWeb'
May 5, 2015 5:26:36 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'HelloWeb': initialization started
May 5, 2015 5:26:36 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'HelloWeb-servlet': startup date [Tue May 05 17:26:36 IST 2015]; root of context hierarchy
May 5, 2015 5:26:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/HelloWeb-servlet.xml]
May 5, 2015 5:26:38 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'HelloWeb': initialization completed in 1750 ms
May 5, 2015 5:26:38 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8082"]
May 5, 2015 5:26:38 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8010"]
May 5, 2015 5:26:38 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 5487 ms
May 5, 2015 5:26:45 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HelloWeb/hello] in DispatcherServlet with name 'HelloWeb'
我正在使用的网址:
http://localhost:8082/HelloWeb/hello
答案 0 :(得分:0)
您错过了配置中的annotation-driven
元素
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<context:component-scan base-package="com.example" />
<!-- <mvc:default-servlet-handler /> -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
另请注意,您需要将扫描值设置为打包而不是类,因此请将设置从<context:component-scan base-package="com.example.HelloController" />
修改为<context:component-scan base-package="com.example" />
答案 1 :(得分:0)
您需要将此添加到web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
答案 2 :(得分:0)
我在不同的系统中进行了相同的设置并且工作正常,因此我认为Eclipse / Tomcat存在一些问题。
答案 3 :(得分:0)
如果hello.jsp放在jsp文件夹中,请将@RequestMapping(“/ hello”)更改为@RequestMapping(“/”)