【问题标题】:Spring Maven + IntelliJ and 404Spring Maven + IntelliJ 和 404
【发布时间】:2017-09-10 17:08:58
【问题描述】:

所以,今天我终于决定试试 IntelliJ IDEA。

设置完所有内容后,我尝试制作一个非常简单的 Spring webMVC 项目。

因为这是一个 Spring 应用程序,所以我现在还没有 运行项目时出现任何错误或警告,但只有 404 页面...

Tomcat 9 运行时没有任何警告或错误,Java 似乎也一样 没有问题。运行时会以太常见的方式打开浏览器 404 - resource not found 错误。

SpringTest.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.clomez</groupId>
    <artifactId>SpringTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

家庭控制器

package com.clomez.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by Clomez-admin on 14.4.2017.
 */

@Controller
public class HomeController {

    @RequestMapping(value = "/")
    public String home(){

        return "home";
    }
}

WebInit.class

package com.clomez.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * Created by Clomez-admin on 14.4.2017.
 */

@Configuration
public class WebInit  extends AbstractAnnotationConfigDispatcherServletInitializer{
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{RootConfig.class};
    }
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

RootConf.class

package com.clomez.config;

import org.springframework.context.annotation.Configuration;

/**
 * Created by Clomez-admin on 14.4.2017.
 */

@Configuration
public class RootConfig {
}

WebConfig.class

package com.clomez.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.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * Created by Clomez-admin on 14.4.2017.
 */

@Configuration
@EnableWebMvc
@ComponentScan("com.clomez")
public class WebConfig extends WebMvcConfigurerAdapter{
    @Bean
    public InternalResourceViewResolver resolver(){

        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

请问有人可以帮我吗?两个月后,我大部分时间都觉得这个 Spring 框架的东西得了癌症,但我别无选择,如果我想完成我的学位。

Tomcat 运行流畅,编辑器中没有任何错误或警告,并打开浏览器并显示 404 - home.jsp not found。

【问题讨论】:

  • 你有 home.jsp 吗?

标签: java spring maven spring-mvc intellij-idea


【解决方案1】:

我在 Wildfly 10 上运行了你的代码,在我将 home.jsp 放入我的 /src/main/webapp/ 目录后,我得到了它的内容。

09:55:10,036 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 69) 1 Spring WebApplicationInitializers detected on classpath
09:55:10,105 INFO  [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 69) Initializing Mojarra 2.2.13.SP1 20160303-1204 for context '/SpringTest-0.0.1-SNAPSHOT'
09:55:10,808 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 69) Initializing Spring root WebApplicationContext
09:55:10,809 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 69) Root WebApplicationContext: initialization started
09:55:10,818 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 69) Refreshing Root WebApplicationContext: startup date [Fri Apr 14 09:55:10 EDT 2017]; root of context hierarchy
09:55:10,852 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 69) Registering annotated classes: [class com.clomez.config.RootConfig]
09:55:10,962 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 69) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
09:55:11,018 INFO  [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 69) Root WebApplicationContext: initialization completed in 209 ms
09:55:11,027 INFO  [io.undertow.servlet] (ServerService Thread Pool -- 69) Initializing Spring FrameworkServlet 'dispatcher'
09:55:11,027 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 69) FrameworkServlet 'dispatcher': initialization started
09:55:11,030 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 69) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Apr 14 09:55:11 EDT 2017]; parent: Root WebApplicationContext
09:55:11,030 INFO  [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 69) Registering annotated classes: [class com.clomez.config.WebConfig]
09:55:11,116 INFO  [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 69) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
09:55:11,270 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 69) Mapped "{[/]}" onto public java.lang.String com.clomez.controller.HomeController.home()
09:55:11,481 INFO  [org.hibernate.validator.internal.util.Version] (ServerService Thread Pool -- 69) HV000001: Hibernate Validator 5.2.4.Final
09:55:11,539 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] (ServerService Thread Pool -- 69) Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Apr 14 09:55:11 EDT 2017]; parent: Root WebApplicationContext
09:55:11,615 INFO  [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 69) FrameworkServlet 'dispatcher': initialization completed in 588 ms
09:55:11,616 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 69) WFLYUT0021: Registered web context: /SpringTest-0.0.1-SNAPSHOT
09:55:11,635 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "SpringTest-0.0.1-SNAPSHOT.war" (runtime-name : "SpringTest-0.0.1-SNAPSHOT.war")

当您在HomeController 中返回"home" 时,您在WebConfig 中设置的InternalResourceViewResolver 用于解析针对内部结构或WAR 的jsps(您的war 文件的默认基础为@987654329 @ 由 maven 添加)。

编辑:

为了向您确认,我下载了 Tomcat 9.0.0.M19,当我请求 http://localhost:8080/SpringTest-0.0.1-SNAPSHOT/ 时收到 404。我会快速旋转一下 8。

编辑 2:

Tomcat 8.5.13 也返回 404。 . . . 8.5 和 9 都支持 Servlet 3 规范,但我猜当它与这些较新的 Spring 特性发生冲突时,它会在某个地方出现问题。我发现Spring Java Config: Tomcat deploy without web.xmlhttp://docs.spring.io/autorepo/docs/spring-framework/4.3.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html 注意到:

Tomcat下映射到'/'

Apache Tomcat 将其内部 DefaultServlet 映射到“/”,并且在 Tomcat 版本

我确实尝试将您的映射从 / 更改为 /home,但 Tomcat 仍然不高兴(Wildfly 是)。

如果我发现任何内容,将会进行更多修改。

编辑 3:

嗯。嗯,我有点目瞪口呆。使用 Tomcat 9 进行测试,而不是请求 http://localhost:8080/SpringTest-0.0.1-SNAPSHOT/ 尝试转到 http://localhost:8080/SpringTest/。使用home.jsp,如上所述,您的代码可以正常工作!

编辑 4(叹气):

为了完整性,也许只是因为我使用的是 Eclipse,我不得不添加 2 位 maven 构建插件配置:

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

如果以前没有,我不认为这些现在会影响到你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2019-04-15
    • 2019-05-06
    • 2017-08-27
    相关资源
    最近更新 更多