Java 带注释控制器的SpringMVC:请求的资源不可用

Java 带注释控制器的SpringMVC:请求的资源不可用,java,spring,maven,spring-annotations,Java,Spring,Maven,Spring Annotations,我正在使用Spring 4.0.1 mvc应用程序,我的浏览器无法呈现我的应用程序的任何资源 我正在通过注释而不是xml文件配置我的应用程序。这就是为什么我问这个问题,而不是重复使用任何已经给出的答案。搜索我的问题只会让我找到那些不使用注释的人。我确实注意到了这一点。Maven是我的构建工具,EclipseKepler是我的IDE 有人能帮我吗?如果您有任何建议和资源,我将不胜感激 以下是我的系统设置: 我试图在http://localhost:8080/Spring4MVCHelloWorl

我正在使用Spring 4.0.1 mvc应用程序,我的浏览器无法呈现我的应用程序的任何资源

我正在通过注释而不是xml文件配置我的应用程序。这就是为什么我问这个问题,而不是重复使用任何已经给出的答案。搜索我的问题只会让我找到那些不使用注释的人。我确实注意到了这一点。Maven是我的构建工具,EclipseKepler是我的IDE

有人能帮我吗?如果您有任何建议和资源,我将不胜感激

以下是我的系统设置:
  • 我试图在
    http://localhost:8080/Spring4MVCHelloWorld/hello
  • tomcat catalina日志没有显示任何错误
  • mvn clean package
    是我执行的maven目标
  • 在这里,您可以在GitHub上找到项目
下面是tomcat错误报告:

HTTP状态404-/Spring4MVCHelloWorld/ 类型状态报告
message/Spring4MVCHelloWorld/
说明请求的资源不可用

这是我的
pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>de.bschandera.jeetutorials</groupId>
    <artifactId>Spring4MVCHelloWorld</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Spring4MVCHelloWorld Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.0.1.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring dependencies START -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring dependencies END -->

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
                    <scope>provided</scope> <!-- provided because servlet api jar file must not be embedded inside the 
            webapp since, obviously, the container already has these classes in its classpath: 
            it implements the interfaces contained in this jar -->
        </dependency>

    </dependencies>

    <build>
        <finalName>Spring4MVCHelloWorld</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  • HelloWorldController.java:

    package de.bschandera.jeetutorials.config;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class HelloWorldController {
    
        @RequestMapping("/hello")
        public String hello(@RequestParam(value = "name", required = false, defaultValue = "World") String name, Model model) {
        model.addAttribute("name", name);
        return "helloworld";
        }
    
    }
    
  • WebInitializer.java:

    package de.bschandera.jeetutorials.config;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRegistration.Dynamic;
    
    import org.springframework.web.WebApplicationInitializer;
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    import org.springframework.web.servlet.DispatcherServlet;
    
    public class WebInitializer implements WebApplicationInitializer {
    
        public void onStartup(ServletContext servletContext) throws ServletException {
    
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(Config.class);
    
        ctx.setServletContext(servletContext);
    
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    
        }
    
    }
    
  • src/main/webapp/WEB-INF/views
    下有
    helloworld.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Spring4 MVC -HelloWorld</title>
      </head>
      <body>
        <h1>Hello : ${name}</h1>
      </body>
    </html>
    


    在前三个答案没有解决问题之后,我会认为问题与任何tomcat配置或除代码本身以外的任何其他配置有关。但是我不知道在哪里可以找到建议的解决方案。

    除非您没有向我们展示什么,否则您没有
    欢迎文件
    ,也没有
    /
    的处理程序

    假设
    /Spring4MVCHelloWorld
    是您的上下文路径,那么您的配置中没有任何内容可以处理请求

    http://localhost:8080/Spring4MVCHelloWorld/
    
    http://localhost:8080/Spring4MVCHelloWorld/hello
    
    也许你是想把请求发送到

    http://localhost:8080/Spring4MVCHelloWorld/
    
    http://localhost:8080/Spring4MVCHelloWorld/hello
    

    您的
    HelloWorldController

    类应该扩展
    WebMVCConfigureAdapter
    类,请查看您的类现在应该是什么样子

    @Configuration
    // Marks this class as configuration
    // Specifies which package to scan
    @ComponentScan("de.bschandera.jeetutorials.config")
    // Enables Spring's annotations
    @EnableWebMvc
    public class Config extends WebMvcConfigurerAdapter {
        @Bean
        public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
        }
    }
    

    我签出了您的代码,并将其部署到Tomcat7.0.27(使用Java7),只做了一个更改,一切正常

    我所做的更改是将jstl依赖项添加到pom中

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    
    
    jstl
    jstl
    1.2
    
    我没有更多的文件要显示了。仅限目标文件夹中的文件夹。但我认为这些都不相关,因为maven创建这些文件依赖于上面所示的文件。我没有
    /
    的处理程序。你的假设是对的。我只是请求
    http://localhost:8080/Spring4MVCHelloWorld/hello
    。我还需要一个吗?@Huster如果你要向该URL发送请求,当然你需要一些东西来处理。当我要向
    发送请求时,我需要
    /
    的处理程序http://localhost:8080/Spring4MVCHelloWorld/
    还有一个(就像我已经做过的那样)当我要向
    http://localhost:8080/Spring4MVCHelloWorld/hello
    。我说得对吗?@Huster您需要为每个需要响应的URL设置一个处理程序。查看欢迎文件。@Husterknupp那么?是
    http://localhost:8080/Spring4MVCHelloWorld/hello
    处理是否正确?您是否为
    /
    添加了处理程序?您的建议也不能解决我的问题:-/但是谢谢您的回答。我刚刚导入了您的项目,添加了Jetty插件并运行了它。它第一次成功了!!你是如何部署你的应用程序的?您是否检查了tomcat管理器页面以查看其是否已部署?现在,我对Jetty插件有问题;-)<代码>mvn jetty:运行停止时出现
    生成错误
    。在maven日志之后,还有两个额外的记录器信息:
    shutdownhook executing
    shutdownhook complete
    。我该怎么处理它呢?我通过扩展pom.xml添加了Jetty插件。我只是添加了一个新的插件节点,没有任何配置。我需要在pom.xml中配置Jetty插件吗?我刚刚添加了该插件并在eclipse中运行了它(使用Jetty:run)。我什么也没做!org.eclipse.jetty jetty maven plugin 9.1.2.v20140210@TedTrippin我终于在eclipse中运行了
    jetty:run
    ,这导致
    [错误]无法执行目标org.eclipse.jetty:jetty maven plugin:9.1.2.v20140210:run(默认cli)在项目Spring4MVCHelloWorld:target org.eclipse.jetty:jetty maven插件:9.1.2.v20140210:run失败:无法加载插件“org.eclipse.jetty:jetty maven插件:9.1.2.v20140210”中的mojo“run”,因为API不兼容:org.codehaus.plexus.component.repository.exception.ComponentLookupException:org/eclipse/jetty/maven/plugin/JettyRunMojo:不支持的major.minor版本51.0
    听起来像是java版本的问题。我使用的是Java7。我添加了依赖项,但仍然存在相同的问题。这个问题可能与我的Tomcat配置或项目本身无关。?但我还能查什么?我将遵循TedTrippins的建议,为maven提供Jetty插件。我真的想不出你还应该检查什么。