This article mainly discusses how to quickly convert ordinary data to JSON data, and discusses 2 methods altogether:
Prime Minister prepares page and entity classes:
Page:
<body>
<div id= "Toplogindiv" >
username:
<input name= "User.Name" id= "LoginName"/>
Password:
<input name= "User.password" id= "Loginpassword"/> <label class=
"Ui-green" >
<input type= "button" Name= "Loginbutton" value= "Login" onclick= "Dologin ();"/>
</label>
</div>
<div id= "Demo" ></div>
</body>
Entity classes:
Package bean;
public class User {
private int id;
Private String userName;
private String password;
...... Omit get and Set methods
}
Method One: Convert JSON data using JSON conversion package
The first step is to introduce related packages
Step Two: 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 ':p assword};
$.getjson (' User_login.action ', data1,function (data) {//Here must use $.getjson 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 three: Struts2 jump to the action for JSON conversion "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= "landed successfully";
flag=true;
} else{
msg= "Login failed, username or password error!" ";
Flag=false;
}
map<string,object> list = new hashmap<string,object> ()//The map here does not use the Get and set method
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 returned value here is null and does not need to be returned to the action configuration for processing
}
Method Two: Use STRUTS2 to configure action for JSON data conversion
The first step: the introduction of the package
This method only needs to introduce the following package on the basis of the package required to use STRUTS2:
Step Two: page submission and callback function processing results. The second step in the reference method one.
Step Three: Configure the action
<package name= "Json_default" namespace= "/" extends= "Json-default" >//Note extends configuration <action name=
"user _* "class=" action.useraction "method=" {1} ">
<result type=" JSON ">//this indicates that the type
<!--parameter root specifies the order The serialized root object-->
<!--The default will serialize the value of all getter methods that have return values in the current action-->
<param name= "root" >list</pa Ram>
<!--parameter includeproperties specifies which attributes in the root object are to be serialized, with multiple attributes separated by commas-->
<param name= "Includeproperti Es >msg,flag,user,user.userName</param>
<!--parameter excludeproperties specifies the properties to be excluded from the root object, excluding the attributes that will not be serialized-->< c9/> <param name= "excludeproperties" >user.password</param>
<!--parameter excludenullproperties Specifies whether to serialize a property with a null value-->
<param name= "excludenullproperties" >true</param>
</result>
</action>
</package>
Step Fourth: Struts2 jump to the action for JSON conversion "key steps"
Private user User=new user ();
Private Boolean flag;
Private String msg;
Private map<string,object> list=null;//need to prepare a get and set method for the Map ... .....
Omit get and Set methods public
String login () throws ioexception{
if (User.getusername (). Equals ("admin") && User.getpassword (). Equals ("123456")) {
msg= "landed successfully";
flag=true;
} else{
msg= "Login failed, username or password error!" ";
Flag=false;
}
list= new Hashmap<string,object> ();
List.put ("flag", flag);
List.put ("msg", msg);
if (flag) {
list.put ("user", user);
}
Return "Success";//returns value is success ensures that the action profile can be skipped for data conversion
The above JSON in struts conversion and transfer method is small to share all the content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.