<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Cyh的博客

    Email:kissyan4916@163.com
    posts - 26, comments - 19, trackbacks - 0, articles - 220

    Spring+JPA+Struts2整合

    Posted on 2009-02-27 15:48 啥都寫點(diǎn) 閱讀(1190) 評論(0)  編輯  收藏 所屬分類: J2EE


    Person類代碼
    package quickstart.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;
    @Entity
    public class Person {     
        @Id     
        @GeneratedValue     
        
    private Integer id;    
       
    private String lastName;    
        private
     String firstName;    
       
    public String getFirstName() {         
        
    return firstName;    
     }   
      
    public void setFirstName(String firstName) {         
           
    this.firstName = firstName;    
     }     
    public String getLastName() {         
           
    return lastName;    
    }     
    public void setLastName(String lastName) {         
            
    this.lastName = lastName;    
     }     
    public Integer getId() {         
            
    return id;    
     }     
    public void setId(Integer id) {         
       
    this.id = id;    
     }
    }
    創(chuàng)建一個(gè)接口,命名為"PersonService",包名為"quickstart.service" PersonService.java
    package quickstart.service; import java.util.List; import quickstart.model.Person; public interface PersonService {     
        
    public List<Person> findAll();
        
        
    public void save(Person person);     
        
        
    public void remove(int id);     
        
        
    public Person find(int id); }
    創(chuàng)建一個(gè)類,命名為"PersonServiceImpl",包名為"quickstart.service"PersonServiceImpl.java
    package quickstart.service;
    import java.util.List;
    import
     javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import
     javax.persistence.Query;
    import
     org.springframework.transaction.annotation.Transactional;
    import quickstart.model.Person;
    @Transactional
    public class PersonServiceImpl implements PersonService {     
       
    private EntityManager em;    
       @PersistenceContext    
       public void setEntityManager(EntityManager em) {         
        
    this.em = em;   
      }    
        @SuppressWarnings(
    "unchecked")   
        
    public List<Person> findAll() {         
        Query query 
    = getEntityManager().createQuery("select p FROM Person p");       
          
    return query.getResultList();   
      }   
       
    public void save(Person person) {        
       
    if (person.getId() == null) {           
            
    // new             em.persist(person);      
      } else {           
         
    // update             em.merge(person);         }   
      }   
           
    public void remove(int id) {        
            Person person 
    = find(id);       
            
    if (person != null) {          
              em.remove(person);       
         }   
     }   
        
    private EntityManager getEntityManager() {        
             
    return em;   
     }    
        
    public Person find(int id) {        
        
    return em.find(Person.class, id);  
       }
    }

    @PersistenceContext會(huì)讓Spring在實(shí)例化的時(shí)候給服務(wù)注入一個(gè)EntityManager。@PersistenceContext注解可以放在實(shí)例變量,或者setter方法前面。如果一個(gè)類被注解為@Transactional,Spring將會(huì)確保類的方法在運(yùn)行在一個(gè)事務(wù)中。
    JPA配置
    persistence.xml中就不用配置了,因?yàn)闀?huì)教給spring保管

    web.xml的配置
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="person" 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">    <display-name>person</display-name>  
     
    <filter>       
        
    <filter-name>struts2</filter-name>     
        
    <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
     
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>    
    </filter-mapping>
    <welcome-file-list>        
    <welcome-file>index.jsp</welcome-file>    
    </welcome-file-list>    
    <listener>       
    <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>  
    </listener>
    </web-app>
    applicationContext.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-2.0.xsd     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">    
       
    <
    bean   class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />    

    <
    bean id="personService" class="quickstart.service.PersonServiceImpl" />  
     
     
    <bean id="entityManagerFactory"    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">   
        
    <property name="dataSource" ref="dataSource" />    
        
    <property name="jpaVendorAdapter">         
              
    <bean   class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">               
                     
    <property name="database" value="MYSQL" />              
                     
    <property name="showSql" value="true" />          
              
    </bean>       
       
    </property>  
     
    </bean>   
     
    <bean id="dataSource"    class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
       
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />    
       
    <property name="url" value="jdbc:mysql://localhost/test" />    
       
    <property name="username" value="root" />     
      
    <property name="password" value="root" />    
    </bean>   
     

    <
    bean id="transactionManager"   class="org.springframework.orm.jpa.JpaTransactionManager">   
        
    <property name="entityManagerFactory" ref="entityManagerFactory" />  
    </bean>   
     

    <
    tx:annotation-driven transaction-manager="transactionManager" />  
     
    <bean id="personAction" scope="prototype" class="quickstart.action.PersonAction">      
      
    <constructor-arg ref="personService" />   
    </bean>

    </beans>

    注意"personAction"bean"class"屬性被設(shè)為Action類的名字,并且"personService"bean會(huì)作為參數(shù)傳遞到action的構(gòu)造器中。改變"dataSource"Bean"url", "username""passwrod"屬性為你數(shù)據(jù)庫的值。更多beans設(shè)置的細(xì)節(jié),請參看Spring的文檔。"scope"Spring2新增的屬性,它意味著Spring會(huì)在該類型的對象被請求時(shí)創(chuàng)建一個(gè)新的PersonAction對象。在Struts2里,一個(gè)新的action對象被創(chuàng)建,用來為每個(gè)請求服務(wù),這就是我們?yōu)槭裁葱枰?/span>scope="prototype"。



    現(xiàn)在我們需要?jiǎng)?chuàng)建一個(gè)簡單的Struts action,它封裝了PersonServices的方法。并且我們配置Struts使用Spring作為對象工廠。
    打開新建類對話框,輸入
    "PersonAction"作為類名,包名為"quickstart.action",內(nèi)容如下:
    package quickstart.action;
    import java.util.List;
    import quickstart.model.Person;
    import quickstart.service.PersonService;
    import
     com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.Preparable;

    public class PersonAction implements Preparable {     
      
    private PersonService service;  
      
    private List<Person> persons;   
      
    private Person person;  
      
    private Integer id;   
     
    public PersonAction(PersonService service) {         
        
    this.service = service;     
    }    
    public String execute() {         
         
    this.persons = service.findAll();       
        
    return Action.SUCCESS;    
     }   
     
    public String save() {         
           
    this.service.save(person);   
          
    this.person = new Person();    
          
    return execute();    
     }   
     
    public String remove() {         
           service.remove(id);   
          
    return execute();    
     }   
     
    public List<Person> getPersons() {         
          
    return persons;    
     }   
     
    public Integer getId() {         
         
    return id;   
     }   
     
    public void setId(Integer id) {        
         
    this.id = id;   
     }   
     
    public void prepare() throws Exception {         
         
    if (id != null)          
          person 
    = service.find(id);   
      }    
        
    public Person getPerson() {         
          
    return person;   
     }  
     
    public void setPerson(Person person) {      
         
    this.person = person;    
     }
    }
    struts.xml中的配置
    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>    
    <constant name="struts.objectFactory" value="spring" />  
     
    <constant name="struts.devMode" value="true" />  
     
    <package name="person" extends="struts-default">  
       
     
    <action name="list" method="execute" class="personAction">            
        <
    result>pages/list.jsp</result>        
       
    <result name="input">pages/list.jsp</result>       
     
    </action>       

     
    <action name="remove" class="personAction" method="remove">        
       
    <result>pages/list.jsp</result>     
       
    <result name="input">pages/list.jsp</result>      
     
    </action>     

      
    <action name="save" class="personAction" method="save">          
         
    <result>pages/list.jsp</result>         
         
    <result name="input">pages/list.jsp</result>    
      
    </action>  

     
    </package>

    </
    struts>

    設(shè)置"struts.objectFactory""spring"會(huì)強(qiáng)制Struts使用Spring來實(shí)例化action,并注入所有定義在applicationContext.xml中的依賴關(guān)系。每個(gè)action別名的"class"屬性被設(shè)置為"personAction",這也就是我們在applicationContext.xml中定義的PersonAction bean。要讓StrutsSpring一起工作,我們僅僅需要做上面這點(diǎn)事情。






                                                                                                           --    學(xué)海無涯
            

    主站蜘蛛池模板: 亚洲精品国产高清嫩草影院| 免费人成在线视频| 久久国产精品免费视频| 免费网站观看WWW在线观看| AAAAA级少妇高潮大片免费看| 一个人看的免费观看日本视频www 一个人看的免费视频www在线高清动漫 | 亚洲午夜久久久久妓女影院 | 久久久久久久91精品免费观看| 99热精品在线免费观看| 18禁成人网站免费观看| 亚洲最新永久在线观看| 亚洲视频在线观看免费| 亚洲国产V高清在线观看| 亚洲国产综合久久天堂| 亚洲日本乱码在线观看| 亚洲国产成人久久综合碰碰动漫3d | 免费av一区二区三区| 18未年禁止免费观看| 性感美女视频在线观看免费精品 | 91精品免费观看| 青娱分类视频精品免费2| 成人激情免费视频| 亚洲成人免费网站| 亚洲免费网站观看视频| 四虎影视免费永久在线观看| 成全影视免费观看大全二| 在线观看免费国产视频| 成全视频免费高清| 亚洲成a人在线看天堂无码| 亚洲精品无码不卡在线播放HE| 亚洲国产精品一区| 亚洲熟妇无码一区二区三区| 免费的黄色的网站| 午夜无码A级毛片免费视频| 免费无码AV电影在线观看| 免费一看一级毛片人| 久久精品亚洲中文字幕无码网站| 亚洲成AV人综合在线观看| 亚洲av无码专区首页| 国内永久免费crm系统z在线| 成年在线观看网站免费|