eclipse Spring MVC 注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5208816/
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
Spring MVC Annotations
提问by mathlovingkitten
I've been over Spring's documentation a couple times, but I don't seem to be able to get @Controller, etc annotations to work.
我已经看过几次 Spring 的文档,但我似乎无法让 @Controller 等注释起作用。
I am loading the dependencies and repositories in my POM (... are my specific values):
我正在我的 POM 中加载依赖项和存储库(...是我的特定值):
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>...</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.context</artifactId>
<version>${org.springframework.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.web.servlet</artifactId>
<version>${org.springframework.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>com.springsource.repository.bundles.release</id>
<url>http://repository.springsource.com/maven/bundles/release/</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.external</id>
<url>http://respoitory.springsource.com/maven/bundles/external</url>
</repository>
</repositories>
</project>
In my web.xml, I am setting up the dispatcher servlet:
在我的 web.xml 中,我正在设置调度程序 servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.4">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
And under WEB-INF I have servlet-context.xml (... is my controller package):
在 WEB-INF 下,我有 servlet-context.xml (...是我的控制器包):
<?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: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 />
<!-- Adds prefix and suffix to returned views -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
I think I must be missing or forgetting something simple. Eclipse doesn't recognize the annotations and maven fails to build the project. Can anyone help me out here?
我想我一定是遗漏或忘记了一些简单的事情。Eclipse 无法识别注释并且 maven 无法构建项目。有人可以帮我从这里出去吗?
EDIT
编辑
For further clarification, in eclipse, adding the annotation results in:
为了进一步说明,在 eclipse 中,添加注释会导致:
The import org.springframework cannot be resolved
And running mvn clean install results in:
并运行 mvn clean install 结果:
package org.springframework.stereotype does not exist
And removing the runtime scope results in the following Maven error:
删除运行时范围会导致以下 Maven 错误:
error reading ...\.m2\repository\org\aopalliance\com.springsource.org.aopalliance.0.0\com.springsource.org.aopalliance-1.9.9.jar; error in opening zip file
采纳答案by mathlovingkitten
I found the answer to my question in a later post.
我在后来的帖子中找到了我的问题的答案。
Basically, I needed to add the m2eclipse plugin to Eclipse. You can find instructions at http://m2eclipse.sonatype.org/installing-m2eclipse.htmlif needed. Once this is installed, you can right click on your project in Eclipse and choose Maven > Enable Dependency Management. Then eclipse will be able to recognize imports from your POM dependencies.
基本上,我需要将 m2eclipse 插件添加到 Eclipse。如果需要,您可以在http://m2eclipse.sonatype.org/installing-m2eclipse.html 上找到说明。安装完成后,您可以在 Eclipse 中右键单击您的项目并选择 Maven > Enable Dependency Management。然后 eclipse 将能够识别来自您的 POM 依赖项的导入。
So in the end, I had the following. Eclipse recognized everything, Maven correct built and packaged the project, and Tomcat deployed the project successfully.
所以最后,我有以下几点。Eclipse 识别了一切,Maven 正确构建并打包了项目,Tomcat 成功部署了项目。
Here is my web.xml hooking up the dispatcher servlet:
这是我的 web.xml 连接调度程序 servlet:
<?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="CoreProject" version="3.0">
<!-- SERVLET FOR HANDLING ALL INCOMING REQUESTS -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- SERVLET MAPPING FOR HANDLING ALL INCOMING REQUESTS -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Here's my servlet-context.xml enabling the annotations and view resolver:
这是我的 servlet-context.xml 启用注释和视图解析器:
<?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: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">
<!-- SCANS ALL MATCHING PACKAGES FOR COMPONENTS -->
<context:component-scan base-package="com.myBasePackage" />
<!-- ALLOWS FOR ANNOTATION MAPPING FOR CONTROLLERS -->
<mvc:annotation-driven />
<!-- ADDS PREFIX AND SUFFIX TO RETURNED VIEWS -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Here's my POM file:
这是我的 POM 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myBasePackage</groupId>
<artifactId>CoreProject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>CoreProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<dependencies>
<!-- SERVLET DEPENDENCIES -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- SPRING DEPENDENCIES -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<!-- TESTING DEPENDENCIES -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Hope this helps someone else.
希望这对其他人有帮助。
回答by Spencer Uresk
It may have something to do with what you've set as the scope for the Spring dependencies. You've set them to "runtime", which means they aren't used for compilation and are only needed at runtime. This would explain why it won't compile in Eclipse (and is probably the problem in your Maven build as well, although you didn't specify the error).
它可能与您设置为 Spring 依赖项的范围有关。您已将它们设置为“运行时”,这意味着它们不用于编译,仅在运行时需要。这将解释为什么它不会在 Eclipse 中编译(并且可能也是您的 Maven 构建中的问题,尽管您没有指定错误)。
Just remove the scope to use the default scope (the default is compile, which is most likely what you want in this case) and see if that gets you any further.
只需删除范围以使用默认范围(默认值是编译,这很可能是您在这种情况下想要的),然后看看这是否能让您更进一步。
Update:
更新:
Thanks for adding the specific error you are getting.
感谢您添加您遇到的特定错误。
This means Maven is trying to use a local dependency, but can't open the jar file. I've seen this happen sometimes where the jar gets corrupted during the download or only downloads partially.
这意味着 Maven 正在尝试使用本地依赖项,但无法打开 jar 文件。我见过这种情况有时会发生在下载过程中 jar 损坏或仅部分下载的情况。
Try deleting the ~/.m2/repository/org/aopalliance/com.springsource.org.aopalliance/ directory and re-run the build. This will cause Maven to re-download it and hopefully get it in a good state.
尝试删除 ~/.m2/repository/org/aopalliance/com.springsource.org.aopalliance/ 目录并重新运行构建。这将导致 Maven 重新下载它并希望它处于良好状态。
回答by Loki
Be sure your eclipse cached version of the repo index is up to date, you can even manually update the index by using maven repo view and update it from there.
确保您的 eclipse 缓存版本的 repo 索引是最新的,您甚至可以使用 maven repo 视图手动更新索引并从那里更新它。
I always try the vanilla version (command line) of my maven builds, if I get an error from some IDE.
如果我从某些 IDE 收到错误,我总是尝试使用我的 Maven 构建的 vanilla 版本(命令行)。
回答by kapil das
use below and try, annotation handler should be used as below in your application context .xml
在下面使用并尝试,注释处理程序应该在您的应用程序上下文 .xml 中使用如下
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="0"/>
</bean>
this will resolved stereotype errors
这将解决刻板印象错误
if not worked add below line for xsi:schemaLocation
如果不起作用,请为 xsi:schemaLocation 添加以下行
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd