WebWork2 combined with Spring, Hibernate

xiaoxiao2021-03-06  87

WebWork2 combines Spring, Hibernate from User, including creation / modification / delete user, and list of users, first look at the web2 Action code for brief explanation, do not consider any interceptor, duplicated submit, validation, etc. Create / modify / Delete User: useerAction.java related code

Java code:

1 UserManager UserManager;

2

3

public

Void SetUserManager

(Usermanager

)

{

...}

4 THIS.

UserManager = UserManager;

5

}

6

Seduce

public

String crete

(

)

{

...}

UserManager.

Create

(Getuser)

(

)

);

9

}

10

11

public

String modify

(

)

{

...}

12 UserManager.

Modify

(Modifyuser)

(

)

)

13

Return "Success";

14

}

15

16

public

String Remove

(

)

{

...}

17 Usermanager.

Remove

(Getuser)

(

)

)

18

Return "Success";

19

}

Xwork.xml related code

Java code:

1

Action name = "user"

Class = "COM.

Caryo.

User.

Actions.

Useraction>

2 UserManager

3 user.

JSP

4

Action>

WebWork Action will automatically accept information uploaded by the page, and also supports JavaBean's BeanInfo (BeanInfo has not been used, even direct acquisition attributes directly in the action, and the work of type conversion and verification) Webwork2 is very convenient to support Command mode, allowing an action to have multiple Command, the corresponding URL call user! Create.action, user! Modify.action, user! Remove.Action, the method in the userAction, then return the value according to the method to find the corresponding View, return "please" will forward to user.jsp. The userManager is the bean defined in the applicationContext in Spring.

Java code:

1

Class = "COM.

Caryo.

User.

DefaultUsermanager ">

2

3

List of User: ListUseraction.java related pieces Java code:

1 UserManager UserManager;

2

3

public

Void SetUserManager

(Usermanager

)

{

...}

4 THIS.

UserManager = UserManager;

5

}

6

// UserManager gets in a root statement of a USER, such as AbstractUseraction, here to repeat it to see

Seduce

8

public

List getAllUsers

(

)

{

...}

9 Usermanager.

GetAllUsers

(

);

10

}

11

12

public

List getonlineusers

(

)

{

...}

13 Usermanager.

GetonlineUsers

(

);

14

}

Xwork.xml related code

Java code:

1

Action name = "listuser"

Class = "COM.

Caryo.

User.

Actions.

Listuseraction>

2 UserManager

3 listuser.

JSP

4

Action>

The corresponding URL is Listuser.Action, which will automatically perform public string execute () without Command mode. Default is an empty method that returns "success", of course you can also rewrite this method, such as do checking, etc. Wait. Listuser.jsp Use WebWork Taglib to use WebWork Taglib to make a piece of User list

Java code:

1

2 Userid:

3 Username:

4

5

6

7 Userid:

8 Username:

9

The web taglib function is relatively powerful, which can also be called with parameters, and more than a relatively improvement of logic controls, seeing http://wiki.opensymphony.com/pages/viewpage.action?pageid=1876 As long as the design Reasonable, basically using its Taglib to control the entire view, including complex pages. Although Velocity, FreeMarker, but considering performance, recommendation or JSP View. The UI layer of large websites should be as thin as possible, JSP is not easy to maintain, I think taglib is the upper limit of acceptable thickness. Spring Hibernate is similar to the DHJ1 moderator, http://forum.javaeye.com/viewtopic.php? T = 7925 is not repeated. Just say some different places 1. Web.xml can use Listener to initialize basic information instead of servlet 2. In session in dao, you should use iterator to return data, then get count, not iterator.next () It is not to get the entire list, then list.size (), but should be a list.get (0); 3. Put HQL in the corresponding HBM.xml Java code:

1

public

Class HibernateVehiDao

Extends CaryohibernateObjectdao

Implements Vehicledao

{

...}

2

3

Class VehicleClass;

4

5

public

Class getPersistentClass

(

)

{

...}

6

Return VehicleClass! =

NULL? VEHICLECLASS:

VehicleClass = Vehicle.

Class

);

Seduce

}

8

9

Public Vehicle GetByid

(

Final long id

)

{

...}

10

Return

(Vehicle

GetByClassID

(ID)

);

11

}

12

13

public

List findallglobalvehicles

(

)

{

...}

14

Return FINDNAMEDQUERY

"" CARYO.

Vehicle_findallglobalvehicles

);

15

}

16

}

VEHICLE.HBM.XML related code

Java code:

1 转载请注明原文地址:https://www.9cbs.com/read-106096.html


New Post(0)