我的Spring MVC配置有什么问题?



我正在尝试设置并学习如何使用Spring MVC开发Web应用程序。我一直在尝试遵循许多教程,但我的应用程序似乎不想工作。

环境:日食,m2e-wtp,春天,整个舍邦

上下文根是 BidApp。

我有一个控制器:

@Controller
public class AuctionController {    
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        String message = "Hello World, Spring 3.0!";
        return new ModelAndView("hello", "message", message);
    }
}

我的网络.xml在 WEB-INF 中:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>BidApp</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
         <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.*</url-pattern>
    </servlet-mapping>
</web-app>

还有我的弹簧.xml也在 WEB-INF 中

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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">
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    <!-- Scans within the base package of the application for @Components to configure as beans -->
    <!-- @Controller, @Service, @Configuration, etc. -->
    <context:component-scan base-package="com.bidapp.controllers" />
    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

我的我启动应用程序并导航到http://localhost:8080/BidApp/hello,它给了我一个 404。我的 xml 文件是否位于错误的位置或缺少某些配置参数?有没有一个好地方我可以查看用于配置 spring 应用程序的所有可用参数。除非我读错了,否则 spring 框架参考没有帮助。

编辑:

Tomcat logs:
Nov 18, 2012 10:07:35 PM 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 FilesJavajre7bin;C:WindowsSunJavabin;C:Windowssystem32;C:Windows;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:php;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;c:Program Files (x86)Microsoft SQL Server100ToolsBinn;c:Program Files (x86)Microsoft SQL Server100DTSBinn;C:Program FilesMATLABR2010bruntimewin64;C:Program FilesMATLABR2010bbin;C:UsersSotoDesktopandroid-sdk-windowstools;C:Program Files (x86)Common FilesTeleca Shared;C:Program Files (x86)Microsoft ASP.NETASP.NET Web Pagesv1.0;C:Program FilesTortoiseSVNbin;C:Program Files (x86)QuickTimeQTSystem;C:Program FilesTortoiseHg;C:Program FilesMySQLMySQL Server 5.5bin;C:MinGWbin;C:Program Filesnodejs;C:Ruby193bin;C:Program Files (x86)SSH Communications SecuritySSH Secure Shell;C:UsersSotoDesktopandroid-sdk-windowstools;C:MinGWbin;C:UsersSotoAppDataRoamingnpm;C:apache-maven-3.0.4bin;C:Program FilesJavajdk1.7.0_02bin;;.
Nov 18, 2012 10:07:35 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:BidApp' did not find a matching property.
Nov 18, 2012 10:07:35 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Nov 18, 2012 10:07:35 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Nov 18, 2012 10:07:35 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 594 ms
Nov 18, 2012 10:07:36 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Nov 18, 2012 10:07:36 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.22
Nov 18, 2012 10:07:36 PM org.apache.catalina.core.StandardContext checkUnusualURLPattern
INFO: Suspicious url pattern: "*.*" in context [/BidApp] - see section SRV.11.2 of the Servlet specification
Nov 18, 2012 10:07:36 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Nov 18, 2012 10:07:36 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Nov 18, 2012 10:07:36 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Nov 18 22:07:36 EST 2012]; root of context hierarchy
Nov 18, 2012 10:07:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]
Nov 18, 2012 10:07:36 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6ad89088: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0]; root of factory hierarchy
Nov 18, 2012 10:07:37 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 739 ms
Nov 18, 2012 10:07:37 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Nov 18, 2012 10:07:37 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Nov 18, 2012 10:07:37 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1395 ms

你必须像这样使用 url 模式

<url-pattern>/</url-pattern>

当您将<mvc:annotation-driven />添加到配置中时,它将替换默认的处理程序映射和处理程序适配器集,这些默认值是处理旧式控制器的默认值。

我认为您应该了解组件扫描和注释配置之间的区别。如果使用扫描,则无需使用配置。<上下文:注释配置>与<上下文:组件扫描>之间的区别

请使用旧代码执行并删除<mvc:annotation-driven />

(一般来说)/即使没有 mvcdefault servlethandler 也能工作

调度程序具有无效的映射。仅接受 1 个通配符。

因此,将调度程序匹配 url 更改为类似 /*

所以/*.do/*.htm *.html但不/*.jsp,因为tomcat已经映射了.jsps

如果你想要一些 REST 网址,我可以建议使用 UrlRewriteFilter 吗?它使用起来非常简单,可以像使用 apache 重写过滤器

一样使用。

url-pattern 更改为 <url-pattern>/</url-pattern> 是不够的。在我的 spring-servlet.xml 文件中,我缺少一些元素:

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- This tag allows for mapping the DispatcherServlet to "/" (all extensions etc)-->
<mvc:default-servlet-handler/>

<!-- Enables many annotations and searches for @Controller annotated methods etc.. -->
<context:annotation-config />

添加这些后,将找到相应的控制器(基于注释),并从 / 开始在正确的 URI 上执行该控制器。

最新更新