0
我有UTF-8字符(例如ěčžýáí)的問題。我發現堆棧溢出的一些解決方案,但它不適合我。這是我的Conf類。我沒有使用web.xml,一切都是通過註釋。我無法在Spring MVC中獲得正確的UTF-8編碼
@Configuration
@EnableWebMvc
@Import({ SecurityConfig.class })
@ComponentScan(basePackages = "cz.prosvaly")
public class AppConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
@Bean(name="multipartResolver")
public CommonsMultipartResolver multipartResolver(){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(10000000);
return multipartResolver;
}
}
這裏是我的編碼過濾器:
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(container);
container.addListener(new ContextLoaderListener(ctx));
FilterRegistration.Dynamic fr = container.addFilter("encodingFilter",
new CharacterEncodingFilter());
fr.setInitParameter("encoding", "UTF-8");
fr.setInitParameter("forceEncoding", "true");
fr.addMappingForUrlPatterns(null, true, "/*");
ServletRegistration.Dynamic servlet = container.addServlet(
"dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
調度配置:
@Configuration
public class DispatcherConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
// registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/");
// registry.addResourceHandler("/html/**").addResourceLocations("/html/");
// registry.addResourceHandler("/images/**").addResourceLocations("/images/");
}
}
春季安全
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).
usersByUsernameQuery("select username,password, enabled from admin where username=?").
authoritiesByUsernameQuery("select username, role from user_roles where username =? ")
.passwordEncoder(new Md5PasswordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
.and().formLogin()
.loginPage("/login").defaultSuccessUrl("/admin/goods").failureUrl("/login?error")
.usernameParameter("username").passwordParameter("password")
.and().logout().logoutUrl("/logout").logoutSuccessUrl("/login?logout")
.and().csrf();
}
}
public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
//do nothing
}
在形式上,我傳送給服務器上的我有這個taglib:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
我試圖更新在Tomcat中此行:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
但是,當我提交表單我看到這個在日誌中
....type for žÄáÞÃé
您使用哪種HTTP方法提交? POST或GET? UREncoding =「UTF-8」用於在CharacterEncodingFilter用於後期獲取。 –
在SO上以這種方式標記解決方案並不是慣例。發佈解決方案作爲答案。 – holmis83