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);
}
}
答案 0 :(得分: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。
@EnableWebMvc
,因为这将
禁用所有spring-boot自动配置。除非您明确提供自己,以手动处理所有配置,例如spring-mvc。jar
打包一起使用,因此如果您将jsp用作视图模板,请尝试使用war
打包。webapp文件夹的内容如果您将包装保留为war
,则会在构建过程中仅自动 处理。