Struts2+spring+ibatis Integrated User Management example of the third: User management, additions and deletions to change

Source: Internet
Author: User


The flow of user management features is:

1. Management functions will jump to Usermanager's action via struts (Spring help injection instance)

2.UserManagerAction inside the ManagerService instance (also spring credit), call inside the method of adding and deleting

3.ManagerService instance directly invoke the concrete implementation method of the increment and deletion check in DAO

4. These specific implementations, through the spring-managed getsqlmapclienttemplate (), directly control the Ibatis

5. Ibatis to find the appropriate mapping configuration, read and write the database



1.UserManagerAction

An Usermanagerservice instance is aggregated, and the implementation method of adding and deleting the calls inside and out is:

public class Usermanageraction extends Actionsupport {private String username;
    private String password;
    
    Private Iusermanager service;
		Public usermanageraction () {} public String Queryall () {list<user_ssi> List = Service.queryall ();
		HttpServletRequest request = Servletactioncontext.getrequest ();
		Request.setattribute ("User", list);
	return SUCCESS;
		Public String Register () {Boolean flag = false;

    	Flag = Service.register (GetUserName (), GetPassword ()); Return flag==true?
    Success:error;
		Public String Update () {HttpServletRequest request = Servletactioncontext.getrequest ();
		String username = request.getparameter ("username");
		Boolean flag = false;
		System.out.println ("username=>" +username);
		System.out.println ("getusername () =" +getusername ());

    	Flag = service.update (username, GetPassword ()); Return flag==true?
	Success:error; } public String Delete () {HttpServletRequest request = SeRvletactioncontext.getrequest ();
		String Username=request.getparameter ("username");
		Boolean flag = false;

    	Flag = Service.delete (GetUserName ()); Return flag==true?
	Success:error; }//setter&getter}


2.ManagerService

A DAO instance is aggregated to enable layering

Directly to the DAO a Vo instance, this debugging time an error: should be completely dependent on spring to inject Vo, his service in the new out of VO may be wrong

public class ManagerService implements Iusermanager
{
	private user_ssi User;
    Private Iuserdao DAO;
    

	Public User_ssi GetUser () {
		return User;
	}
	public void SetUser (User_ssi u) {
		this.user = u;
	}
	public void Setdao (Iuserdao dao) {
		This.dao = DAO;
	}
	Public Iuserdao Getdao () {
		return dao;
	}
	
	
	Public list<user_ssi> Queryall () 
	{
		list<user_ssi> L1 = Dao.queryuser ();
		return L1;
	}
	
	public boolean register (string username, string password)
    {
		user.setusername (username);
    	User.setpassword (password);
    	Dao.insertuser (user);//A Nullpointer exception, because it is not possible to create a new user here, but to rely on spring injection

    	return true;
	
	public boolean update (string username, string password) 
	{
		user.setusername (username);
    	User.setpassword (password);
    	return Dao.updateuser (user);
	
	public boolean Delete (String username) 
	{
		return Dao.deleteuser (username);
	}
	
}



3.UserDaoImpl

Ibatis to read the configuration of the task to spring, where the introduction of Sqlmapclientdaosupport directly call template

Save a lot of things.

Accordingly, the information that Ibatis should read should be configured in spring.

A namespace user is added to this implementation, and the configuration is ibatis responsible for the

public class Userdaoimpl extends Sqlmapclientdaosupport implements Iuserdao {/* here do not have to be initialized manually, waiting for spring to complete the direct use of * SPRI
	Ng will go into the service and inject a DAO * private static Sqlmapclient Sqlmap with DataSource and Ibatis configuration; 
	      void Init () {try {reader reader = Resources.getresourceasreader ("Sqlmapconfig.xml");//read in XML configuration with reader, note the location
	      Sqlmap = sqlmapclientbuilder.buildsqlmapclient (reader); 
	    Reader.close ();
	      } catch (IOException e) {//Fail fast.
	    throw new RuntimeException ("Something bad happened while building the Sqlmapclient instance." + E, E);
		}} */Public list<user_ssi> Queryuser () {list<user_ssi> List = null;
		List = Getsqlmapclienttemplate (). queryForList ("User.getalluser");	
	return list;
		} public boolean deleteuser (String ID) {int deletedcount = 0;
		
		Deletedcount = Getsqlmapclienttemplate (). Delete ("User.deleteuserbyid", id);	
	Return deletedcount>0?true:false; } public void Insertuser (User_ssi u) {int Insertcount = 0;	
	Getsqlmapclienttemplate (). Insert ("User.insertuser", u);  
		} public boolean UpdateUser (User_ssi u) {int updatedcount = 0;
		Updatedcount = Getsqlmapclienttemplate (). Update ("User.updateuser", u); 
		System.out.println ("updatedcount=>" +updatedcount);
	Return updatedcount>0?true:false;
		Public list<user_ssi> queryusername (String name) {list<user_ssi> List = null;
		List = Getsqlmapclienttemplate (). queryForList ("User.selectuserbyusername", name);
	return list; }

}



4.iBatis mapping file for user This is nothing to say, Ibatis's job, integration will not change

<?xml version= "1.0" encoding= "UTF-8"?> <!

DOCTYPE sqlmap Public "-//ibatis.apache.org//dtd SQL Map 2.0//en" "Http://ibatis.apache.org/dtd/sql-map-2.dtd" > <sqlmap namespace= "User" > <typealias alias= "user" type= "vo. User_ssi "/><!--aliases--<resultmap id=" Userresult "class=" User "> <result property=" username "column=" Username "/> <result property=" password "column=" password "/> </resultMap> <select id=" Getalluser "R esultclass= "User" parameterclass= "string" > select * from User_ssi </select> <select id= "Selectuse
    Rbyusername "parameterclass=" string "resultclass=" User "> select username, password from User_ssi where username = #username # </select> <insert id= "Insertuser" parameterclass= "User" > INSERT INTO user_s SI (username, password) VALUES (#username #, #password #) </insert> <update id= "UpdateUser" Paramet
	erclass= "User" >	Update User_ssi Set password = #password # where username = #username # </update> <delete id= "DeleteUser
 Byid "parameterclass=" string "> Delete from User_ssi where username = #username # </delete> </sqlMap>




5. JSP for display

Management window is responsible for: modify and delete

Click the Change icon, a new window action pops up,

After the modification, the popup window will refresh this page (refresh the parent page)

<form action= "#" id= "MyForm" name= "MyForm" method= "POST" > <table border=1 width= "80%" > <tr> <th wi Dth= "39%" > User name </th> <th width= "39%" > Password </th> <th> modify </th> <th> Delete </th> &lt ;/tr> <tr> <s:iterator value= "#request. User" id= "id" status= "st" > <s:if test= "#st. index%2==0| | #st. Last "> <tr> </s:if> <td><s:property value=" #id. Username "/></ td> <td><s:property value= "#id. Password"/></td> <td><input style= "width:40px; height:40px "name=" button "type=" button "class=" Class1 "onmouseover=" This.classname= ' class2 ' "onmouseout="  This.classname= ' Class1 ' "onclick=" window.open (' Update.jsp?username=<s:property value= ' #id. Username "
					/>&password=<s:property value= "#id. Password"/> ', ' new ', ' height=500,width=800,top=100,left=400 ') " Value= "Modify"/></td> <td><inputStyle= "width:40px; height:40px "name=" button "type=" button "class=" Class3 "onmouseover=" this.classname= ' class4 ' "onmouseout=" This.clas Sname= ' Class3 ' "onclick=" Dolink (' Delete?username=<s:property value= ' #id. Username "/>") "VA Lue= "Delete"/></td> <s:if test= "#st. index%2==0| | #st. Last "> </tr> </s:if> </s:iterator> </tr> </ Table> </form>


The result after the modification

This is a pop-up window, to refresh the parent page, otherwise he shut down, the parent page of the data must be manually refreshed to be consistent

  <body>

    































Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.