SPRINGMVC Series (iii) jquery and JSON mode parameter passing and handling javaweb Chinese garbled problem

Source: Internet
Author: User

In this paper, we mainly talk about the two common methods of transmitting parameters between view and controller in SPRINGMVC:

1. JQuery

2. JSON

First, the way of jquery

Idea: When you click a button, the jquery event is triggered, and the entire form is submitted

In JSP

<script type= "Text/javascript" >function AddUser () {var form=document.form[0];form.action= "/springMVC/user/ Data/adduser "; form.method=" get "; Form.submit ();} </script><body>

Controller in

the parameters in the JSP and controller are consistent  

@Controller @requestmapping ("/user/data") public class Datacontroller {//one: Get Parameters directly @requestmapping ("/adduser") public String AddUser (String username,string age,httpservletrequest request) {Request.setattribute ("username", username); Request.setattribute ("Age", age); return "/usermanager";} Another: Gets the parameter @requestmapping ("/adduser") public String addUser (user user,httpservletrequest request) through the user entity { Request.setattribute ("username", user.getusername); Request.setattribute ("Age", user.getage); return "/usermanager" ;}}

How to solve the Java WB Chinese garbled?

1, the JSP defines the page encoding method: UTF-8

<%@ page language= "java" pageencoding= "UTF-8"%>

2. Set the server (Tomcat here) uriencoding= "UTF-8"

... \tomcat\apache-tomcat-6.0.35\conf\server.xml

    <connector uriencoding= "UTF-8"  port= "8080" protocol= "http/1.1"                connectiontimeout= "                20000" redirectport= "8443"/>

This can only solve the get commit garbled problem, but not solve the post submission! Mainly because of the different way of transmitting parameters

To troubleshoot post garbled issues, see the following scenarios:

3. Filter with filter in spring configuration file

Set Forceencoding to force encoding true at initialization time

Url-pattern: Set *.jsp to filter all JSP pages, set to */To filter all requests

<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>/*.jsp</url-pattern>  


Two, JSON mode

Idea: Get the value from the control's ID and then convert it to JSON format for delivery

Problems here: Post submission normal, get commit garbled

Solution: First jquery will get the value of encodeURI respectively encoded, after the controller in the removal of the time with Urldecoder.decode respectively to decode.

In JSP

coded separately using the encodeURI

<script type= "Text/javascript" >$ (document). Ready (function () {$ ("Add"). Click (function () {var username= encodeURI ($ ("#username"). attr ("value"), Var Age=encodeuri ($ ("#age"). attr ("value"), Var user={username:username, Age:age};$.ajax ({url: "/springmvc/user/data/adduser", type: "Get", data:user,success:function (data) {alert (" Username-> "+data.username+" age-> "+data.age);})})}) </script><body>

Controller in

decode with Urldecoder.decode (String)

<p> @Controller @requestmapping ("/user/data") public class Datacontroller {</p><p> // Get Parameters </p><p>  @RequestMapping ("/adduserjson")  public void AddUser (user user) through the user entity, HttpServletRequest request,httpservletresponse response) {  </p><p>//decodes the parameter </p><p >string Username=urldecoder.decode (User.getusername, "UTF-8");   string Age=urldecoder.decode ( User.getage, "UTF-8");     //stitching the acquired parameters into JSON format, and then passing to the foreground display   //{"UserName": "UserName", " Age ":" Age "}  string result=" {\ "username\": \ " "+user.getusername () +" \ ", \" age\ ":" +user.getage () + "\"} ";   printwriter out=null;   Response.setcontenttype ("Application/json")           try {       out=response.getwriter ();      out.write (result);   } catch ( IOException e) {   e.printstacktrace ();  }</p><p> }}</p> 


Summarize:

Springmvc The way the parameters are passed between the page and the controller, this article only describes the two most commonly used methods (Jquery+json), focusing on: JAVA Web Chinese garbled problem.

The first thing to do is to set the encoding method in the JSP to UTF-8

In jquery mode, if the Get commit method garbled, you can change the server encoding mode, if the post is garbled, can be configured filter filtering

JSON mode, the general use of post-submission method, Normal, if it is submitted by get, it is important to encode and decode the passed parameters separately.

SPRINGMVC Series (iii) jquery and JSON mode parameter passing and handling javaweb Chinese garbled problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.