SSH2 (Struts2 + Spring + Hibernate) Integrated Framework
The SSH2 (Struts2 + Spring3 + Hibernate4) framework used in U8Server, including the configuration files integrated by U8Server and some general components abstracted, this allows developers who develop based on the J2EE framework to quickly start their work.
U8Server is the server end of the U8SDK unified channel SDK access framework. U8Server is developed using mature SSH2 (J2EE framework. Reliable Performance and simple structure.
It is easy to develop a project using the SSH2 framework. It takes some time to integrate and build the framework. The problem mainly lies in the use of configuration methods and frameworks.
In the past, many people asked about the configuration and the entire framework used by U8Server, which often failed to be integrated with the Framework. Here, we open-source the framework and configuration used by U8Server. Projects that require SSH2 R & D can be carried out directly on the basis of this framework, eliminating the hassle of configuration from scratch.
The entire framework is based on the MVC ideology and all configurations are annotated. This avoids the hassle and complexity of configuration file configuration.
Dependent Jar package:
The lib/jars directory contains some jar packages that the SSH2 framework depends on. Add a new jar package. do not cause a jar package conflict.
Framework configuration instructions:
ApplicationContext. xml: Spring container configuration. All Component Management and dependency injection (IOC) are managed by Spring, which is basically fixed and does not need to be modified.
Jdbc. properties: Database Configuration. Modify the database connection information here.
Log4j. properties: log configuration, which is basically fixed and does not need to be modified
Struts. xml: the configuration of struts2. Because we use the annotation configuration method, this file is rarely configured and fixed
WEB-INFweb.xml: This is a fixed configuration of web projects, the need to configure struts2, spring, hibernate and Other filters and listeners. Basic fixed, no need to modify
Source code directory (root directory: com. u8.server): cache/: cache-related directory. In U8Server, the game objects, channel objects, and channel vendor objects are cached. common/: in this directory, U8Server provides a simple encapsulation dao/for the SSH2 framework /: the typical DAO layer (data access layer) data/: data Object filters/: struts2 filter in MVC mode. For example, to perform background management operations, you must log on to the user and have relevant permissions, use filters to intercept log/: log directory service/: Typical MVC mode Service layer (business logic layer) utils/: Common auxiliary tool web/: Typical MVC mode Web layer, here, the struts2 Action framework is used:
For example, you need to develop a user management system,
1. We will abstract a User object. Create a UUser object in the data directory:
/* ** User data object * Conventions: names of all fields, it must be the same as the field name in the database Table */@ Entity // indicates that the object is a data class @ Table (name = uuser) // one-to-one correspondence between the database and a Table in the database. Name is the name of the database table public class UUser {@ Id // indicates that this field is the primary key @ GeneratedValue (strategy = GenerationType. IDENTITY) // The primary key generation method, which is automatically increased here. For more information, see private int id, private int appID, private int channelID, private String name, private String channelUserID, private String channelUserName, private String channelUserNick, and private Date createTime; private Date lastLoginTime; private String token; public JSONObject toJSON () {JSONObject json = new JSONObject (); json. put (id, id); json. put (appID, appID); json. put (channelID, channelID); json. put (name, name); json. put (channelUserID, channelUserID); json. put (channelUserName, channelUserName); json. put (channelUserNick, channelUserNick); return json;} public int getId () {return id;} public void setId (int id) {this. id = id;} public int getAppID () {return appID;} public void setAppID (int appID) {this. appID = appID;} public int getChannelID () {return channelID;} public void setChannelID (int channelID) {this. channelID = channelID;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getChannelUserID () {return channelUserID;} public void setChannelUserID (String channelUserID) {this. channelUserID = channelUserID;} public String getChannelUserName () {return channelUserName;} public void setChannelUserName (String channelUserName) {this. channelUserName = channelUserName;} public String getChannelUserNick () {return channelUserNick;} public void setChannelUserNick (String channelUserNick) {this. channelUserNick = channelUserNick;} public Date getCreateTime () {return createTime;} public void setCreateTime (Date createTime) {this. createTime = createTime;} public Date getLastLoginTime () {return lastLoginTime;} public void setLastLoginTime (Date lastLoginTime) {this. lastLoginTime = lastLoginTime;} public String getToken () {return token;} public void setToken (String token) {this. token = token ;}}
2. With the UUser object, we need to perform some operations on the object. First, we need to obtain the data table corresponding to this object from the database. Therefore, we create a UUserDao class under the dao directory as the Data Metadata class of the UUser object. All Dao Data Handler classes inherit UHibernateTemplate. This abstract class has two generic parameters. The first is the data object to be accessed in the current data category, and the second parameter is the primary key type of the current data object.
Note: All data category classes must be declared using the @ Repository annotation. the name in the brackets is the name that must be used when the object of this class is declared elsewhere.
/*** User Data Handler class */@ Repository (userDao) public class UUserDao extends UHibernateTemplate
{// Data operate}
3. With UUserDao, we also need to create a business operation class of UUserManager as a UUser object in the service directory.
Note: All business operation classes must be declared using the @ Service annotation. the name in the brackets is the name that must be used when the class object is declared elsewhere.
/*** Handle UUser-related logical operations here */@ Service (userManager) public class UUserManager {@ Autowired // use the Autowired annotation to declare, use Spring dependency injection to automatically instantiate the object. Private UUserDao userDao; // when the variable name must be userDao and the above @ Repository declares the class, the specified name is consistent // obtain the user information public UUser getUserByCpID (int appID, int channelID, String cpUserID) {String hql = from UUser where appID =? And channelID =? And channelUserID = ?; Return (UUser) userDao. findUnique (hql, appID, channelID, cpUserID);} // obtain the public List of all users in the specified channel
GetUsersByChannel (int channelID) {String hql = from UUser where channelID = ?; Return userDao. find (hql, new Object [] {channelID}, null);} // search for public Page by Page
QueryPage (int currPage, int num) {PageParameter page = new PageParameter (currPage, num, true); OrderParameters order = new OrderParameters (); order. add (id, OrderParameter. orderType. DESC); String hql = from UUser; return userDao. find (page, hql, null, order);} public UUser getUser (int userID) {return userDao. get (userID);} public void saveUser (UUser user) {userDao. save (user);} public void deleteUser (UUser user) {userDao. delete (user );}}
4. With UUser-related data objects and business operations, we need a controller to control URL access, parse and verify parameters, and call the logic at the business layer for processing, then return the result to the client. Create a UserAction class in the web directory.
Note: All Action classes must inherit the UActionSupport class. UActionSupport is a simple encapsulation of struts2 in the common directory.
If you want this Action to pass parameters in a data-driven manner, you need to implement the ModelDriven interface.
We recommend that you write the same business method in an Action class. For example, the User object operation. Add, delete, modify, query, and other operations are in this class
Http: // localhost: 8080/users/showUsers: Open the user list interface.
Http: // localhost: 8080/users/getAllUsers: retrieve user data by PAGE
Http: // localhost: 8080/users/saveUser: Add or edit user data
Http: // localhost: 8080/users/removeUser: delete user data
@ Controller // all actions are added with the Controller annotation @ Namespace (/users) // differentiate Namespace based on business. Namespace is used to prevent naming conflicts and the format is standard URLpublic class UserAction extends UActionSupport implements ModelDriven
{Private int page; // the page number of the current request, private int rows; // The number of rows displayed on each page, private UUser user; private int currUserID; @ Autowired private UUserManager userManager; // http: // localhost: 8080/users/showUsers // access this address, will access the WEB-INF/admin/users. jsp @ Action (value = showUsers, results = {@ Result (name = success, location =/WEB-INF/admin/users. jsp)}) public String showUsers () {return success;} // http: // localhost: 8080/users/getAllUsers @ Action (getAllUsers) public void getAllUsers () {try {Page
CurrPage = this. userManager. queryPage (page, rows); JSONObject json = new JSONObject (); json. put (total, currPage. getTotalCount (); JSONArray users = new JSONArray (); for (UUser m: currPage. getResultList () {users. add (m. toJSON ();} json. put (rows, users); renderJson (json. toString ();} catch (Exception e) {e. printStackTrace () ;}/// add or edit // http: // localhost: 8080/users/saveUser @ Action (saveUser) public Void saveUser () {try {userManager. saveUser (this. user); renderState (true); return;} catch (Exception e) {e. printStackTrace ();} renderState (false);} // http: // localhost: 8080/users/removeUser @ Action (removeUser) public void removeUser () {try {Log. d (Curr userID is + this. currUserID); UUser user = userManager. getUser (this. currUserID); if (user = null) {renderState (false); return;} userManager. deleteU Ser (user); renderState (true); return;} catch (Exception e) {e. printStackTrace ();} renderState (false);} private void renderState (boolean suc) {JSONObject json = new JSONObject (); json. put (state, suc? 1: 0); json. put (msg, suc? Operation successful: operation failed); renderText (json. toString () ;}@ Override public UUser getModel () {if (this. user = null) {this. user = new UUser ();} return this. user;} public UUser getUser () {return user;} public void setUser (UUser user) {this. user = user;} public int getPage () {return page;} public void setPage (int page) {this. page = page;} public int getRows () {return rows;} public void setRows (int rows) {this. rows = rows;} public int getCurrUserID () {return currUserID;} public void setCurrUserID (int currUserID) {this. currUserID = currUserID ;}}
We recommend that you use IntelliJ IDEA to develop the SSH2 project. This framework is also developed based on this IDE. If it is another IDE, you may need to manually convert and reconfigure it.