This part is more complicated, had seen their classmates before the development of an elective system when the use of JSON, but always do not know what the use of writing things are useless, so did not go to learn him. And now with this with curiosity, this is what to do with, this is how to use, this is how to combine the mentality to learn, the effect is very good.
This time using the Easyui data grid, the DataGrid. You need to refer to the JSON data from a URL and then show it neatly and beautifully on the page. Think of what you have done before, directly with the database data and HTML table code for stitching, neat and tidy, but the code is particularly awkward to write. Let me stand on the idea of a designer, if I now provide a table template, and then I want to read your data in order to arrange it, then we need to define a common format, I can read any data that follows this format and show it in Datagird, This is the function of Easyui, and the function of the format is who realizes it--json debut.
The Json,javascript object NOTATION,JS is a markup (representation) method that resembles XML but is smaller and faster than XML. XML extracts elements that use the DOM operation, need child these things.
JSON can pass JS parsing and Ajax transmission, right, that's it.
Talking about Ajax by the way also looked at a bit, used to forget. Ajax is the function of the page is not refreshed and secretly connected to the server to get the data back to the front.
I show this form here, in fact, I want the data is secretly used Ajax to get the data, and through the JS parsing and then put back to the table accurately.
(i) JSON
Syntax rules:
Divide name and value pairs, data delimited: {} save Object [] Save array "A": 1 corresponds to JS in a = 1.
JSON Data Examples:
[{"id": 1, "name": "ee", "Password": "1"},
{"id": 2, "name": "DF2", "Password": "123"},
{"id": 3, "name": "45ty", "Password": "123"},
{"id": 4, "name": "Sdfy", "Password": "123"},
{"id": Ten, "Name": "Sdfy", "Password": "123"}]
There are 4 elements in the array, and an element is an object: There are id,name and password.
(ii) The DataGrid in Easyui gets the JSON data.
Datagrid:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Url= "List_ej"
The point is, Url,url wrote it. If you return the URL of the JSON-formatted data, we use the action to jump to the response JSP page.
List_ej the following action to get the data, then jump to list_ej.jsp.
Action inside to get the database data and JSON data conversion, all on the Web is copied paste with the JSON framework, the code of those and write the mess up eight bad, his groping for a long time.
Jsonarray can convert the data in the list into JSON format.
Public String List_ej () {Actioncontext ctx=actioncontext.getcontext (); Connection C = Connecttosql.getconn (); Statement st = CONNECTTOSQL.GETST (c); list<user> list = new arraylist<user> (); String result= "{}"; try {ResultSet rs = st.executequery ("SELECT * from User"), while (Rs.next ()) {User U = new user (); U.setid ( Rs.getint ("userid")), U.setname (rs.getstring ("username")), U.setpassword (rs.getstring ("password")); List.add (U);}} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} list<user> o = jsonarray.fromobject (list); result=o.tostring (); try {//servletactioncontext.getresponse () . Getwriter (). println (Jsonarray.fromobject (list)); Ctx.put ("json", result);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return "Success";}
after we get the data, we use the native JSON class to convert the JSON format and then return the string to another page list_ej.jsp. This page is where the DataGrid finally takes the data.
The problem lies
Here I dug a big hole: I wrote the JSP before.
<% @page import= "com.opensymphony.xwork2.ActionContext"%><%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" ><%@ page Import= "util.*, Org.apache.struts2.ServletActionContext"%> <%@ taglib prefix= "s" uri= "/struts-tags"%> <% @ Page import= "java.sql.*,java.util.*,net.sf.json.*"%><%! Connection c = null; Statement st = null; ResultSet rs = null; String s = "";%><%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%>
There is no problem with such logic, that is, it has not been shown. It has been tuned for a long time.
The problem is ———— I'm writing too much stuff.
list_jsp only need:
<%string json= (String) request.getattribute ("JSON");%><%=json%>
Finally, Datagird successfully fetch the database data.
Using Struts2 and jquery Easyui to implement a simple crud system (ii)--JSP,JSON,EASYUI combination