Tag: thymeleaf

使用@Configuration和@Controller注释一个类。 需要帮助重构

下面是我必须同时使用@Configuration和@Controller类,因为在整个应用程序中应该只有一个Thymeleaf实例,否则我会得到例外。 我的其他类用@RequestScope注释,所以我不能使用单例作用域bean。 所以我有一个配置和控制器的混合来获得结果,但我觉得这是一个不好的做法。 我将不胜感激任何帮助重构代码并删除不良做法。 UPDATE 我使用的是spring-boot 1.5.14 。 我使用以下方法处理模板并将处理后的模板保持为字符串。 @Controller @Configuration @EnableWebMvc @ApplicationScope public class MyThymeleafConfig { @GetMapping(“/view-template”) @ResponseBody public void viewTemplates() { Context context = new Context(); context.setVariable(“mydata”, “this is it”); String html = templateEngine().process(“templates/view-to-process.html”, context); System.out.println(html); } /* configuration for thymeleaf and template processing */ @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = […]

Thymeleaf没有显示弹簧控制器的值

我是第一次练习百里香模板引擎。 我已经按照教程等等,但我不知道我哪里出错了。 我的控制器: public String mainPage(Model model){ model.addAttribute(“data”, “Hello Thymeleaf”); return “main”; } 我的HTML如下: th:text=”${data}” 当我点击localhost时,它显示th:text =“$ {data}”而不是Hello Thymeleaf “${data}” 也不起作用。 查看解析程序配置必须正确,因为它将main解析为main.html。 我正在使用spring4 SpringTemplateEngine和spring4百万美元视图解析器。 提前致谢

如何将对象列表绑定到百万美元的复选框?

我在使用对象列表绑定复选框输入时遇到了很多困难。问题是当我在输入字段类型复选框上添加th:field =“* {userRole}”时 ,我在Web浏览器上收到错误请求作为响应。 这是我的代码: 用户模型类: public class User implements Serializable { private Integer id; private String username; private String password; private boolean enabled; private List userRole = new ArrayList(0); } UserRole Model类: public class UserRole implements Serializable { @Id @GeneratedValue private Integer userRoleId; @ManyToMany(mappedBy = “userRole”) private List users; @Column(name = “role”, nullable = […]

与Thymeleaf的utf8 charset

当使用Spring和Thymeleaf时,我所有的西里尔字符都显示为????? 在页面上。 运用 @RequestMapping(value = “/login”, method = RequestMethod.GET, produces = “text/html; charset=utf-8”) 正如在此建议的那样: https : //stackoverflow.com/a/11866822/1479414 ,这里: https : //stackoverflow.com/a/12023816/1479414没有帮助。 如何解决这个问题?

LocalDateforms

我有一个关于Spring + Thymeleaf日期格式的问题。 我有一个带有LocalDate date字段的简单实体。 我希望从表单中的用户获取此日期并将其保存到MySQL数据库。 我收到这样的错误: 无法将类型为java.lang.String的属性值转换为属性日期所需的类型java.time.LocalDate; 嵌套exception是org.springframework.core.convert.ConversionFailedException:无法从类型java.lang.String转换为类型java.time.LocalDate,值为2019-04-30; 嵌套exception是java.time.format.DateTimeParseException:无法在索引2处解析文本2019-04-30 我的实体: @Entity @Table(name=”game”) public class Game{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Transient private User gameOwner; private LocalDate date; private LocalTime time; //other fields Thymeleaf观点/forms: Date: 这个问题的原因是什么? 也许还有其他更好的存储日期方式?

Thymeleaf + Spring(不启动) – 如何显示来自messageSource的消息

我在使用Thymeleaf设置我的Spring MVC(不使用Boot,因为我在发现Spring Initializr之前启动它)以显示来自我的资源包的消息时遇到了问题。 该应用程序的主要配置类是: @Configuration @EnableWebMvc @ComponentScan(basePackages = “bcmh”) public class HrmConfiguration extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); templateResolver.setTemplateMode(“HTML5”); templateResolver.setCharacterEncoding(“UTF-8”); templateResolver.setPrefix(“/WEB-INF/html/”); templateResolver.setSuffix(“.html”); SpringTemplateEngine engine = new SpringTemplateEngine(); engine.addDialect(new SpringSecurityDialect()); engine.addDialect(new LayoutDialect(new GroupingStrategy())); engine.setTemplateResolver(templateResolver); ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(engine); viewResolver.setCache(false); return viewResolver; } @Bean(name = “messageSource”) public MessageSource […]

Spring Boot + Thymeleaf ERROR java.lang.ClassNotFoundException:org.thymeleaf.dom.Attribute

我有使用Spring Boot和thymeleaf的以下问题,我无法解决它。 在错误发生后立即运行Application类时发生错误。 我与百里香的布局在于 主\资源\模板 HomeController的 @Controller public class HomeController { @GetMapping(“/”) public String root() { return “page/home”; } } 应用 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } POM org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf nz.net.ultraq.thymeleaf thymeleaf-layout-dialect org.springframework.boot spring-boot-devtools true LOG ERROR […]

Spring启动使用Thymeleaf作为模板发送电子邮件 – 配置不起作用

我在基于Spring Boot(最新的1.1.5.RELEASE)和Thymeleaf的WebApp上工作。 现在我想添加发送电子邮件的function,并使用Thymelaf作为模板引擎。 在pom.xml中我添加: org.springframework.integration spring-integration-mail javax.mail mail 1.4.7 javax.activation activation 1.1.1 遵循本教程: http : //www.thymeleaf.org/doc/articles/springmail.html 得到完整的(没有XML)Java配置: @Configuration public ThymeleafReplaceConfigurator { @Bean public JavaMailSender getJavaMailSenderImpl(){ JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); Properties props = new Properties(); /* some properties here */ javaMailSender.setJavaMailProperties(props); return javaMailSender; } @Bean public ClassLoaderTemplateResolver emailTemplateResolver(){ ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver(); emailTemplateResolver.setPrefix(“/mails/”); emailTemplateResolver.setSuffix(“.html”); […]

Thymeleaf:用替换换行符

我有一个字段( ),可能包含换行符,我想用它们的HTML替换它们: 。 我怎样才能做到这一点? 我正在使用Thymeleaf 2.1.4.RELEASE。

带有Thymeleaf的SpringBoot – 未找到css

首先要说的是,我现在一直在寻找解决方案,现在我非常绝望。 在Spring Boot运行时,我无法从html页面访问css文件。 html.file AntiIntruder … Application.java @SpringBootApplication // adds @Configuration, @EnableAutoConfiguration, @ComponentScan @EnableWebMvc public class Application extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(“/assets/**”).addResourceLocations(“classpath:/assets/*”); } } 文件夹结构: 我已经尝试将css文件夹放入static文件夹和/或删除addResourcesHandlers,通过相对路径和其他一些东西引用css。 似乎没有解决这个问题。 请让我知道,如果你试图解决这个问题,但没有找到解决办法,那么我知道,我不会被忽视。