英文:
spring boot application run, but I get error 404 in the web browser
问题
以下是翻译好的内容:
spring boot应用程序运行,但在Web浏览器中出现404错误,前端使用了jsp。该应用程序最初是使用Spring MVC开发的,然后我将其转换为Spring Boot。转换后,应用程序按预期运行,但在浏览器中看不到数据。**"此应用程序对 /error 的映射没有明确设置,因此您正在将其作为回退显示。
2020年5月28日,GMT-06:00
发生了意外错误(类型=未找到,状态=404)。
无可用消息"**
我有一个WebMvcConfigurer类,其中包含了我的视图解析器,我需要它吗?
Thymeleaf不需要视图解析器,对于jsp也是一样吗?
这是我的类:
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);
}
}
英文:
spring boot application run, but I get 404 error in web browser, for frontend I have used jsp. The app was developed in spring mvc then I converted it to spring boot. after conversion is run as expected, but I do not see the data in browser. "This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu May 28 18:07:37 GMT-06:00 2020
There was an unexpected error (type=Not Found, status=404).
No message available"
I have a WebMvcConfigurer class, it contains my view resolver do I needs it?
Thymeleaf does not need a view Reslover, is it the same for jps, too?
here my class:
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);
}
}
答案1
得分: 0
是的,您确实需要在视图解析器中使用后缀和前缀,因为内部视图解析器使用前缀和后缀来为您的视图进行逻辑映射,但您也可以在 application.properties
文件中进行配置,如下所示:
spring.mvc.view.prefix = /WEB-INF/classes/templates
spring.mvc.view.suffix = .jsp
此外,嵌入式的 Tomcat 包(在 Spring Boot 中用于创建可执行 JAR 文件)默认不包含 JSP,因此我们必须将模块 "org.apache.tomcat.embed:tomcat-embed-jasper" 作为依赖添加。我们在 Spring Boot 中添加 tomcat-embed-jasper 依赖的原因是可以在 JSP 中使用 JSTL 标签。JSP 文件需要被编译成 HTML。
另外,由于您从 Spring MVC 切换到了 Spring Boot,请确保从项目中移除注解 @EnableWebMvc
,因为这会禁用所有 Spring Boot 的自动配置。除非您明确地自己提供此注解,以便像在 Spring MVC 中一样手动处理所有配置。
另外,Spring Boot 和 JSP 在使用 jar
作为打包方式时并不完全兼容,所以如果您在视图模板中使用 JSP,尝试使用 war
打包。Webapp 文件夹的内容只有在将打包方式设置为 war
时才会在构建过程中自动包含进来。
英文:
Yeah you do need the suffix and prefix for the view resolver as the internal view resolver uses the prefix and suffix to make a logical mapping for your view , but you could also provide it in the application.properties
file like :
spring.mvc.view.prefix = /WEB-INF/classes/templates
spring.mvc.view.suffix = .jsp
Also, Embedded Tomcat package (which is used in springboot to create executable jar)does not include JSP by default, we must add the module “org.apache.tomcat.embed:tomcat-embed-jasper
” as dependency as well.The reason why we are adding tomcat-embed-jasper as dependency in springboot, is that we can use the jstl tags in jsp.The jsp files needs to be transpiled into html.
- Also, since you moved to spring-boot from spring mvc make sure you
removed the annotation@EnableWebMvc
from your project as this will
disable all spring-boot autoconfigurations.Unless, you are explicitly providing it yourself to handle all configurations manually like in spring-mvc. - Also spring-boot and JSP quite doesn't work together with
jar
as the packaging so try to usewar
packaging if you are using jsp as the view template.The contents of the webapp folder is automatically taken up during build only if you keep the packaging aswar
.
专注分享java语言的经验与见解,让所有开发者获益!
评论