Spring MVC - Charset在我提交表单时破坏了

时间:2016-01-17 00:13:48

标签: java spring hibernate spring-mvc

解决方案:我已将服务器从tomcat更改为jetty并且charset恢复正常

我在申请时遇到了一些麻烦。每次我将数据保存到数据库时,编码都会中断,但是对于读取是可以的(我通过手动更改数据库来测试)。

正如拉尔夫问我在保存数据库之前检查了字符集是否已损坏,以及它。所以我改变问题以更好地反映问题。

我使用的是Spring MVC,Hibernate和MySQL。我已配置spring以在我的配置类中使用UTF-8:

public class ServletSpringMVC extends     AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { AppWebConfiguration.class, JPAConfiguration.class, SecurityConfiguration.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] {};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("UTF-8");
        encodingFilter.setForceEncoding(true);
        return new Filter[] {encodingFilter};
    }

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {
        registration.setMultipartConfig(new MultipartConfigElement(""));
    }


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException             {
        super.onStartup(servletContext);
        servletContext.addListener(RequestContextListener.class);
        servletContext.setInitParameter("spring.profiles.active", "dev");
    }

}

我的JSP是:

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<tags:template title="Jobs">
<jsp:attribute name="bodyArea">

<div class="container">
    <c:if test="${not empty jobId}">
        <c:url var="urlAction" value="${spring:mvcUrl('JC#updateJob').arg(0,jobId).build()}"/>
    </c:if>
    <c:if test="${empty jobId}">
        <c:url var="urlAction" value="${spring:mvcUrl('JC#addJob').build()}"/>
    </c:if>
    <form:form action="${urlAction}" method="post" modelAttribute="jobDTO" id="form" acceptCharset="utf-8">
        <input type="hidden" name="board" value="${boardId}">

        <div class="form-group">
            <label> <spring:message code="job.form.client"/> </label>
            <form:input path="client" cssClass="form-control"/>
            <form:errors path="client"/>
        </div>

        <div class="form-group">
            <label> <spring:message code="job.form.priority"/> </label>
            <form:select path="priority" cssClass="form-control">
                <form:option value="" label=""/>
                <form:options items="${priorities}"/>
            </form:select>
            <form:errors path="priority"/>
        </div>

        <div class="form-group">
            <label> <spring:message code="job.form.status"/> </label>
            <form:select path="status" cssClass="form-control">
                <form:option value="" label=""/>
                <form:options items="${statuses}"/>
            </form:select>
            <form:errors path="status"/>
        </div>

        <div class="form-group">
            <label> <spring:message code="job.form.responsible"/> </label>
            <form:select path="responsible" cssClass="form-control">
                <form:option value="" label=""/>
                <form:options items="${boardHelper.getBoardUsers(boardId)}"/>
            </form:select>
            <form:errors path="responsible"/>
        </div>

        <div class="form-group">
            <label> <spring:message code="job.form.description"/> </label>
            <form:textarea path="description" cssClass="form-control" rows="5"/>
            <form:errors path="description"/>
        </div>
    </form:form>
    <div class="container">
        <div class="row">
            <div class="col-lg-1">
                <button type="submit" class="btn btn-success" form="form">
                    <span class="glyphicon glyphicon-floppy-disk"></span>
                    <spring:message code="Save"/>
                </button>
            </div>
            <c:if test="${not empty jobId}">
                <div class="col-lg-1">
                    <form action="${spring:mvcUrl('JC#deleteJob').arg(0,jobId).build()}" method="post">
                        <button type="submit" class="btn btn-danger">
                            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                            <span class="glyphicon glyphicon-trash"></span>
                            <spring:message code="Delete"/>
                        </button>
                    </form>
                </div>
            </c:if>
        </div>
    </div>
</div>

</jsp:attribute>
</tags:template>

0 个答案:

没有答案