[Error Resolution] SpringMVC receiving object Chinese garbage problem solving

wrong description:

The following sample code for a simple piece of front end:

1 <!-- 请求对象实体类 -->
2       <form action="SpringMVCHandler/testObjectProperties" method="post">
3           学号:<input type="text" name="id"><br>
4           姓名:<input type="text" name="name"><br>
5           家庭住址:<input type="text" name="address.homeAddress"><br>
6           School Address: < INPUT type = "text" name = "address.schoolAddress" > < br > 
. 7            < INPUT type = "Submit" value = "entity class parameter passing (easy)" > 
. 8        </ form >

Background and receive code:

. 1      @RequestMapping (value = "testObjectProperties" )
 2      public String testCookieValue (Student Student) { // Student form the form must be consistent with the properties (attributes support cascading) 
. 3          System.out.println (student.getId () + " "student.getName + () +" "+ student.getAddress () getHomeAddress () +" "+. student.getAddress () getSchoolAddress ());.
 . 4          return " Success " ;
 . 5      }

And two entity classes and Student Address

Student.java

 1 package org.zlc.entity;
 2 
 3 public class Student {
 4     private int id;
 5     private String name;
 6     private Address address;
 7     public int getId() {
 8         return id;
 9     }
10     public void setId(int id) {
11         this.id = id;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public Address getAddress() {
20         return address;
21     }
22     public void setAddress(Address address) {
23         this.address = address;
24     }
25 }
View Code

Address.java

 1 package org.zlc.entity;
 2 
 3 public class Address {
 4     private String homeAddress;
 5     private String SchoolAddress;
 6     public String getHomeAddress() {
 7         return homeAddress;
 8     }
 9     public void setHomeAddress(String homeAddress) {
10         this.homeAddress = homeAddress;
11     }
12     public String getSchoolAddress() {
13         return SchoolAddress;
14     }
15     public void setSchoolAddress(String schoolAddress) {
16         SchoolAddress = schoolAddress;
17     }
18     
19 }
View Code

After the front-end input console output typing

 

 

But if Chinese will be garbled output

 

 Solution:

Add the character set interceptors in web.xml

1      <! - Character Set Filter -> 
2      < filter > 
. 3          < Description > character set filter </ Description > 
. 4          < filter-name > characterEncodingFilter </ filter-name > 
. 5          < filter-class > org.springframework .web.filter.CharacterEncodingFilter </ filter-class > 
. 6          < the init-param > 
. 7              < Description > character set encoding </ Description > 
. 8              <param-name>encoding</param-name>
 9             <param-value>UTF-8</param-value>
10         </init-param>
11         <init-param>
12             <param-name>forceEncoding</param-name>
13             <param-value>true</param-value>
14         </init-param>
15     </filter>
16 
17     <!- Character Set intercept-->
18     <filter-mapping>
19         <filter-name>characterEncodingFilter</filter-name>
20         <url-pattern>/*</url-pattern>
21     </filter-mapping>

Chinese no longer garbled

 

Guess you like

Origin www.cnblogs.com/zlc364624/p/12603166.html
Recommended