Mik*_*ike 1 spring jsp-tags internationalization
我找不到像处理常规文本那样将占位符内的文本国际化的方法。我想将我的国际化放在:
input type="text" class="form-control" name="s" placeHolder="Search Hobbies"
与输入类型=“文本”类=“形式控制”名称=“S”占位符=“ctrlpanel.search.placeholder”
ctrlpanel.search.placeholder=搜索爱好
现在,在我的 JSP 中,我包含了标签
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<c:set var="contextRoot" value="${pageContext.request.contextPath}" />
<c:url var="search" value="/search" />
<hr/>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<form action="${search}" method="post">
<input type="hidden" name="${_csrf.parameterName}"
value="${_csrf.token}" />
<div class="input-group input-group-lg">
<input type="text" class="form-control" name="s" placeHolder="Search Hobbies">
<span class="input-group-btn">
<button id="search-button" class="btn btn-primary" type="submit">
<spring:message code="ctrlpanel.find.button" text="default text" />
</button>
</span>
</div>
</form>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
我的配置工作正常:
package com.caveofprogramming.configuration;
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(new Locale("es", "ES"));
//sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
}
@Bean
LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry interceptorRegistry){
interceptorRegistry.addInterceptor(localeChangeInterceptor());
}
}
Run Code Online (Sandbox Code Playgroud)
由于这不起作用。
package com.caveofprogramming.configuration;
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(new Locale("es", "ES"));
//sessionLocaleResolver.setDefaultLocale(Locale.US);
return sessionLocaleResolver;
}
@Bean
LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry interceptorRegistry){
interceptorRegistry.addInterceptor(localeChangeInterceptor());
}
}
Run Code Online (Sandbox Code Playgroud)
使用 spring:message 的 var 属性,将结果绑定到页面、请求、会话或应用程序范围时使用。
<spring:message code="ctrlpanel.search.placeholder" var="searchPlaceholder"/>
Run Code Online (Sandbox Code Playgroud)
然后像这样更新您的输入字段:
<input type="text" class="form-control" name="s" placeHolder="${searchPlaceholder}">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1241 次 |
| 最近记录: |