【问题标题】:Spring MVC tomcat constant 404 error when trying to display .jsp尝试显示.jsp时出现Spring MVC tomcat常量404错误
【发布时间】:2019-08-04 16:46:15
【问题描述】:

我试图让我的网络应用程序显示除 404 错误之外的任何内容,但无法弄清楚代码有什么问题。这里是:

主控制器:

package pl.borys.controller;

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

@Controller
public class HomeController {
    @RequestMapping("/home")
    public String home(){
        return"test";

    }
}

应用配置:

package pl.borys.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "pl.borys")
public class AppConfig extends WebMvcConfigurerAdapter {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

AppInitializer:

package pl.borys.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class AppInitializer implements WebApplicationInitializer {
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);
        ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");
    }
}

文件结构如下:

src
]--main
 ]--java
 | ]--pl.borys
 |  ]--config
 |   ]--AppConfig
 |   ]--AppInitializer
 |  ]--controller
 |   ]--HelloController
 ]--webapp
 | ]--WEB-INF
 |  ]--views
 |   ]--test.jsp

无论我输入什么地址,它都会显示 404 错误。我试图通过以下地址访问 test.jsp:

http://localhost:8080/home

编辑: 这是我的 pom.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>pl.borys</groupId>
    <artifactId>springkurwamac</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>


</project>

【问题讨论】:

    标签: spring jsp tomcat model-view-controller http-status-code-404


    【解决方案1】:

    尝试使用ServletContextInitializer而不是WebApplicationInitializer在上面加上@Configuration注解并将servlet.addMapping("/")改为servlet.addMapping("/*")

    【讨论】:

    • 感谢您的回复。我应该导入什么才能使用这个类?我做了一些谷歌研究,它看起来像是用于 Spring Boot,但我没有在这里使用它。
    • 啊,对不起,无论如何,首先让我们尝试将/更改为/*
    • 不幸的是它没有解决问题。 viewResolver 中的前缀有没有可能是错误的?会不会是从错误的目录开始?我尝试在它前面添加../,但它似乎没有做任何事情。
    • 现在让我们试试这个:viewResolver.setViewClass(JstlView.class);
    • 如果我理解正确,我应该用viewResolver.setViewClass(JstlView.class); 替换viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); 对吧?
    【解决方案2】:

    好的,所以我做了一个新项目,再次下载了tomcat,问题以某种方式解决了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-19
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2018-02-01
      • 1970-01-01
      • 2015-05-06
      相关资源
      最近更新 更多