gpt4 book ai didi

java - Spring Boot不渲染JSP View

转载 作者:行者123 更新时间:2023-11-30 02:33:24 26 4
gpt4 key购买 nike

我使用 Spring Boot 1.5.3.RELEASE。这是类 MvcConfiguration

package hello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {

private static final Logger logger = LoggerFactory.getLogger(MvcConfiguration.class);

@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
logger.info("Register ViewResolver success.");
return resolver;
}

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

@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("locale");
return lci;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
super.addInterceptors(registry);
}

}

Controller :

package hello;

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

@Controller
@RequestMapping(path = "/")
public class HomeController {

@RequestMapping(method = RequestMethod.GET)
public String home(){
return "currency";
}

}

调试应用程序时,我看到日志

2017-05-02 07:41:48.970  INFO 3333 --- [           main] hello.MvcConfiguration                   : Register ViewResolver success.

但是 webapp 返回纯 JSP 文本: enter image description here

我在 build.gradle 添加了必要的依赖项。我更喜欢在独立的 Tomcat 上运行 Web 应用程序。如何解决?

最佳答案

根据 Spring Boot 文档:

JSPs should be avoided if possible, there are several known limitations when using them with embedded servlet containers.

这是链接:

http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-template-engines

关于java - Spring Boot不渲染JSP View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43728448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com