I have already written in my encoding UTF-8 everywhere: in jsp:

<% response.setCharacterEncoding("UTF-8"); %> <% response.setContentType("text/html; charset=UTF-8");%> <% request.setCharacterEncoding("UTF-8"); %> 

in the controller:

 @RequestMapping(method = RequestMethod.GET, produces = { "text/html;charset=UTF-8" }) @RequestMapping(method = RequestMethod.POST, produces = { "text/html;charset=UTF-8" }) 

in bins:

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value =".jsp" /> <property name="contentType" value="text/html; charset=UTF-8"/> </bean> 

in configuration:

 InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setContentType("text/html;charset=UTF-8"); 

in web.xml:

 <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-group> </jsp-config> 

but still the request goes in the wrong encoding and, after checking, gives "СѲ ²" instead of "fv"

  • In chrome, click the settings icon - additional tools - encoding - unicode (ff8), has the page become normal? - MrFylypenko
  • in chrome, the encoding is set to unicode UTF-8 by default - Alexander
  • Let's take another way, do you have a parameter in html: <head><meta charset="UTF-8"></head> ? and add the minimum jsp code of the page where the encoding is not displayed correctly so that it can be reproduced. - MrFylypenko
  • yes of course it costs <head> <meta charset = "UTF-8"> </ head> - Alexander
  • one
    Tried to add a filter for conversion to yutf-8, as here ? - MrFylypenko

3 answers 3

Here is the form code - after checking the correctness of filling in the form, the value in the form changes the encoding - the controller receives data in a wrong encoding: messages about incorrect data are generated in Russian - the problem in the field is similar: <form:input type="text" path="firstName" id="firstName" class="form-control input-sm"/>

 <form:form method="POST" modelAttribute="student" class="form-horizontal"> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="firstName">Имя</label> <div class="col-md-7"> <form:input type="text" path="firstName" id="firstName" class="form-control input-sm"/> <div class="has-error"> <form:errors path="firstName" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="lastName">Last Name</label> <div class="col-md-7"> <form:input type="text" path="lastName" id="lastName" class="form-control input-sm"/> <div class="has-error"> <form:errors path="lastName" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="sex">Sex</label> <div class="col-md-7" class="form-control input-sm"> <form:radiobutton path="sex" value="M" />Male <form:radiobutton path="sex" value="F" />Female <div class="has-error"> <form:errors path="sex" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="dob">Date of birth</label> <div class="col-md-7"> <form:input type="text" path="dob" id="dob" class="form-control input-sm"/> <div class="has-error"> <form:errors path="dob" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="email">Email</label> <div class="col-md-7"> <form:input type="text" path="email" id="email" class="form-control input-sm"/> <div class="has-error"> <form:errors path="email" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="section">Section</label> <div class="col-md-7" class="form-control input-sm"> <form:radiobuttons path="section" items="${sections}" /> <div class="has-error"> <form:errors path="section" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="country">Country</label> <div class="col-md-7"> <form:select path="country" id="country" class="form-control input-sm"> <form:option value="">Select Country</form:option> <form:options items="${countries}" /> </form:select> <div class="has-error"> <form:errors path="country" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="firstAttempt">First Attempt ?</label> <div class="col-md-1"> <form:checkbox path="firstAttempt" class="form-control input-sm"/> <div class="has-error"> <form:errors path="firstAttempt" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-group col-md-12"> <label class="col-md-3 control-lable" for="subjects">Subjects</label> <div class="col-md-7"> <form:select path="subjects" items="${subjects}" multiple="true" class="form-control input-sm"/> <div class="has-error"> <form:errors path="subjects" class="help-inline"/> </div> </div> </div> </div> <div class="row"> <div class="form-actions floatRight"> <input type="submit" value="Register" class="btn btn-primary btn-sm"> </div> </div> </form:form> 
  • Add the code of the controller method, where do you get the data and in what place is the encoding wrong? - MrFylypenko
  • Thank you so much, filter helped. But somewhere in the description of Spring Mvc I read that web.xml is not required at all, so I tried to do it all without him! - Alexander
  • It is possible without it, but it is added to the config anyway, here’s the first example where and how it is configured. - MrFylypenko

Add a filter for conversion to UTF-8 in web.xml ( from here ):

 <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

    here are two methods I bring in the student object to the protocol on the 0-, 1- messages the student is empty for another 3 already in the wrong encoding

     @RequestMapping(method = RequestMethod.GET, produces = { "text/html;charset=UTF-8" }) public String newRegistration(ModelMap model) { Student student = new Student(); System.out.print("0-" + student); model.addAttribute("student", student); System.out.print("1-" + student); return "enroll"; } @RequestMapping(method = RequestMethod.POST, produces = { "text/html;charset=UTF-8" }) public String saveRegistration(@Valid Student student, BindingResult result, ModelMap model) { System.out.print("2" + student); if (result.hasErrors()) { return "enroll"; } model.addAttribute("success", "Dear " + student.getFirstName() + " , your Registration completed successfully"); return "success"; }