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

时间:2014-05-01 11:48:01

标签: java spring maven spring-annotations

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

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

任何人都可以帮助我吗?我会很感激任何提示和资源。

这是我的系统设置:

  • 我尝试访问http://localhost:8080/Spring4MVCHelloWorld/hello上的网络应用。
  • tomcat catalina日志未显示任何错误。
  • mvn clean package是我执行的maven目标。
  • 您可以在此处找到GitHub上的项目https://github.com/Husterknupp/spring-octo-lana

In这是tomcat错误报告:

  

HTTP状态404 - / Spring4MVCHelloWorld /

     

输入状态报告
  message / Spring4MVCHelloWorld /
  description请求的资源不可用。

这是我的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>

src/main/java下有三个类:

  1. Config.java:

    package de.bschandera.jeetutorials.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.view.JstlView;
    import org.springframework.web.servlet.view.UrlBasedViewResolver;
    
    @Configuration
    // Marks this class as configuration
    // Specifies which package to scan
    @ComponentScan("de.bschandera.jeetutorials.config")
    // Enables Spring's annotations
    @EnableWebMvc
    public class Config {
        @Bean
        public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
        }
    }
    
  2. 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";
        }
    
    }
    
  3. 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);
    
        }
    
    }
    
  4. 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>
    

    src/main/webapp/WEB-INF下,有index.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 INDEX</title>
      </head>
      <body>
        <h1>Index page - welcome!</h1>
      </body>
    </html>
    

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

3 个答案:

答案 0 :(得分:0)

除非您没有向我们展示,否则您没有welcome-file而且没有/的处理程序。

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

的请求
http://localhost:8080/Spring4MVCHelloWorld/

也许您打算将请求发送到

http://localhost:8080/Spring4MVCHelloWorld/hello

可由HelloWorldController处理。

答案 1 :(得分:0)

您的课程应该延长WebMvcConfigurerAdapter课程,看看您的课程现在应该如何

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

答案 2 :(得分:0)

我检查了你的代码并将其部署到Tomcat 7.0.27(使用Java 7),只进行了一次更改,一切正常。

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

    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>