Spring MVC主目录404错误Intellij Idea

问题描述 投票:1回答:2

我正在学习Spring MVC,而且几乎在开始时就陷入困境。我已经关注了有关创建第一个MVC应用程序的视频,但这对我不起作用。我想将main.jsp作为起始页面,在配置之后但是页面没有加载,我一直都是404。我已经创建了一个新的Maven项目。然后我为项目添加Spring MVC框架支持,为Spring配置创建web.xml和spring-mvc-demo-servlet。我已经多次检查过我的项目,但没有成功。此外,我已检查过类似的主题,但也无法找到解决方案。

Project Structure Image

pom.hml

<?xml version="1.0" encoding="UTF-8"?>

HTTP://maven.Apache.org/下水道/maven-4.0.0.下水道"> 4.0.0

<groupId>group</groupId>
<artifactId>artifact</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <spring.version>5.0.4.RELEASE</spring.version>
</properties>



<dependencies>

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

    <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>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
</dependencies>

VEB.HML

<?xml version="1.0" encoding="UTF-8"?>

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd“id =”WebApp_ID“version =”3.1“>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Spring的MVC-演示servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.test.spring" />

<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>

<!-- Step 5: Define Spring MVC view resolver -->
<bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/" />
    <property name="suffix" value=".jsp" />
</bean>

HomeController的

package com.test.spring;

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

@Controller
public class HomeController {

    @RequestMapping("/")
    public String showPage() {
        return "main";
    }
java spring maven spring-mvc model-view-controller
2个回答
0
投票

我认为WebApplicationInitalizer从Spring 5和Intellij开始更简单。

1)将WebMvcConfig类添加到com.test.spring包中,如下所示:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.test.spring"})
public class WebMvcConfig implements WebMvcConfigurer {

   @Bean
   public InternalResourceViewResolver resolver() {
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
      resolver.setViewClass(JstlView.class);
      resolver.setPrefix("/WEB-INF/views/");
      resolver.setSuffix(".jsp");
      return resolver;
   }

2)将您的WEB-INF文件夹移动到一个新文件夹src/main/webapp(mabe,你可以通过右键单击它将它作为源文件夹)

3)删除您的web.xmlspring-mvc-demo-servlet.xml文件以及所有旧的Web文件夹

4)添加应用程序初始化程序

public class AppInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

   @Override
   protected String[] getServletMappings() {
      return new String[] { "/" };
   }
}

最后右键单击AppInitializer类,然后运行它...

Optionnally,您可以将Tomcat添加到Maven pom.xml,然后运行命令mvn tomcat7:run

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/</path>
            </configuration>
        </plugin>
    <plugins>
<build>

希望对你有帮助


0
投票

感谢你的回答Karbos,但我必须找到完全针对我提供的来源的解决方案,否则我将无法继续Udemy的课程......

我解决了这个问题,但最令人毛骨悚然的一刻是,我不知道我是如何做到的。我在项目中添加了一些更改。 web.xml中的更改

<servlet>
    <servlet-name>my-dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

pom.xml中的更改

<packaging>war</packaging>

和其他一些我可能没有注意到原因是自动完成的。

所以这个问题花了我一天半的时间。觉得自己这么迟钝......

© www.soinside.com 2019 - 2024. All rights reserved.