In the spirit of a function of a functional learning mentality, so write it! And the implementation of the servlet, and then write back, so that you can compare learning.
The first step is to build the database:
The code will not be written, directly post the field:
The second step: Establish the Database Information class: Mysqldbinfor class (for storing database connection information, later easy to modify) and connecting the Connectdb class:
1.MySqlDBInfor class:
Database configuration information
public class Mysqldbinfor {
public static String drivername = "Com.mysql.jdbc.Driver";
public static String URL = "Jdbc:mysql://localhost:3306/pethome";
public static String user = "root";
public static String pwd = "lisike13141.";
}
2.ConnectDB class:
public class Connectdb {
static Connection Connection = Null;//1.
Database connection
public static Connection Getconnectdb () {
try {
Class.forName (mysqldbinfor.drivername);//2.
Connection = Drivermanager.getconnection (mysqldbinfor.url,mysqldbinfor.user,mysqldbinfor.pwd);//3.
return connection;
} catch (Exception e) {
System.out.println (E.getmessage ());
}
return connection;
}
Database connection shutdown
public void Closedb () {
try {
Getconnectdb (). Close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
The third step is to create the user (Bean) class:
public class User {
private int id;
Private String uname;
Private String pwd;
Private String phone;
Private String Email;
The set and get methods are slightly
}
The fourth step is to establish the Userdao (DAO) class:
public class Userdao {
Connection Connection = Connectdb.getconnectdb ();
Query All Users
Public list<user> selectallusers () {
String sql = "Select*from user;";
list<user> list = new arraylist<user> ();
try {
PreparedStatement statement = connection.preparestatement (SQL);
ResultSet set = Statement.executequery ();
while (Set.next ()) {
User user = new user ();
int id = set.getint ("id");
String uname = set.getstring ("uname");
String pwd = set.getstring ("pwd");
User.setid (ID);
User.setuname (uname);
User.setpwd (PWD);
User.setphone (set.getstring ("Phone"));
User.setemail (set.getstring ("email"));
List.add (user);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return list;
}
}
Fifth step: Implement the Useraction class:
public class Useraction extends actionsupport{
/**
*
*/
Private static final long serialversionuid = 1L;
1. Convert the resulting data into JSON
Userdao dao = new Userdao ();
@Override
Public String Execute () throws Exception {
TODO auto-generated Method Stub
JSON ();
This sentence is important ******* tell struts to finish the action and no longer perform the next steps ************************************
Actioncontext.getcontext (). Getactioninvocation (). GetProxy (). Setexecuteresult (false);
return null;
}
2, return the results to the page
public void json () throws exception{
Get Response Object
HttpServletResponse response = Servletactioncontext.getresponse ();
Setting the response header
Response.setcontenttype ("Type=text/json;charset=utf-8");
PrintWriter writer = Response.getwriter ();
Convert list<user> to JSON data and write Web pages
Jsonarray array = Jsonarray.fromarray (Dao.selectallusers (). ToArray ());
Writer.write ("+array");
}
}
Sixth step: Struts.xml configuration (web. XML self-configuring):
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apache software foundation//dtd Struts Configuration 2.0//en"
"Http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<package name= "Webface" extends= "Struts-default" >
<!--query database pethome, query users, and then upload to JSP--
<action name= "JSON" class= "action. Useraction "Method=" Execute >
<!--These two sentences are not used here for the time being.
<result name= "JSON" >/index.jsp</result>
<result name= "JSON" >/test.html</result>
</action>
</package>
</struts>
Seventh Step: Easyui Front-end code (note URL):
<%@ 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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<link rel= "stylesheet" type= "Text/css" href= "./easyui/themes/default/easyui.css" >
<link rel= "stylesheet" type= "Text/css" href= "./easyui/themes/icon.css" >
<link rel= "stylesheet" type= "Text/css" href= "./easyui/demo/demo.css" >
<script type= "Text/javascript" src= "./easyui/jquery.min.js" ></script>
<script type= "Text/javascript" src= "./easyui/jquery.easyui.min.js" ></script>
<script type= "Text/javascript" src= "./easyui/locale/easyui-lang-zh_cn.js" ></script>
<body>
<table id= "DG" title= "My Users" class= "Easyui-datagrid" style= "width:550px;height:250px"
Url= "JSON"
Toolbar= "#toolbar"
Rownumbers= "true"
Fitcolumns= "true"
Singleselect= "true" >
<thead>
<tr>
<th field= "uname" width= ">UName</th>"
<th field= "pwd" width= ">Pwd</th>"
<th field= "Phone" width= ">Phone</th>"
<th field= "Email" width= ">Email</th>"
</tr>
</thead>
</table>
<div id= "Toolbar" >
<a href= "#" class= "Easyui-linkbutton" iconcls= "Icon-add" plain= "true" onclick= "NewUser ()" >new User</a>
<a href= "#" class= "Easyui-linkbutton" iconcls= "Icon-edit" plain= "true" onclick= "Edituser ()" >edit User</a >
<a href= "#" class= "Easyui-linkbutton" iconcls= "Icon-remove" plain= "true" onclick= "Destroyuser ()" >Remove User </a>
</div>
</body>
Summary: Data from the background to the front-end code, mainly action there, for the front-end, just need to write the field and JSON data correctly on the line. That is, the background only need to transform the data into JSON data, and to the front-end, and then the front end can go through field to find the corresponding field value.
Struts+jquery Easyui Implementation Query