This article mainly discusses how to quickly convert ordinary data into Json data, and discusses 2 methods in total:
Prime Minister prepares page and entity category:
page:
<body> <div id="topLoginDiv"> Username: <input name="user.name" id="loginName" /> Password: <input name="user.password" id="loginPassword" /> <label> <input type="button" name="loginButton" value="Login" onclick="doLogin();" /> </label> </div> <div id="demo" ></div></body>
Entity Class:
package bean;public class User {private int id;private String userName;private String password;......Omit Get and Set methods}Method 1: Use JSON conversion package to convert JSON data
The first step is to introduce related packages
Step 2: Page submission and callback function processing results.
<script type="text/javascript">function doLogin(){var name = $('#loginName').val();var password = $('#loginPassword').val();var data1 ={'user.userName':name,'user.password':password};$.getJSON('user_login.action',data1,function(data){// Here, $.getJSON must be used to process JSON data if(data.flag){$('#topLoginDiv').html("");$('#demo').html("Current user: "+data.user.userName+" "+data.msg);}else{$('#demo').html(data.msg);}});}</script> Step 3: Struts2 jumps to Action to convert JSON "Key Steps"
private User user=new User();private boolean flag;private String msg;......Omit Get and Set methods public String login() throws IOException{if(user.getUserName().equals("admin")&&user.getPassword().equals("123456")){msg="Login successfully";flag=true;}else{msg="Login failed, username or password is incorrect!";flag=false;}Map<String,Object> list = new HashMap<String,Object>();//The Map here does not use get and Set methods list.put("flag", flag); list.put("msg",msg); if(flag){list.put("user",user);}ServletActionContext.getResponse().setCharacterEncoding("UTF-8");ServletActionContext.getResponse().getWriter().print(JSONObject.fromObject(list)); return null;//The return value here is NULL, and there is no need to return to the ACTION configuration for processing}Method 2: Use Struts2 to configure Action for JSON data conversion
Step 1: Introduce the package
This method only requires introducing the following package based on the required packages for Struts2:
Step 2: Page submission and callback function processing results. Refer to the second step in Method 1.
Step 3: Configure Action
<package name="json_default" namespace="/" extends="json-default">//Note the extends configuration here<action name="user_*" method="{1}"> <result type="json">//Specify the type here<!-- Parameter root specifies the root object to be serialized--> <!-- The values of all getter methods with return values in the current Action will be serialized by default--> <param name="root">list</param> <!-- Parameter includeProperties specifies which properties in the root object to be serialized, and multiple properties are separated by commas--> <param name="includeProperties">msg,flag,user,user.userName</param> <!-- Parameter excludeProperties specifies the properties to be excluded from the root object. Excluded properties will not be serialized --> <param name="excludeProperties">user.password</param> <!-- Parameter excludeNullProperties specifies whether to serialize the properties with empty value --> <param name="excludeNullProperties">true</param> </result> </action></package> Step 4: Struts2 jumps to Action to convert JSON "Key Steps"
private User user=new User();private boolean flag;private String msg;private Map<String,Object> list=null;//You need to prepare get and Set methods for MAP............Omit Get and Set methods public String login() throws IOException{if(user.getUserName().equals("admin")&&user.getPassword().equals("123456")){msg="Login successfully";flag=true;}else{msg="Login failed, username or password is incorrect!";flag=false;}list= new HashMap<String,Object>();list.put("flag", flag);list.put("msg",msg);if(flag){list.put("user",user);}return "success";//The return value is success to ensure that you can jump into the Action configuration file for data conversionThe above conversion and delivery method of Json in Struts is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.