Spring Maven + IntelliJ和404

时间:2017-04-14 12:29:29

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

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

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

因为这是一个Spring应用程序,所以我不会这样做 有任何错误或警告,但在我运行项目时只有404页...

Tomcat 9在没有任何警告或错误的情况下运行,Java似乎也是如此 没有问题。运行将以太常见的方式打开浏览器404 - 找不到资源错误。

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>

的HomeController

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。

1 个答案:

答案 0 :(得分: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")

当您在"home"中返回HomeController时,您在InternalResourceViewResolver中设置的WebConfig用于解析针对内部或战争的jsps(默认情况下)你的战争文件的基础由maven添加/src/main/webapp

编辑:

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

编辑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注意到:

  

映射到&#39; /&#39;在Tomcat下

     

Apache Tomcat将其内部DefaultServlet映射到&#34; /&#34;,并且在Tomcat版本&lt; = 7.0.14上,无法以编程方式覆盖此servlet映射。 7.0.15修复了这个问题。覆盖&#34; /&#34; servlet映射也已在GlassFish 3.1下成功测试。

我确实尝试将您的映射从/更改为/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>

如果他们以前没有这些影响,我不会指望这些影响你。