Tag: thymeleaf

如何在使用Spring Boot时使用SpringTemplateEngine

我正在使用Thymeleaf SpringTemplateEngine在我的Spring应用程序上创建HTML电子邮件。 当我使用纯Spring MVC时,一切都很完美。 现在使用Spring Boot,类无法找到我的.html模板。 我认为问题在于ServletContext没有返回正确的路径,但我无法弄清楚如何解决它。 那么我应该使用另一个Context来处理模板吗? 哪一个? 这是我的MailService为纯Spring MVC工作。 @Service public class MailService { private JavaMailSenderImpl mailSender; private SpringTemplateEngine templateEngine; public MailService() { mailSender = new JavaMailSenderImpl(); //mailSender config templateEngine = new SpringTemplateEngine(); Set templatesResolvers = new HashSet(); ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver(); emailTemplateResolver.setPrefix(“mail/”); emailTemplateResolver.setTemplateMode(“HTML5”); emailTemplateResolver.setCharacterEncoding(“UTF-8”); emailTemplateResolver.setOrder(1); templatesResolvers.add(emailTemplateResolver); ServletContextTemplateResolver webTemplateResolver = new ServletContextTemplateResolver(); […]

Spring Boot / Thymeleaf / Hibernate:带有Java注释的Sessionfactory Bean

我使用Thymeleaf和Hibernate创建了一个带有IntelliJ的Spring Boot Web应用程序。 我到目前为止,我可以创建所有数据库连接,它工作正常。 据我所知,将Sessionfactory作为bean并在所有执行db操作的Service Classes中自动assembly它是一种好方法。 我有一个SpringMvcConfiguration作为配置文件,如下所示: package eu.barz.familykurse.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; 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 java.util.Locale; @Configuration public class SpringMvcConfiguration extends WebMvcConfigurerAdapter{ @Bean public LocaleResolver localeResolver(){ SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver(); sessionLocaleResolver.setDefaultLocale(Locale.GERMAN); return sessionLocaleResolver; } @Bean LocaleChangeInterceptor localeChangeInterceptor(){ LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName(“lang”); return localeChangeInterceptor; […]

将图片存储在H2数据库spring boot thymleaf中

美好的一天。 我想将图像存储在h2数据库中,然后在html页面中检索并显示相同的图像。 我正在使用spring boot和文件上传方法,但是我在绑定结果中遇到错误 这是页面/类: Category.java package com.vishal.project.entities; @Entity @Table(name=”category”) public class Category implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name=”ID”) private Long id; @Size(min=1, max=90) @Column(name=”CATEGORY_NAME”) private String CategoryName; @Lob @Column(name=”CATEGORY_PHOTO”) private byte[] CategoryPhoto; public Category(Long id, @Size(min = 1, max = 90) String categoryName, byte[] categoryPhoto) { […]

如何从Thymeleaf调用对象的方法?

我的模板看不到从Spring传递的对象。 我的代码: public class PublicModelAndView extends ModelAndView { @Autowired TemplateModulesHandler templateModulesHandler; public void init() { setViewName(“index”); CSSProcessor cSSProcessor = new CSSProcessor(); cSSProcessor.setSiteRegion(“public”); super.addObject(“CSSProcessor”, cSSProcessor); JSProcessor jSProcessor = new JSProcessor(); super.addObject(“JSProcessor”, jSProcessor); templateModulesHandler.setPublicModelAndView(this); } } 控制器的代码: @SpringBootApplication @Controller public class IndexPage { @Autowired PublicModelAndView publicModelAndView; @Autowired OurServicesBean ourServicesBean; @Autowired PortfolioBean portfolioBean; @RequestMapping(value = “/”, method = […]

Thymeleaf多个提交按钮以一种forms

我有一个HTML页面的片段,其中包含一个表单和2个按钮: save cancel 和控制器: @RequestMapping(value=”/edit”, method=RequestMethod.POST) public ModelAndView edit(@ModelAttribute SomeModel model, @RequestParam(value=”action”, required=true) String action) { if (action.equals(“save”)) { // do something here } if (action.equals(“cancel”)) { // do another thing } return modelAndView; } 这项工作很好,但如果我有更多按钮,我必须添加更多if语句来检查action字符串。 还有另一种方法可以为表单中的每个按钮创建一个动作吗?

Thymeleaf:如何获取URL属性值

我找不到使用Thymeleaf从URL获取属性的任何解决方案。 例如,对于URL: somesite.com/login?error=true 我需要获得“错误”属性值。 我也在使用SpringMVC,如果有用的话。

Thymeleaf的Spring Security简单示例

嗨,我正在尝试按照一个简单的例子来做一个简单的登录表单页面,我在这个页面中找到http://docs.spring.io/autorepo/docs/spring-security/4.0.x/guides/form.html 问题是我每次尝试登录时都会收到此错误我收到此错误: Expected CSRF token not found. Has your session expired? Expected CSRF token not found. Has your session expired? 当我收到此错误时,我按下浏览器中的后退按钮并尝试第二次登录,当我这样做时,我收到此错误: HTTP 403 – Invalid CSRF Token ‘null’ was found on the request parameter ‘_csrf’ or header ‘X-CSRF-TOKEN’ 在教程页面中是这条消息: We use Thymeleaf to automatically add the CSRF token to our form. If we were not […]

Spring Boot和自定义404错误页面

在我的Spring Boot应用程序中,我正在尝试配置自定义错误页面,例如对于404,我在应用程序配置中添加了以下Bean: @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, “/404.html”)); } }; } 另外,我创建了以下简单的Thymeleaf模板: 404 Not Found 404 Not Found 404 Error java.lang.NullPointerException Back to Home Page 并将其添加到/resources/templates/文件夹中。 就在404错误我只能看到白屏。 我做错了什么以及如何正确设置我的404页面? 此外,是否可以使用模板而不仅仅是自定义错误页面的静态页面?