java - 带注释 Controller 的 SpringMVC : requested resource is not available

标签 java spring maven spring-annotations

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

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

有人可以帮我吗?我将不胜感激任何提示和资源。

这是我的系统设置:

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

这是tomcat错误报告:

HTTP Status 404 - /Spring4MVCHelloWorld/

type Status report
message /Spring4MVCHelloWorld/
description The requested resource is not available.

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

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 配置 或除代码本身之外的其他任何内容有关。但除了建议的解决方案之外,我不知道在哪里寻找。

最佳答案

除非您没有向我们展示某些内容,否则您没有 welcome-file 也没有 / 的处理程序。

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

http://localhost:8080/Spring4MVCHelloWorld/

也许您想将请求发送至

http://localhost:8080/Spring4MVCHelloWorld/hello

可以由您的 HelloWorldController 处理。

关于java - 带注释 Controller 的 SpringMVC : requested resource is not available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23407042/

相关文章:

java - 如何将要注入(inject)的当前 Activity 传递给 Dagger2 中的对象

java - @AttributeOverride - 继承一些属性作为默认值

java - CDI - Bean 名称不明确

java - Apache James Spring 发行版未启动

IntelliJ 中的 Java 项目没有错误,但通过 Maven 编译失败

java - 如何完全驱逐Spark中的持久 "Stream Blocks"

java - Spring 全局方法安全性和 Aspectj

java - Spring JPA : Insert succesfully but Update Failed (javax. persistence.PersistenceException:托管刷新期间出错)

maven - liquibase-maven-plugin 无法执行目标

java - IDEA Groovy 测试类已经存在