【问题标题】:Spring 4 Java based config with HTML基于 Spring 4 Java 的 HTML 配置
【发布时间】:2017-04-25 14:48:35
【问题描述】:

我正在尝试从基于 java 的 Spring 4 配置项目调度到 html(而不是 jsp)。

这是 ApplicationContextConfig

@Configuration
@EnableWebMvc
@ComponentScan("net.codejava.spring")
@EnableTransactionManagement
public class ApplicationContextConfig {
    @Bean(name = "viewResolver")
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/html/");
        viewResolver.setSuffix(".html");
        return viewResolver;
    }

这是 SpringWebAppInitializer

public class SpringWebAppInitializer extends WebMvcConfigurerAdapter implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(ApplicationContextConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/images/**").addResourceLocations("/images/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
    }

}

这是 webapp 文件夹:

这是 Spring 控制器:

@Controller
public class HomeController {

@RequestMapping("/")
    public ModelAndView showLogin(){
        return new ModelAndView("login");
    }
}

但是我收到了这个警告:

在名称为“SpringDispatcher”的 DispatcherServlet 中找不到带有 URI [/spring/html/login.html] 的 HTTP 请求的映射

而且 html 不显示。

【问题讨论】:

    标签: java html spring spring-mvc


    【解决方案1】:

    你的 html 文件在哪里?

    像 WEB-INF/views/login.html 一样放置你的 html 文件

    使用映射

    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".html");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 2014-09-25
      • 2012-02-01
      • 2015-05-09
      • 2018-02-13
      相关资源
      最近更新 更多