1. Speaking of this JSON plug-in implementation of the asynchronous refresh of the function of my two days, I would like to use juery to achieve, but in the JSP page is always unable to get JSON data, ultimately helpless can only choose the most primitive way.
2. The first is the need to load struts2-json-plugin-2.3.20.1.jar (because I used the Struts version is 2.3.20.1) to the project, next is the configuration struts.xml configuration file, and in this process I have encountered a very annoying thing is that I configured JSO N, the need to inherit Json-default, always error, this problem has been in my previous blog "SSH using JSON plug-in implementation of AJAX problems," there is a specific elaboration, so here I will not be described.
3. First, define a Myjson.java action class with the following code:
public class Myjson extends baseaction{
Public String Getrno () {
return Rno;
}
public void Setrno (String rno) {
Rno = Rno;
}
Private String Rno;
Public roombiz getroombiz () {
return roombiz;
}
public void Setroombiz (Roombiz roombiz) {
This.roombiz = roombiz;
}
Roombiz roombiz;
Public list<roominfo> getroomlist () {
return roomlist;
}
public void Setroomlist (list<roominfo> roomlist) {
This.roomlist = roomlist;
}
Private list<roominfo> roomlist;
Public String Getroombyrnobyjson () throws Exception {
Roomlist=new arraylist<roominfo> ();
System.out.println ("rno==================" +rno);
Roombiz=servicemanager.getroombiz ();
Roominfo Roominfo=roombiz.getroombyrno (Rno);
Roomlist.add (Roominfo);
System.out.println ("roomlist=============" + roomlist.get (0). Getrtel ());
Return "Success";
}
}
4. The configuration code specifically written in the Struts.xml configuration file is:
<package name= "Myjson" namespace= "/myjson" extends= "Json-default" >
<action name= "Szqjson" class= "Com.hibtest3.action.MyJson" method= "Getroombyrnobyjson" >
<result type= "json"/>
</action>
</package>
After the 5.struts.xml is configured, the next step is the call on the page, which is when the drop-down menu of the room number is changed to trigger the event, returning a JSON data format.
<tr>
<td> Room Number </td>
<td><select class= "Selectstyle" name= "Rno" id= "Rno"
Onchange= "Jsonclick (this.options[this.options.selectedindex].value)" >
<%for (int i=0;i<roomlist.size (); i++) {
Roominfo Roominfo=roomlist.get (i);
%>
<option value= "<%=roominfo.getrno ()%>" ><%=roominfo.getrno ()%></option>
<%}%>
</select></td>
</tr>
function Jsonclick (obj) {
var url = "/hibtest3/myjson/szqjson?" rno= "+ obj;
if (window. XMLHttpRequest) {
IE7, Firefox, opera support
req = new XMLHttpRequest ();
}else if (window. ActiveXObject) {
IE5,IE6 Support
req = new ActiveXObject ("Microsoft.XMLHTTP");
}
Req.open ("POST", url, True);
The onReadyStateChange property has a function to handle the server response, with 5 values representing different states
Req.onreadystatechange = callback;
The Send function sends the request
Req.send (NULL);
}
function callback () {
if (req.readystate = = 4 && req.status = = 200) {
var check = Req.responsetext;
Show (check);
}
}
function Show (str) {
var rstyle = document.getElementById (' Rstyle ');
var rprice = document.getElementById (' Rprice ');
var lens=str.length;
var str1=str.substring (2,lens-5);
var str2=str1.split (', ');
for (Var i=0;i<str2.length;i++) {
var obj=str2[i].split (': ');
if (obj[0]== ' "Rstyle" ') {
RSTYLE.VALUE=OBJ[1];
}else if (obj[0]== ' "Rprice" ') {
RPRICE.VALUE=OBJ[1];
}
}
}
6. The result of the final operation is as follows, when the room number is changed, the corresponding room type and room price will also change accordingly.
STRUTS2 using JSON plug-ins for asynchronous refresh functions