春季启动应用程序运行,但在网络浏览器中出现错误404

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

spring boot应用程序运行,但是在Web浏览器中出现404错误,对于前端,我使用了jsp。该应用程序是在spring mvc中开发的,然后将其转换为spring boot。转换按预期运行后,但在浏览器中看不到数据。 “此应用程序没有针对/ error的显式映射,因此您将其视为备用。2020年5月28日星期四18:07:37 GMT-06:00发生意外错误(类型=未找到,状态= 404)。没有可用的消息“

我有一个WebMvcConfigurer类,它包含我的视图解析器,我需要吗?Thymeleaf不需要视图Reslover,jps也一样吗?这是我的课:

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.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

import thursday.com.todolist.util.ViewNames;



public class WebConfig implements WebMvcConfigurer {
    private static final String RESOLVER_PREFIX = "/templates/WEB-INF/view/";
    private static final String RESOLVER_SUFFIX = ".jsp";

    @Bean
    public ViewResolver viewResolver(){
        UrlBasedViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix(RESOLVER_PREFIX);
        viewResolver.setSuffix(RESOLVER_SUFFIX);
        return viewResolver;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName(ViewNames.HOME);
    }
}
java spring spring-boot
1个回答
0
投票

是的,您确实需要视图解析器的后缀和前缀,因为内部视图解析器使用前缀和后缀来为您的视图进行逻辑映射,但是您也可以在application.properties文件中提供它,例如:

spring.mvc.view.prefix = /WEB-INF/classes/templates
spring.mvc.view.suffix = .jsp

此外,Embedded Tomcat软件包(在springboot中用于创建可执行jar)默认情况下不包括JSP,我们也必须添加模块“ org.apache.tomcat.embed:tomcat-embed-jasper”作为依赖项。之所以添加tomcat-embed- jasper作为springboot中的依赖项,是我们可以在jsp中使用jstl标记.jsp文件需要转换为html。

  • 此外,由于您已从spring mvc转移到spring-boot,因此请确保您从您的项目中删除了注释@EnableWebMvc,因为这将禁用所有spring-boot自动配置。除非您明确提供自己,以手动处理spring-mvc中的所有配置。
  • 而且spring-boot和JSP不能与jar一起使用,因此如果将jsp用作视图模板,请尝试使用war封装。webapp文件夹的内容在处理过程中会自动被占用如果将包装保留为war,则构建only
© www.soinside.com 2019 - 2024. All rights reserved.