java 我的 Spring MVC 配置有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13447410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's wrong with my Spring MVC configuration?
提问by Sotirios Delimanolis
I'm trying to setup and learn how to develop web applications with Spring MVC. I've been trying to follow a number of tutorials, but my application doesn't seem to want to work.
我正在尝试设置和学习如何使用 Spring MVC 开发 Web 应用程序。我一直在尝试学习一些教程,但我的应用程序似乎不想工作。
Enviro: Eclipse, m2e-wtp, spring, the whole shebang
环境:Eclipse、m2e-wtp、spring、整个shebang
Context root is BidApp.
上下文根是 BidApp。
I have one controller:
我有一个控制器:
@Controller
public class AuctionController {
@RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
My web.xml in WEB-INF:
我在 WEB-INF 中的 web.xml:
<!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>
And my spring-servlet.xml also in WEB-INF
我的 spring-servlet.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>
My I start the application and navigate to http://localhost:8080/BidApp/hello
, it gives me a 404. Are my xml files in the wrong place or am I missing some configuration parameter? Is there a good place I can look at all the available parameters for configuring a spring application. The spring framework reference, unless I'm reading it wrong, isn't helping.
我的我启动应用程序并导航到http://localhost:8080/BidApp/hello
,它给了我一个 404。我的 xml 文件在错误的地方还是我缺少一些配置参数?有什么好地方可以查看用于配置 spring 应用程序的所有可用参数。spring 框架参考,除非我读错了,否则没有帮助。
Edit:
编辑:
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 Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\php\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server0\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL Server0\DTS\Binn\;C:\Program Files\MATLAB\R2010b\runtime\win64;C:\Program Files\MATLAB\R2010b\bin;C:\Users\Soto\Desktop\android-sdk-windows\tools;C:\Program Files (x86)\Common Files\Teleca Shared;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseHg\;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\MinGW\bin;C:\Program Files\nodejs\;C:\Ruby193\bin;C:\Program Files (x86)\SSH Communications Security\SSH Secure Shell;C:\Users\Soto\Desktop\android-sdk-windows\tools;C:\MinGW\bin;C:\Users\Soto\AppData\Roaming\npm\;C:\apache-maven-3.0.4\bin;C:\Program Files\Java\jdk1.7.0_02\bin;;.
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
采纳答案by user533
You must use the url pattern like this
您必须像这样使用 url 模式
<url-pattern>/</url-pattern>
When you add <mvc:annotation-driven />
to your config, it replaces the default set of handler mappings and handler adapters, and those defaults were the ones that handled the old-style controllers.
当您添加<mvc:annotation-driven />
到配置时,它会替换默认的处理程序映射和处理程序适配器集,这些默认值是处理旧式控制器的那些。
i think you should understand the difference between component-scan and annotation-config. If you use scan then you do not need to use config. Difference between <context:annotation-config> vs <context:component-scan>
我认为您应该了解 component-scan 和 annotation-config 之间的区别。如果您使用扫描,则不需要使用配置。 <context:annotation-config> 与 <context:component-scan> 的区别
Please do execute with your old code with removing the <mvc:annotation-driven />
请使用旧代码执行并删除 <mvc:annotation-driven />
(In general)/ will be working even without mvcdefault servlethandler
(一般情况下)/ 即使没有 mvcdefault servlethandler 也能工作
回答by Hugo Dozois
The dispatcher has an invalid mapping. Only 1 wildcard is accepted.
调度程序有一个无效的映射。仅接受 1 个通配符。
So change the dispatcher matching url to something like /*
因此,将调度程序匹配的 url 更改为类似 /*
So /*.do
or /*.htm
*.html
but not /*.jsp
as tomcat already maps the .jsps
所以/*.do
或/*.htm
*.html
但不是/*.jsp
因为 tomcat 已经映射了 .jsps
If you want some REST urls, may I suggest using UrlRewriteFilter? It is really simple to use and can be used in closely the same way the apache rewrite filter is used.
如果您想要一些 REST 网址,我可以建议使用UrlRewriteFilter吗?它使用起来非常简单,并且可以像使用 apache 重写过滤器一样使用。
回答by Sotirios Delimanolis
Changing the url-pattern to <url-pattern>/</url-pattern>
was not enough. In my spring-servlet.xml file I was missing a few elements:
将 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 />
Once these were added, the appropriate Controller was found (based on annotations) and it was executed at the proper URI starting at /
.
一旦添加了这些,就会找到合适的控制器(基于注释),并在从/
.