有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Tomcat Spring UTF8

我用的是spring 4和tomcat 8。我对编码有问题。我输入一些值并提交表单。在控制器中,我的参数不是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};
    }
}

我在{}中设置{}:

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

接头p80也是如此

你能告诉我怎么了吗


共 (1) 个答案

  1. # 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);
    
            ...