2015-12-21 50 views
2

我使用spring 4和tomcat 8.我的编码有问题。我输入一些值并提交表单。在控制器中,我的参数不是UTF-8编码。查看控制器上的评论。Tomcat Spring UTF-8

这是我的JSP的一部分:

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" language="java" %> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> ... 
    <form:form modelAttribute="category" htmlEscape="false" id="categoryUpdateForm" method="post" acceptCharset="UTF-8" cssClass="form-horizontal"> 
     <c:if test="${not empty message}"> 
      <div id="message" class="${message.type}"> 
       <c:out value="${message.message}" /> 
      </div> 
     </c:if> 
     <div class="form-group"> 
      <div class="col-md-3 text-right"> 
       <label> 
        Name 
       </label> 
      </div> 
      <div class="col-md-6"> 
       <form:input path="name" cssClass="form-control" /> 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-3 text-right"> 
       <label> 
        Parent category 
       </label> 
      </div> 
      <div class="col-md-6"> 
       <form:select path="parentId" id="parentId" class="form-control "> 
        <form:option value="" selected="true">Without category</form:option> 
        <c:if test="${not empty categories}"> 
         <form:options items="${categories}" itemValue="id" itemLabel="name" /> 
        </c:if> 
       </form:select> 
      </div> 
     </div> 
     <div> 
      <a class="btn btn-default" href="<%=MVCConstants.Url.ADMIN + MVCConstants.Url.Admin.CATEGORIES%>"> 
           cancel 
          </a> 
      <button type="submit" class="btn btn-primary"> 
       save 
      </button> 
     </div> 
    </form:form> 

,这是我的控制器:

@Controller 
@RequestMapping("/admin") 
public class CategoryController { 
    ... 
    @RequestMapping(value = MVCConstants.Url.Admin.CATEGORY_EDIT, method = RequestMethod.POST) 
    public String updateCategory(Category category, 
           BindingResult result, 
           HttpServletRequest request, 
           Model model) { 

     String item = request.getParameter("name"); 
     byte[] bytes = item.getBytes(StandardCharsets.ISO_8859_1); 
     item = new String(bytes, StandardCharsets.UTF_8); 


     Logger.error("cat. Name: " + category.geName()); // ÐÐ±Ñ 
     Logger.error("req. Name: " + request.getParameter("name")); // ÐÐ±Ñ 
     Logger.error("item: " + item); // Correct value! 
     Logger.error("request.getCharacterEncoding(): " + request.getCharacterEncoding()); //UTF-8 
     try { 
      request.setCharacterEncoding("UTF-8"); 
     } catch (UnsupportedEncodingException e) { 
      Logger.error("can't change request encoding",e); 
     } 
     Logger.error("e. req. UTF-8. name: " + request.getParameter("name")); // ÐÐ±Ñ 
     Logger.error("request.getCharacterEncoding(): " + request.getCharacterEncoding()); // UTF-8 
     model.asMap().clear(); 
     categoryServiceImpl.save(category); 
     return MVCConstants.Views.Admin.CATEGORIES; 
    } 
} 

我有UTF-8过滤器:

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 
... 
@Override 
    protected Filter[] getServletFilters() { 
     CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); 
     characterEncodingFilter.setEncoding("UTF-8"); 
     return new Filter[] { characterEncodingFilter}; 
    } 
} 

,我在设置URIEncoding="UTF-8"server.xml

<Connector port="8080" protocol="HTTP/1.1" 
       connectionTimeout="20000" 
       redirectPort="8443" 
       maxPostSize="20971520" 
       URIEncoding="UTF-8" /> 

连接器p80也一样。

你能告诉我什么是错?

+0

请尝试使标题更具体到您的问题。 – Ram

回答

1

CharacterEncodingFilter必须在SecurityFilter之前启动。我把它移动到安全配置:

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    @Qualifier("userDetailsService") 
    UserDetailsService userDetailsService; 

    @Autowired 
    SuccessLoginHandler successLoginHandler; 

    @Autowired 
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService); 
     auth.authenticationProvider(authenticationProvider()); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter(); 
     encodingFilter.setEncoding("UTF-8"); 
     encodingFilter.setForceEncoding(true); 

     http.addFilterBefore(encodingFilter,CsrfFilter.class); 

     ...