java - Spring Hello World Web 应用程序

标签 java spring tomcat spring-mvc

我正在尝试将 Spring Framework 用于 Web 应用程序,但似乎无法让它与简单的 hello world MVC Web 应用程序一起使用。使用 Spring 3.2、Tomcat 6(上下文设置为“/spring”)。可能找不到带注释的 Controller 类?

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

网络.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>

springapp-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: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>

我的 Controller

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";
    }


}

Tomcat 输出(找到并加载 springapp-servlet.xml)

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'

最佳答案

您尚未启用 annotated MVC configuration

<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 />

编辑:
正如下面的评论所示,这是应用程序类路径的问题

关于java - Spring Hello World Web 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432115/

相关文章:

java - Spring 中的 session 或请求范围变量

Tomcat 警告 : "Setting property ' showServerInfo' to 'false' did not find a matching property"

java - 由自己的 CA 签名的有效 SSL 证书

tomcat - HTTP参数编码

java - CriteriaBuilder 查询中的重音不敏感

java - 如何在 Spring MVC 中获取选定复选框的列表

java - 如何根据应用程序运行的系统加载不同的 native 库(dll、so)

java - 配置 Spring Boot 以监听本地主机旁边的请求

java - ... 中构造函数的参数 0 需要类型为 'org.springframework.web.client.RestTemplate' 的 bean,但无法找到

spring - 用于开发的Jar和用于Gradle部署的War