Java Spring Hello World Web应用程序

Java Spring Hello World Web应用程序,java,spring,tomcat,spring-mvc,Java,Spring,Tomcat,Spring Mvc,我正在为一个web应用试用Spring框架,但似乎无法让它与一个简单的hello world MVC web应用一起工作。使用Spring3.2、Tomcat6(上下文设置为“/Spring”)。可能找不到带注释的控制器类 我正在点击http://localhost:8080/spring/hello web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <web-app version="2.4" xmln

我正在为一个web应用试用Spring框架,但似乎无法让它与一个简单的hello world MVC web应用一起工作。使用Spring3.2、Tomcat6(上下文设置为“/Spring”)。可能找不到带注释的控制器类

我正在点击
http://localhost:8080/spring/hello

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > 
    <display-name>
      Spring
    </display-name>
    <description>
     Spring Test
    </description>

     <servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

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



</web-app>
Tomcat输出(找到并加载springapp-servlet.xml)

您尚未启用


例:


编辑:

应用程序的类路径有问题,如下面的注释所示

我在哪里添加它?试图在开始时将其添加到springapp-servlet.xml…org.xml.sax.SAXParseException:元素“mvc:annotation-driven”的前缀“mvc”未绑定。您需要添加
xmlns:mvc=”http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc                      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
as shown问题如开头所示……警告:找不到URI为[/spring/hello]的HTTP请求的映射在名为“springapp”的DispatcherServlet中,不要点击此
http://localhost:8080/spring/hello
,但点击此按钮:
http://localhost:8080/hello
您没有将
spring
word放入
web.xml
文件中的servlet映射中,因此将spring word从URL中排除如果将此应用程序的上下文路径设置为tomcat中的“/spring”…尝试了一下,以防不起作用
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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">
    <context:component-scan base-package="com.test.web" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
package com.test.web;

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("name", "Test");
        return "hello";
    }


}
INFO: Deploying configuration descriptor spring.xml
Jan 20, 2013 10:22:27 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'springapp'
Jan 20, 2013 10:22:27 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'springapp': initialization started
Jan 20, 2013 10:22:27 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'springapp-servlet': startup date [Sun Jan 20 22:22:27 EST 2013]; root of context hierarchy
Jan 20, 2013 10:22:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/springapp-servlet.xml]
Jan 20, 2013 10:22:27 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7897aaa6: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Jan 20, 2013 10:22:27 PM org.springframework.web.servlet.FrameworkServlet initServletBean
    INFO: FrameworkServlet 'springapp': initialization completed in 321 ms
Jan 20, 2013 10:22:29 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring/hello] in DispatcherServlet with name 'springapp'
<mvc:annotation-driven />
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                    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="..." />

<mvc:annotation-driven />