Struts2 is a difficult and important point for beginners to operate databases. Now I will explain how to operate databases in struts2 and use struts2 to add, delete, modify, query, and query databases by page, see the following code:
User class
Public class UserAction extends ActionSupport {
List <User> userList;
UserDao userDao = new UserDao ();
User user;
User2 user2;
Public User2 getUser2 (){
Return user2;
}
Public void setUser2 (User2 user2 ){
This. user2 = user2;
}
Public User getUser (){
Return user;
}
Public void setUser (User user ){
This. user = user;
}
Public int getFirstRecord (){
Return firstRecord;
}
Public void setFirstRecord (int firstRecord ){
This. firstRecord = firstRecord;
}
Private int currentPage = 1;
Private int Records;
Private int pageSize = 5;
Private int pages;
Int firstRecord;
Public List <User> getUserList (){
Return userList;
}
Public void setUserList (List <User> userList ){
This. userList = userList;
}
Public UserDao getUserDao (){
Return userDao;
}
Public void setUserDao (UserDao userDao ){
This. userDao = userDao;
}
Public int getCurrentPage (){
Return currentPage;
}
Public void setCurrentPage (int currentPage ){
This. currentPage = currentPage;
}
Public int getRecords (){
Return Records;
}
Public void setRecords (int records ){
Records = records;
}
Public int getPageSize (){
Return pageSize;
}
Public void setPageSize (int pageSize ){
This. pageSize = pageSize;
}
Public int getPages (){
Return pages;
}
Public void setPages (int pages ){
This. pages = pages;
}
Public void pages (){
Records = userDao. getAllCount ();
Pages = Records/pageSize + (Records % pageSize = 0 )? 0: 1 );
FirstRecord = (currentPage-1) * pageSize;
}
Public String listAll () throws Exception {
This. pages ();
UserList = userDao. doList (firstRecord, pageSize );
Return SUCCESS;
}
Public String addUser (){
UserDao. addUser (user );
Return SUCCESS;
}
Public String deleteUser (){
UserDao. deleteUser (user );
Return SUCCESS;
}
Public String updateUser (){
UserDao. updateUser (user );
Return SUCCESS;
}
Public String getInfo (){
User = userDao. getInfo (user. uid );
Return SUCCESS;
}
You can write the database connection by yourself, and then configure the action to run it. If you need the source code, download it to http://blog.csdn.net/u014626784resource.