Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法运行支持Thymeleaf的Spring Boot WebMVC_Java_Spring_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 无法运行支持Thymeleaf的Spring Boot WebMVC

Java 无法运行支持Thymeleaf的Spring Boot WebMVC,java,spring,spring-mvc,thymeleaf,Java,Spring,Spring Mvc,Thymeleaf,我已经搜索了很多,但我没有找到我的问题的答案,所以我把我的问题贴在这里。请看一看,并提出解决方案,我错了 我已经使用SpringToolSuite(STS)创建了SpringBootWebMVC项目,该项目支持thymeleaf。当我运行它时,给我“白标签错误页面”页面。这意味着找不到映射 努力: WebConfig.java package com.springthymeleaf.config; import org.springframework.boot.context.embedded

我已经搜索了很多,但我没有找到我的问题的答案,所以我把我的问题贴在这里。请看一看,并提出解决方案,我错了

我已经使用SpringToolSuite(STS)创建了SpringBootWebMVC项目,该项目支持thymeleaf。当我运行它时,给我“白标签错误页面”页面。这意味着找不到映射

努力:

WebConfig.java

package com.springthymeleaf.config;

import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

@Configuration
@ComponentScan("com.springthymeleaf")
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter  {

    @Bean
    ServletRegistrationBean servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.addUrlMappings("/console/*");
        return registrationBean;
    }

    //start Thymeleaf specific configuration
    @Bean(name ="templateResolver") 
    public ServletContextTemplateResolver getTemplateResolver() {
        ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
//      templateResolver.setPrefix("/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("XHTML");
    return templateResolver;
    }
    @Bean(name ="templateEngine")       
    public SpringTemplateEngine getTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(getTemplateResolver());
    return templateEngine;
    }
    @Bean(name="viewResolver")
    public ThymeleafViewResolver getViewResolver(){
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 
        viewResolver.setTemplateEngine(getTemplateEngine());
    return viewResolver;
    }
    //end Thymeleaf specific configuration
    @Bean(name ="messageSource")
    public MessageSource getMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("/WEB-INF/i18/thymeleafResource");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}
SecurityConfiguration.java

package com.springthymeleaf.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/").permitAll();
    }
}
package com.springthymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringThymeLeafApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringThymeLeafApplication.class, args);
    }
}
ServletInitializer.java

package com.springthymeleaf;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringThymeLeafApplication.class);
    }

}
springapplication.java

package com.springthymeleaf.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.authorizeRequests().antMatchers("/").permitAll();
    }
}
package com.springthymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringThymeLeafApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringThymeLeafApplication.class, args);
    }
}
IndexController.java

package com.springthymeleaf.controllers;


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

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(){
        return "index";
    }
}
我已经在resources/templates文件夹中创建了
index.html
文件。但我还是犯了那个错误。我在网上搜索了很多,但没有找到线索。请有人帮帮我


Spring Boot已经为您配置了Thymeleaf,因此无需手动配置。删除所有与Thymeleaf相关的配置,同时删除
@EnableWebMvc
,因为这会干扰Spring引导自动配置。
@ComponentScan
也是冗余的

Spring Boot还为您注册了一个
MessageSource
,因此无需进行配置。不确定servlet注册是什么,但这是您唯一需要的

另外,我建议删除控制器并使用视图控制器,您可以在
WebConfig
类中配置该控制器。为您保存一个控制器

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter  {

    @Bean
    ServletRegistrationBean servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();
        registrationBean.addUrlMappings("/console/*");
        return registrationBean;
    }

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
}
要让自动配置的消息源拾取自定义捆绑包,请将以下内容添加到
src/main/resources/application.properties

spring.messages.basename=/WEB-INF/i18/thymeleafResource
我还建议简单地让
springbootservleafapplication
扩展
springbootservleinitializer

@SpringBootApplication
public class SpringThymeLeafApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SpringThymeLeafApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringThymeLeafApplication.class);
    }
}

另外,请确保模板位于
src/main/resources/templates
中,而不是
src/main/resources/resources/templates
中,否则将找不到这些模板。

添加thymeleaf依赖项时,Spring boot会执行所有自动配置。那么你应该做以下的事情

  • 删除WebConfig.java上的所有thymeleaf配置
  • 如果您使用的是Maven,请确保您对pom.xml具有以下依赖关系,否则,如果您使用的是gradle,请查看spring网站以了解等效的依赖关系:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
    
    org.springframework.boot
    弹簧启动装置
    
  • 第三,确保扫描控制器的位置,在SpringThymeLeafApplication.java上添加以下内容:

    @ComponentScan(basePackages=“your.path.to.controllers”)

  • 最后,您必须将.html文件添加到资源/模板中


  • 实际上,Spring Boot配置Thymeleaf是开箱即用的。它应与以下设置一起使用:

    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter
    {
        @Override
        protected void configure(HttpSecurity http) throws Exception
        {
            http
                .authorizeRequests()
                    .anyRequest().authenticated()
                    .and()
                .formLogin().loginPage("/login").defaultSuccessUrl("/").permitAll() // http://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#jc-form
                    .and()
                .logout().permitAll(); // http://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#jc-logout
        }
    
        @Override
        public void configure(WebSecurity web) throws Exception
        {
            web
                .ignoring()
                    .antMatchers("/resources/**"/*, ... */);
        }
    }
    
    @Controller
    public class LoginController
    {
        @RequestMapping("/login")
        static String login(Model model)
        {
            return "login";
        }
    }
    

    删除web配置类。Spring Boot已经为您配置了。对于I18N支持,将
    spring.messages.basename=/WEB-INF/i18/thymeleafResource
    添加到
    应用程序.properties
    。简而言之,使用框架而不是框架。我已经删除了WebConfig.java文件,现在我也不想使用任何资源,所以我没有配置资源包。在此之后,我试图运行我仍然得到相同的问题。添加您的目录结构。此外,错误页面可能意味着完全不同的内容,检查日志中是否有错误(并将堆栈跟踪/错误添加到问题中)。我已使用目录结构屏幕截图更新了我的问题。请查看并向我建议解决方案。我还在templates文件夹中添加了error.html页面。现在,每当我尝试访问任何页面时,它都只显示error.html页面,而不是实际的页面表单indexcontroller,它是模板文件夹中用于“/”映射的index.html。我已经完成了您提到的所有操作。我已经删除了所有东西,比如webconfig.java和任何与thymeleaf相关的东西。只有安全配置和引导配置。我还在模板中创建了error.html页面。不显示错误页面,而显示控制器页面。感谢Deinum没有您的帮助,我将不做进一步的操作。你的回答和Shinchillah的回答让我更进一步。在这里,我只能接受一个答案,但请投你一票。这不起作用。它将我带到没有上下文根路径的登录页。@Mandy我在LoginController注释中犯了一个错误。将“/”更改为“/登录”。。。“没有上下文根路径”是什么意思?上述设置应保护任何请求,并应重定向到“/login”下的登录页面。成功登录后,它应该重定向到“/”。在这里,我们需要更多的东西,让我做实际的项目工作。我还要感谢迪纳姆先生,没有他的指导,我将无法完成我的任务。谢谢迪纳姆。