Struts2+spring+ibatis One of the integrated user management instances: Multiple action, service configuration

Source: Internet
Author: User
Tags aop rollback


Oneself used to practice the entry-level instances, the master can directly ignore haha.

These days, no time, no control, the front desk display also some scriptlet did not change to label, and so on some days have free to add it.

The division of the three framework will not be described, the previous blog has 22 of the detailed records of the integration, mainly about the program.

One: Structure

Program functions are mainly divided into "login log Out" and "user Management" two parts:

The corresponding action and service are divided into two parts, loginaction and usermanageraction, according to these two contents. DAO has only one, that is, action and service are divided into two lines by business in struts and spring management, and converge in DAO when Ibatis is managed.

In addition struts through session management login, log out information, file structure system is as follows:


II: Configuration of SSI

First, the general implementation of the process, and then analyze the specific implementation


1.struts Configuration

Struts is equipped with two action:login and Usermanager (of course, by spring)

Login and Logout loginaction,

User management of the additions and deletions change to go usermanager

<struts> <!--Chinese prevent garbled--<constant name= "struts.i18n.encoding" value= "GBK"/> <package Name= "struts2-spring" namespace= "/" extends= "Struts-default" > <!--1.Login action--> <action name= "Login "class=" Login "method=" login ><!--login is a loginaction instance of Spring injection (loginaction contains a loginservice)-< Result name= "Success" >loginSuccess.jsp</result> <result name= "error" >loginFail.jsp</result> & lt;/action> <action name= "LogOut" class= "Login" method= "LogOut" ><!--different methods for the same instance--<result name= "s Uccess ">index.jsp</result> </action> <!--2.Manager action--> <action name=" register "class= "Usermanager" method= "register" ><!--Usermanager is spring injected usermanageraction instance--<result name= "success ">operSuccess.jsp</result> <result name=" error ">register.jsp</result> </action> <act Ion Name= "Queryall" class= "Usermanager"method=" Queryall "> <result name=" Success ">manage.jsp</result> </action> <action name=" upd Ate "class=" Usermanager "method=" Update "> <result name=" Success ">operSuccess.jsp</result> </ action> <action name= "delete" class= "Usermanager" method= "delete" > <result name= "Success" >opersuccess .jsp</result> </action> </package> </struts>


2.spring Configuration

The spring configuration is broadly divided into four parts:

1: Configure Data source, change from Ibatis configuration to spring configuration

The ibatis of the 2:spring is formulated in order to Sqlmapclienttemplate

3:spring injection process (here and Struts fit)

4: Transaction processing, AOP implementation

	<!--1: Configure the data source (change from Ibatis configuration to spring Configuration)-<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSour Ce "destroy-method=" close "> <property name=" driverclassname "value=" Oracle.jdbc.driver.OracleDriver "/&G    
        T <property name= "url" value= "Jdbc:oracle:thin: @localhost: 1521:orcl"/> <property name= "username" value = "Scott"/> <property name= "password" value= "890307"/> </bean> <!--2:spring The Ibatis is formulated to sqlmapclienttemplate--<bean id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMa    
        Pclientfactorybean "> <property name=" configlocation "value="/web-inf/sqlmapconfig.xml "/> <property name= "DataSource" ref= "DataSource"/> </bean> <!--3.Spring injection process--<bean ID = "User_ssi" class= "Vo. User_ssi "/> <!--daoimpl injection <bean name=" Userdao "DAO. Userdaoimpl "> <properTy name= "sqlmapclient" ref= "sqlmapclient" ></property> </bean> <!--login to inject--and <bean Name= "Loginservice" class= "service.  Loginservice "> <property name=" User "ref=" User_ssi "></property> <property name=" DAO "ref=" Userdao " /> </bean> <bean name= "Login" class= "action. Loginaction "><!--generate an instance called login, call the Set ' service ' method, inject the previous instance ' Loginservice '--<property name=" service " ref= "Loginservice"/> </bean> <!--Manager Injection--<bean name= "ManagerService" class= "service. ManagerService "> <property name=" User "ref=" User_ssi "></property> <property name=" DAO "ref=" Userda o "/> </bean> <bean name=" Usermanager "class=" action. Usermanageraction "> <property name=" service "ref=" ManagerService "/> </bean> <!--4. Transaction processing, need matching xmlns and schemalocation--<bean id= "TransactionManager" class= " Org.springframework.jdbc.datasource.DataSourceTrAnsactionmanager "> <property name=" dataSource "ref=" DataSource "/> </bean> <t    
          X:advice id= "Transactionmanageradivice" transaction-manager= "TransactionManager" > <tx:attributes> <tx:method name= "query*" propagation= "REQUIRED" read-only= "true" rollback-for= "Java.lang.RuntionException"/&    
          Gt <tx:method name= "update*" propagation= "REQUIRED" rollback-for= "java.lang.RuntionException"/> </tx:att ributes> </tx:advice> <aop:config> <aop:pointcut id= "Allmanagermethod" Expres sion= "Execution (* service.*.* (..))" /> <!--execution range-<aop:advisor advice-ref= "Transactionmanageradivice" pointcut-ref= "Allmanagermethod  "/> </aop:config>


3.iBatis Configuration

Most of the configurations let spring take over,

Only the specific configuration of the namespace and Vo are two things

<sqlMapConfig>   

  <!--ibatis namespace, which is still the file after consolidation--  
  <!--But the datasource configuration is Spring tube--  
  < Settings usestatementnamespaces= "true"/>   
  
  <!--database configuration is given to Applicationcontext-common.xml file management  
  < TransactionManager type= "jdbc" >  
    <datasource type= "simple" >  
      <property name= "jdbc. Driver "value=" Oracle.jdbc.driver.OracleDriver "/>  
      <property name=" jdbc. Connectionurl "value=" Jdbc:oracle:thin: @localhost: 1521:orcl "/>  
      <property name=" jdbc. Username "value=" Scott "/>  
      <property name=" JDBC. Password "value=" 890307 "/>  
    </dataSource>  
  
  <!--the corresponding configuration for each table and class, this does not change--and  
  <! --specific configuration, also unchanged, with Ibatis original configuration--     
	<sqlmap resource= "User_ssi.xml"/>
	
</sqlMapConfig>


4.webApp Web. XML

Here you want to specify that the filter is struts2, add a spring listener, and configure the spring configuration file location, etc.

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class> Org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</ Url-pattern>
  </filter-mapping>

<listener>
    <listener-class>org.springframework.web.context.contextloaderlistener</ listener-class>
  </listener>
  <context-param>
    <param-name> Contextconfiglocation</param-name>
    <param-value>/web-inf/applicationcontext.xml,classpath*: Applicationcontext.xml</param-value>
  </context-param>





































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.