UTF-8表单数据未正确提交

时间:2017-08-02 09:03:33

标签: jsp spring-mvc spring-security

我的问题是有时会正确提交UTF-8数据,但有时字符串会被破坏。

正确提交时,它看起来像这样:আনোয়ার/好/我是一个好男孩,但在提交不正确的时候,它看起来像:à|à|¨§§à§à||||||

UTF-8的数据库配置:

在MySql中:Charset:utf-8整理:utf8_general_ci

我的application.properties文件:

jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url =jdbc:mysql://localhost:3306/icdb?useUnicode=true&characterEncoding=UTF-8&connectionCollation=utf8_general_ci&characterSetResults=utf8 jdbc.username = root
jdbc.password =password
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = true

这是我的SpringMvcInitializer类:

public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 @Override
public void onStartup(ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);
    FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding-filter", new CharacterEncodingFilter());
    encodingFilter.setInitParameter("encoding", "UTF-8");
    encodingFilter.setInitParameter("forceEncoding", "true");
    encodingFilter.addMappingForUrlPatterns(null, false, "/*");
}

在控制器中:

@RequestMapping(value = "/saveNewOrder", produces = {"application/json; charset=UTF-8"}, method = RequestMethod.POST)
public String saveNewOrder(HttpServletRequest request, @Valid CatMenuSelection catMenuSelection, CatMenuSelectionExtra catMenuSelectionExtra, Model m, BindingResult result)

JSP文件如下所示:

   <%@page contentType="text/html" pageEncoding="UTF-8"%>
<meta charset="utf-8">  <meta http-equiv="X-UA-Compatible" content="IE=edge">  
 <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">


<form:form action="saveNewOrder" method="post" modelAttribute="catMenuSelection" acceptCharset="UTF-8">
                            <table class="table">
                                <form:hidden name="iddd" path="id"/>
                                <div class="col-lg-12">
                                    <div class="form-group row">
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.people" /></label></div>
                                        <div class="col-lg-3">
                                            <form:input path="totalGuest" id="popleId"  class="col-lg-2 form-control "  style="width: 76px; height: 32px" required="true"/>
                                            <!--                                        <div class="col-lg-1">
                                            <%--<form:errors path="totalGuest" cssclass="error"></form:errors>--%>
                                        </div>-->
                                            <div class="col-lg-1">

                                               <select id="typeId" style="width: 76px; height: 25px" name="distributionType" required="true">
                                                <option value="V.I.P"><fmt:message key="catering.lalel.vip" /></option>
                                                <option value="Packet"><fmt:message key="catering.lalel.packet" /></option>
                                                <option value="Public"><fmt:message key="catering.lalel.public" /></option>
                                            </select>
                                            </div>
                                        </div>
                                            <!--                                            <div class="col-lg-1">
                                            <%--<form:errors path="distributionType" cssclass="error"></form:errors>--%>
                                        </div>-->
                                        <!--</div>-->
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.date" /></label></div>
                                        <div class="col-lg-3">
                                            <form:input path="occassionDate"  class="datepicker form-control" style="width: 170px" required="true"/>
                                        </div>
                                        <!--                                        <div class="col-lg-1">
                                        <%--<form:errors path="occassionDate" cssclass="error"></form:errors>--%>
                                    </div>-->
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.time" /></label></div>
                                        <div class="col-lg-3">
                                            <select name="occassionTime" style="width: 170px; height: 32px" required="true">
                                                <option value="Morning"><fmt:message key="catering.lalel.morning" /></option>
                                                <option value="Noon"><fmt:message key="catering.lalel.noon" /></option>
                                                <option value="Evening"><fmt:message key="catering.lalel.evening" /></option>
                                                <option value="Night"><fmt:message key="catering.lalel.night" /></option>
                                            </select>
                                        </div>
                                        <!--                                        <div class="col-lg-1">
                                        <%--<form:errors path="occassionTime" cssclass="error"></form:errors>--%>
                                    </div>-->
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.event" /></label></div>
                                        <div class="col-lg-3"><form:input path="occassion" class="form-control" style="width: 170px" required="true"/></div>

                                        <!--                                    <div class="col-lg-1"> 
                                        <%--<form:errors path="occassion" cssclass="error"></form:errors>--%>
                                    </div>-->
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.name" /></label></div>
                                        <div class="col-lg-3"><form:input path="customerName" class="form-control" style="width: 170px" required="true" /></div>
                                        <!--cusname-->
                                        <!--<div class="col-lg-1">
                                        <%--<form:errors path="customerName" cssclass="error"></form:errors>--%>
                                        </div>-->
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.place" /></label></div>
                                        <div class="col-lg-3"><form:input path="occassionPlace" class="form-control" style="width: 170px" required="true"/></div>
                                        <!--                                    <div class="col-lg-1"> 
                                        <%--<form:errors path="occassionPlace" cssclass="error"></form:errors>--%>
                                    </div>-->


                                    </div>
                                    <div class="form-group row">
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.vipprice" /></label></div>
                                        <div class="col-lg-3"><form:input path="guestPrice" id="vipPriceId" class="form-control " style="width: 170px" required="true"/></div>

                                        <!--                                    <div class="col-lg-1"> 
                                        <%--<form:errors path="guestPrice" cssclass="error"></form:errors>--%>
                                    </div>-->
                                        <div class="col-lg-1"><label ><fmt:message key="catering.lalel.packetprice" /></label></div>
                                        <div class="col-lg-3"><form:input path="driverPrice" id="packetPriceId" class="form-control" style="width: 170px" placeholder="Packet Price" required="true"/></div>

                                        <!--                                    <div class="col-lg-1"> 
                                        <%--<form:errors path="driverPrice" cssclass="error"></form:errors>--%>
                                    </div>-->
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.driver" /></label></div>
                                        <div class="col-lg-3"><form:input path="totalDriver" id="driverId" class="form-control " style="width: 170px" required="true"/></div>

                                        <!--                                    <div class="col-lg-1">
                                        <%--<form:errors path="totalDriver" cssclass="error"></form:errors>--%>
                                    </div>-->
                                    </div>
                                    <div class="form-group row">
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.grosstotal" /></label></div>
                                        <div class="col-lg-3"><form:input path="grossTotalAmnt" id="grossTotalId" class="form-control " style="width: 170px" required="true"/></div>
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.phone" /></label></div>
                                        <div class="col-lg-3"><form:input path="phoneNo" class="form-control" style="width: 170px" required="true" /></div>

                                        <!--                                    <div class="col-lg-1"> 
                                        <%--<form:errors path="phoneNo" cssclass="error"></form:errors>--%>
                                    </div>-->
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.address" /></label></div>
                                        <div class="col-lg-3"><form:input path="address" class="form-control" style="width: 170px" required="true"/></div>

                                        <!--                                    <div class="col-lg-1">
                                        <%--<form:errors path="address" cssclass="error"></form:errors>--%>
                                    </div>-->

                                    </div>

                                    <div class="form-group row">

                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.discount" /></label></div>
                                        <div class="col-lg-3"><form:input path="discount" class="form-control" style="width: 170px" /></div>
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.advancedpay" /></label></div>
                                        <div class="col-lg-3"><form:input path="advanceDeposit" class="form-control" style="width: 170px"/></div>
                                        <!--                                    <div class="col-lg-1">
                                        <%--<form:errors path="advanceDeposit" cssclass="error"></form:errors>--%>
                                    </div>-->
                                        <div class="col-lg-1"><label><fmt:message key="catering.lalel.comment" /></label></div>
                                        <div class="col-lg-3"><form:input path="mainRemarks" class="form-control" style="width: 170px"/></div>
                                        <form:errors path="mainRemarks" cssclass="error"></form:errors>
                                        </div>
                                    </div>

请帮助我找出总是无法插入utf-8数据的错误并获得解决方案。

2 个答案:

答案 0 :(得分:1)

我使用了以下代码,但它确实有效。

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{ 
@Override
protected void configure(HttpSecurity http) throws Exception {

    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    http.addFilterBefore(filter, CsrfFilter.class);}

答案 1 :(得分:0)

因此,您的问题与数据库配置无关。但实际上是弹簧配置问题。

然后,尝试在<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />标记中添加此<head>。并尝试将您的FilterRegistration更改为:

CharacterEncodingFilter ceFilter = new CharacterEncodingFilter();
ceFilter.setEncoding("UTF-8"); ceFilter.setForceEncoding(true);
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding-filter", ceFilter);

如下面的评论所述。并且还要确保CharacterEncodingFilter是过滤器链中的第一个过滤器。