Struts 2 + hibernate 3 + spring basic routines

Source: Internet
Author: User
Everything has certain routines and rules, and Struts 2 + hibernate 3 + spring is also the same. struts2 is much better than struts1, but currently the only thing that is uncomfortable
Struts2 does not have any good ide tools to support design, such as dw8, and hopes to improve it in the future.
Here, we use a simple user registration and login to describe the simple combination between the three. The others can be done as follows:
1. Create a Domain Layer
Create a package domain with a user. Java
Public class user {
Private long ID;
Private string username;
Private string password;
// Getter, setter .....

2) create user. HBM. xml
Omitted
3) create a DaO Layer
Create a DaO package with userdao. Java

Import java. util. List;

Import Liao. domain. user;

Public interface userdao {
Public user findbyid (long ID );
Public void save (User user );
Public list <user> findbyusernameandpassword (string username, string password );
Public list <user> findbyusername (string username );
}

4. Create userdaoimpl. Java

Import java. util. List;

Import org. springframework. Orm. hibernate3.support. hibernatedaosupport;

Import Liao. Dao. userdao;
Import Liao. domain. user;

Public class userdaoimpl extends hibernatedaosupport implements userdao {

Public user findbyid (long ID ){
Return (User) gethibernatetemplate (). Get (user. Class, ID );
}

Public list <user> findbyusername (string username ){
Return gethibernatetemplate (). Find ("from user where username =? ", New string [] {username });
}

Public list <user> findbyusernameandpassword (string username, string password ){
Return gethibernatetemplate (). Find ("from user where username =? And Password =? ", New string [] {username, password });
}

Public void save (User user ){
Gethibernatetemplate (). saveorupdate (User );
}

}

5. Set up the sevrice layer and set up the service package.
Import edu. jlu. Fuliang. domain. user;

Public interface usermanageservice {
Public void register (User user );
Public Boolean login (User user );
}

6. Service implementation
  

Import java. util. List;

Import Liao. Service. usermanageservice;
Import Liao. Dao. userdao;
Import Liao. domain. user;

Public class usermanageserviceimpl implements usermanageservice {

Private userdao;

Public Boolean login (User user ){
List <user> Users = userdao. findbyusernameandpassword (user. GetUserName (), user. GetPassword ());
Return users. Size ()! = 0;
}

Public void register (User user ){
Userdao. Save (User );
}
 
Public userdao getuserdao (){
Return userdao;
}

Public void setuserdao (userdao ){
This. userdao = userdao;
}

}

After 7, it is the action layer of struts2.
First, register registeraction. Java

Import com. opensymphony. xwork2.actionsupport;

Import Liao. Service. usermanageservice;
Import Liao. domain. user;

Public class registeraction extends actionsupport {
Private user;
Private usermanageservice;
 
Public String execute () throws exception {
Usermanageservice. Register (User );
Return success;
}
 
Public user getuser (){
Return user;
}
 
Public void setuser (User user ){
This. User = user;
}
 
Public void setusermanageservice (usermanageservice ){
This. usermanageservice = usermanageservice;
}
}

Loginaction. Java
 

Public class loginaction extends actionsupport {
Private user;
Private usermanageservice;
 
Public String execute () throws exception {
If (usermanageservice. login (User )){
Servletactioncontext. getcontext (). getsession (). Put ("user", user );
Return success;
}
Return login;
}

Public user getuser (){
Return user;
}

Public void setuser (User user ){
This. User = user;
}

Public void setusermanageservice (usermanageservice ){
This. usermanageservice = usermanageservice;
}
}

8. Configure applcaition. xml. We recommend that you separate several configurations to avoid placing them all in one XML
Application-db.xml: more traditional, not to mention
Application-dao.xml:
<Bean id = "userdao" class = "Liao. Dao. impl. userdaoimpl">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
Application-service.xml:
<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: AOP = "http://www.springframework.org/schema/aop"
Xmlns: Tx = "http://www.springframework.org/schema/tx"
Xsi: schemalocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop.xsd>
<Bean id = "usermanageservice" class = "Liao. Service. impl. usermanageserviceimpl">
<Property name = "userdao">
<Ref bean = "userdao"/>
</Property>
</Bean>
<AOP: config>
<AOP: pointcut id = "serviceoperation" expression = "execution (* edu. jlu. Fuliang. Service. impl. * serviceimpl. * (..)"/>
<AOP: Advisor pointcut-ref = "serviceoperation" advice-ref = "txadvice"/>
</AOP: config>
 
<TX: Advice id = "txadvice">
<TX: Attributes>
<TX: method name = "get *" Read-Only = "true"/>
<TX: method name = "*"/>
</TX: Attributes>
</TX: Advice>
 
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
9 struts. xml
<Action name = "login" class = "loginaction">
<Result name = "success">/uploadsong. jsp </result>
<Result name = "login">/login. jsp </result>
</Action>

<Action name = "register" class = "registeraction">
<Result name = "success" type = "Redirect-action"> listsong </result>
<Result name = "input">/register. jsp </result>
</Action>
10 web. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>

<Filter>
<Filter-Name> Struts-cleanup </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. actioncontextcleanup </filter-class>
</Filter>

<Filter>
<Filter-Name> sitemesh </filter-Name>
<Filter-class> com. opensymphony. Module. sitemesh. Filter. pagefilter </filter-class>
</Filter>

<Filter>
<Filter-Name> struts </filter-Name>
<Filter-class> org. Apache. struts2.dispatcher. filterdispatcher </filter-class>
</Filter>

<Filter>
<Filter-Name> hibernatefilter </filter-Name>
<Filter-class> org. springframework. Orm. hibernate3.support. opensessioninviewfilter </filter-class>
</Filter>

<Filter-mapping>
<Filter-Name> Struts-cleanup </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>


<Filter-mapping>
<Filter-Name> sitemesh </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>


<Filter-mapping>
<Filter-Name> struts </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

<Filter-mapping>
<Filter-Name> hibernatefilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value> classpath: applicationcontext-*. xml </param-value>
</Context-param>
 
<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>
</Web-app>

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.