我正在尝试使用Spring启动和Thymeleaf创建一个多语言应用程序.
我制作了几个属性文件来保存不同的消息,但我只能用我的浏览器语言显示它(我试过更改浏览器区域设置的扩展但它们似乎不起作用),无论如何我想在我的网站上放一个按钮做这个职责(改变语言),但我不知道如何或在哪里找到如何管理这个.
要告诉你我的配置:
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class I18nConfiguration extends WebMvcConfigurerAdapter {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
th:with="lang=${#locale.language}" th:lang="${lang}">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>Spring Boot and Thymeleaf</h3>
<p>Hello World!</p>
<p th:text="${nombre}"></p>
<h1 th:text="#{hello.world}">FooBar</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
messages_en_US.properties
hello.world = Hello people …Run Code Online (Sandbox Code Playgroud) 问题是我无法使这些下划线出现,我不确定是否有任何优雅的解决方案(或不太糟糕)来存档此文件。我已经考虑过设置绝对位置并使其合适,但是我不确定这是否是最佳解决方案。
我知道问题看起来没有那么详细,但是我没有更多信息可提供。
谢谢!
即时'开发'使用Spring Boot创建的应用程序,我不太确定如何管理模板.
我有两种方法可以做到这一点:
使用自定义标记并将HTML/JSP文件的值作为String返回并使用它
像这样的东西
<th:header></th:header>
<!-- This would be the header (The implementation of this tag in Java) -->
<body>
Things that are not common
<th:footer></th:footer>
<!-- This would be the footer -->
</body>Run Code Online (Sandbox Code Playgroud)
另一种方式可能是使用包括,但我不太确定如何做到这一点......
不知道是否有另一种使用Spring的方法.希望你能理解我.
先谢谢你:·).
我面临的问题是关于@Autowire存储库接口(在我的情况下是UserRepository),我不知道为什么,但是@Autowire失败了.
UserController类调用一个@Service类而这一次调用@Component(DAO类),在DAO类是@Autowiring所述@Repository.
春季靴子主要
package com.leagueofsummoners;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.HttpStatus;
import com.leagueofsummoners.persistence.interfaces.UserRepository;
@SpringBootApplication
public class LeagueofsummonersApplication {
public static void main(String[] args) {
SpringApplication.run(LeagueofsummonersApplication.class, args);
}
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, …Run Code Online (Sandbox Code Playgroud) 我正在尝试迭代Thymeleaf中的字符串列表.我想替换${nombre}变量的值加上".png",但由于某种原因,只有atts title和alt显示值.请你帮助我好吗?
<div class="modal-body" id="modal-champions-body">
<div th:each="nombre : ${listaNamesChamps}" style="float:left;">
<img src="../../img/champion_icons/${nombre}.png"
th:attr="src=@{/img/champion_icons/${nombre}.png},title=${nombre},alt=${nombre}"/>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
结果HTML
spring ×4
java ×3
spring-boot ×3
html ×2
spring-mvc ×2
thymeleaf ×2
css ×1
javascript ×1
jdbctemplate ×1