我在我的网络应用程序中使用Spring MVC和Freemarker 2.3.27,当我尝试显示俄文时,我只有“?????”。我在utf-8中保存了我的ftl页面并添加了meta:
<!DOCTYPE html>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<title>Registration</title>
<#assign contextPath = request.contextPath>
</head>
<body>
<div class="panel-heading">
<h3 class="panel-title">Зарегистрируйтесь</h3>
</div>
<div class="panel-body">
<form method="post" action="" name="saveUser">
<input name="${_csrf.parameterName}" value="${_csrf.token}" type="hidden">
<div class="form-group">
<@spring.bind "userForm.username"/>
<div class="alert-link">
<#if spring.status.error>
<div class="alert alert-danger" role="alert">
<#list spring.status.errorMessages as error>
<li>${error}</li>
</#list>
</div>
</#if>
</div>
<label for="username">Username</label>
<input type="text" class="form-control" id="username" placeholder="Username" name="username">
</div>
...
</form>
</div>
<body/>
<html/>
这是我的Web配置:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "aquaplant")
public class WebConfig extends WebMvcConfigurerAdapter {
.....
@Bean
public FreeMarkerConfigurer getFreeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new DefaultFreeMarkerConfigurer();
configurer.setDefaultEncoding("UTF-8");
configurer.setTemplateLoaderPaths("/", "/WEB-INF/views");
return configurer;
}
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource getMessageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:validation");
messageSource.setDefaultEncoding("UTF-8");
//messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
}
在我的整个程序中,我使用utf-8。它应该工作,但事实并非如此。有谁能够帮我?
通过创建这样的过滤器解决了这个问题:
public class CharsetFilter implements Filter {
private String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("requestEncoding");
if (encoding == null) encoding = "UTF-8";
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request.getCharacterEncoding()==null) {
request.setCharacterEncoding(encoding);
}
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
必须将此过滤器添加到servletContext:
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
CharsetFilter filter = new CharsetFilter();
servletContext.addFilter("charsetFilter", filter).addMappingForUrlPatterns(null, false, "/*");
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(SpringConfig.class, WebConfig.class);
context.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher11 = servletContext.
addServlet("dispatcher11", new DispatcherServlet(context));
dispatcher11.setLoadOnStartup(1);
dispatcher11.addMapping("/");
}
}