The integration of jsf,hibernate,spring and its talk

Source: Internet
Author: User

A recent study of spring has a little understanding of AOP,IOC. First, I send a consolidated code

1, use the IDE is NetBeans, personally feel what tools are the same.

2, Code structure view

3, the various classes of code, first from the DAO, Iuserdao and Userdaoimpl This needless to say, is the persistence layer of code. Use spring to manage hibernate. First you inherit hibernatedaosupport. Then we can use the Gethibernatetemplate () method.

Iuserdao.java

Package Com.fly.user.dao;

/**
 *
 * @author Fly.zhou
/public interface Iuserdao {
    //Add a record of the corresponding entity public  
    Boolean Add ( Object o);

    Update a record for the corresponding entity public
    boolean update (Object o);

    Add or update a record of the corresponding entity public
    Boolean addorupdate (Object o);

    Deletes a record of the corresponding entity public
    Boolean Delete (Object o);
}
Userdaoimpl.java

Package Com.fly.user.dao.daoimpl;

Import Com.fly.user.dao.IUserDao;
Import Org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 *
 * @author Administrator/public
class Userdaoimpl extends Hibernatedaosupport implements Iuserdao {

    @Override public
    boolean add (Object o) {
        gethibernatetemplate (). Save (o);
        return true;
    }

    @Override Public
    boolean update (Object o) {
        gethibernatetemplate (). Update (o);
        return true;
    }

    @Override Public
    boolean addorupdate (Object o) {
        gethibernatetemplate (). Saveorupdate (o);
        return true;
    }

    @Override Public
    Boolean delete (Object o) {
        gethibernatetemplate (). Delete (o);
        return true;
    }


4, with the support of the persistence layer, the business logic layer can be implemented. First of all, to define the interface Iuserservice, some people may not understand why to define Iuserdao and Iuserservice, in fact, this is the spring into the thought of a embodiment.
Iuserservice.java

Package com.fly.user.service;

Import Com.fly.user.entity.User;
Import java.util.List;

/**
 *
 * @author Administrator
/public interface Iuserservice {public

    boolean addUser (user user) ;

    public boolean updateuser (user user);

    public boolean deleteuser (user user);

    Public User Logincheck (string name, string passwd);

    public boolean loginout ();

    Public list<user> showusers ();

    public boolean ifusernamehave (Object pvalue);


Userserviceimpl.java

Package Com.fly.user.serviceimpl;
Import Com.fly.user.dao.IUserDao;
Import Com.fly.user.entity.User;
Import Com.fly.user.service.IUserService;

Import java.util.List; /** * * @author Administrator/public class Userserviceimpl implements Iuserservice {private Iuserdao userdao;/

    /This place cannot use the new method, or it loses the meaning of using spring, and an exception occurs, and then the set method must be generated, corresponding to the spring's set injection mode.
    Public Iuserdao Getuserdao () {return userdao;
    public void Setuserdao (Iuserdao userdao) {This.userdao = Userdao;
    @Override public boolean addUser (user user) {return userdao.add (user);  @Override public boolean updateuser (user user) {Throw the new unsupportedoperationexception ("not supported

    yet. ");} @Override public boolean deleteuser (user user) {Throw the new unsupportedoperationexception ("not supported yet.")
    ); @Override Public User Logincheck (string name, string passwd) {throw new unsupportedoperationexception("Not supported yet.");
    @Override public boolean loginout () {Throw the new unsupportedoperationexception ("not supported yet."); @Override public list<user> Showusers () {throw new unsupportedoperationexception (' not support

    Ed yet. ");} @Override public boolean ifusernamehave (Object pvalue) {throw new Unsupportedoperationexception (' Not Supporte
 D yet. ");}}
5, the business logic has been realized, for beans to say Userloginbean

public class Userloginbean {

    private iuserservice userservice;
    This place cannot use the new method, or it loses the meaning of using spring, and an exception occurs, and then the set method must be generated, corresponding to the spring's set injection mode.
    private String name;
    Private String passwd;

    Public Iuserservice Getuserservice () {return
        userservice;
    }

    public void Setuserservice (Iuserservice userservice) {
        this.userservice = userservice;
    }

    /**
     * Creates a new instance of Userloginbean *
     * public
    Userloginbean ()
    {} public

    void Addaction () {
        User user = new User (name, passwd, "", 1);
        Userservice.adduser (user);
    }

    Public String GetName () {return
        name;
    }

    public void SetName (String name) {
        this.name = name;
    }

    Public String getpasswd () {return
        passwd;
    }

    public void setpasswd (String passwd) {
        this.passwd = passwd;
    }
}
6, then spring is injected, and of course it's configured with Applicationcontext.xml.
<!--configuration DataSource use Hibernate2 to modify the first line below--> <bean id= "DataSource" class= Rce. Drivermanagerdatasource "> <property name=" driverclassname "> <value>com.mysql.jdbc.drive r<alue> </property> <property name= "url" > <value>jdbc:mysql://localhos t:3306/nrfmwebdb<alue> </property> <property name= "username" > <value>r oot<alue> </property> <property name= "password" > &LT;VALUE&GT;ROOT&LT;ALUE&G
        T </property> </bean> <!--configuration Sessionfactory, note the different--> of the packages introduced here <bean id= "Sessionfactory" class = "Org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <property name= "DataSource" > & Lt;ref local= "DataSource"/> </property> <property name= "Mappingresources" >
;list>                <value>User.hbm.xml<alue> <st> </property> <propert Y name= "hibernateproperties" > <props> <prop key= "Hibernate.dialect" >org.hiberna te.dialect.mysqldialect</prop> <prop key= "Hibernate.show_sql" >true</prop> &L t;/props> </property> </bean> <bean id= "TransactionManager" class= "Org.springframework". Orm.hibernate3.HibernateTransactionManager "> <property name=" sessionfactory "> <ref local=" Sessionfactory "/> </property> </bean> <bean id=" Userdao "class=" Com.fly.user.dao.daoimp
        L.userdaoimpl "> <property name=" sessionfactory "> <ref local=" sessionfactory "/>
        </property> </bean> <bean id= "UserService" class= "Com.fly.user.serviceimpl.UserServiceImpl" > <property name="Userdao" > <ref bean= "Userdao"/> </property> </bean> 

Besides this, there's faces-config.xml.

 <managed-bean> <managed-bean-name>userManage</managed-bean-name> & Lt;managed-bean-class>com.fly.user.beans.usermanagebean</managed-bean-class> <managed-bean-scope >session</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name> ;userlogin</managed-bean-name> <managed-bean-class>com.fly.user.beans.userloginbean</ Managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <MANAGED-PROPERTY&G
            T <property-name>userService</property-name> <value>#{userService}</value> </ Managed-property> </managed-bean> <application><variable-resolver> Org.springframework.web.jsf.delegatingvariableresolver</variable-resolver></application> 
You need to add the following configuration to replace the JSF variable parser with the spring's variable parser, so you can manage Managedbean in spring, and faces-config.xml don't need to configure Managedbean information. The configuration is as follows:

<bean id= "Managebean" name= "Userlogin" scope= "session" class= "Com.fly.user.beans.UserLoginBean" >
     < Property Name= "UserService" ref= "UserService"/><!--careful observation, will find with the Faces-config.xml configuration almost identical, very simple-->
</bean>
6, and finally the JSF page, here's a little.
Third, the key points of the Spring,jsf,hibernate integration are discussed in the end.
1, with spring MVC, you have to understand the injection, spring three injection methods, the single most common or set.
2, since the use of injection can not use new, the use of new can not inject, because this is in itself contradictory.
3, the Xxxdaoimpl class using Gethibernatetemplate () must inherit Hibernatedaosupport.
4, in general, for example, in the business implementation Xxxserviceimpl of the life-sustaining layer Ixxxdao interface, and then insert the set mode, while configuring the spring configuration file, typically two beans, one is the spring management business bean, One is the spring management entity bean. If you call several persistence-layer interfaces in Xxxserviceimpl, you need to configure several beans to manage them. If you want to give Managedbean to spring, you have a minimum of three profiles.

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.