Ext2.0 + struts2 + spring2.5 + ibatis2 + Hsqldb (Oracle) + velocity1.5 + semi-automatic "5"

2009-12-05  来源:本站原创  分类:Java  人气:342 

http://www.blogjava.net/ilovezmh/archive/2008/12/28/248725.html
Part V struts2 + spring2.5 + ibatis2 Integration

1.web.xml

The main configuration struts2 filters and spring start

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>action2</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>classpath:applicationContext.xml</param-value>
        <!--
        <param-value>classpath*:applicationContext*.xml</param-value>
         -->
    </context-param>

    <welcome-file-list>
      <welcome-file>login_sys.jsp</welcome-file>
    </welcome-file-list>

</web-app>

2.struts2 related configuration

struts.properties file (src below)

This document provides a framework to change the default behavior mechanisms. Under normal circumstances, if it is not intended to make it easier to debug if you do not need to change the document.

In the "struts.properties" attributes defined in the file can be in the "web.xml" file "init-param" tab in configuration, such as:

<init-param>
        <param-name>struts.devMode</param-name>
        <param-value>true</param-value>
    </init-param>

Or by "struts.xml" file "constant" tab to modify, such as:

<constant name="struts.devMode" value="true" />

In Struts2's wiki on the latest information on these attributes, the address of http://struts.apache.org/2.x/docs/strutsproperties.html.

Struts2-Core of the jar in the release, there is a default properties file, called "default.properties". If you want to make changes to the property, then

Only in the project source directory (src), create one called "struts.properties" document, and then you want to modify the properties added to the file,

The new property will be the default coverage out. In the development environment, the following attributes may be modified:

» Struts.objectFactory = spring

Integration with the spring

» Struts.i18n.reload = true

Re-enshrined in international documents to activate the functions of » struts.devMode = true

Activation pattern of development, in order to provide a more comprehensive debugging features.
» Struts.configuration.xml.reload = true

Reload the XML configuration file to activate the function (which is prepared for the Action), when the file has been modified since you do not need to reload Servlet container applied to the entire Web.

» Struts.url.http.port = 8080

To configure the server running on port number (all the generated URL will be created correctly)

struts.xml file (src below)

Contained in the document when the development of Action need to modify the configuration information, of course, we can even make the whole "struts.xml" file from the application removed

It all depends on the application's functional requirements. In this chapter we have discussed in the configuration information, can be replaced by other means, such as annotations,

"Web.xml" boot parameter and the URL mapping and replacement program.

Must be in the "struts.xml" in the configuration information that only the overall results, exception handling, and custom interceptor stack.

<?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>

    <include file="struts-system.xml" />

    <package
        name="struts2"
        extends="struts-default"
        namespace="/struts2">

        <default-interceptor-ref name="paramsPrepareParamsStack" />

        <global-results>
            <result name="timeout" type="dispatcher">/login.jsp</result>
        </global-results>

    </package>

</struts>

Personal recommend using a modular configuration, define multiple module configuration files, such as the struts-system.xml, etc., <include> come in, so that a clear structure.

There is a problem is that when there is the overall result, when each module on how to share, the solution is to the overall result defined in the struts.xml file, other modules continue bearing struts.xml,

Such as the struts-system.xml documents:

<?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>
    <package
        name="system"
        extends="struts2"
        namespace="/system">

           <action name="user">
               <result name="list" type="dispatcher">/system/user/list.jsp</result>
           </action>

          <action name="resource">
          </action>

          <action name="group">
          </action>

    </package>

</struts>

When the action can be used to configure a wildcard *, as follows:

<action name="*/*" method="{2}">
       <result name="list" type="dispatcher">/system/user/list.jsp</result>
  </action>

Such as: available by visiting: http://localhost:8080/test/system/user/doLogin.action access to user of this Action's doLogin method

struts2 default access method is user! doLogin.action.

3.ibatis2 configuration

sql-map-config.xml (src below)

The main configuration parameters and the number of ibatis modules sqlmap file, that is, the file sql statement.

Note: If you are using a Spring2.5 said that the previous version, you need to remove:
maxRequests = "32"
maxSessions = "10"
maxTransactions = "5"
Otherwise, will be prompted to Spring configuration error.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
    <settings cacheModelsEnabled="true"
            enhancementEnabled="true"
            lazyLoadingEnabled="true"
            useStatementNamespaces="false"
            statementCachingEnabled="true"
            classInfoCacheEnabled="true"
            errorTracingEnabled="true"/> 

    <sqlMap resource="com/zhu/system/user/ibatis/user.xml" />
    <sqlMap resource="com/zhu/system/resource/ibatis/resource.xml" />
    <sqlMap resource="com/zhu/system/group/ibatis/group.xml" />
    <!--
    ${object_conf}
    -->

</sqlMapConfig>

Note: That comment on the $ (object_conf) is automatically generated when adding the related configuration came with the applicationContext.xml inside.

4.spring file configuration

applicationContext.xml (src below)

Mainly configuration data source, transaction processing, ibatis of sqlMapClient, as well as the business logic of the DAO, Serviice, Action, etc.

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jee="http://www.springframework.org/schema/jee"
       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.5.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

    <!-- database.properties -->
    <bean>
        <property name="locations">
            <list>
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>
    <!-- dataSource -->
    <bean>
        <property name="driverClassName" value="${dataSource.driverClassName}" />
        <property name="url" value="${dataSource.url}" />
        <property name="username" value="${dataSource.username}" />
        <property name="password" value="${dataSource.password}" />
    </bean>

    <!-- dataSource -->
    <!--
    <bean destroy-method="close">
        <property name="driverClassName">
          <value>oracle.jdbc.driver.OracleDriver</value>
        </property>
        <property name="url">
          <value>jdbc:oracle:thin:@132.159.173.168:1521:EOS145</value>
        </property>
        <property name="username">
          <value>dc</value>
        </property>
        <property name="password">
          <value>dc</value>
        </property>
      </bean>
       -->

      <!-- transactionManager -->
      <bean>
        <property name="dataSource">
          <ref local="dataSource"/>
        </property>
      </bean>

      <!-- single transactionManager -->
      <!--
      <bean>
        <property name="transactionManager">
          <ref bean="transactionManager" />
        </property>
        <property name="target">
          <ref local="userDAO" />
        </property>
        <property name="transactionAttributes">
          <props>
            <prop key="insert*">PROPAGATION_REQUIRED</prop>
            <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
          </props>
        </property>
      </bean>
      -->

    <!-- transactionManager Advice -->
    <tx:advice transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="create*" rollback-for="Exception"/>
            <tx:method name="update*" rollback-for="Exception"/>
            <tx:method name="delete*" rollback-for="Exception"/>
            <tx:method name="*" read-only="true" rollback-for="Exception"/>
        </tx:attributes>
    </tx:advice> 

    <!-- transactionManager config -->
    <aop:config>
        <aop:pointcut expression="execution(* com.zhu.system.*.service.*(..))"/>
        <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
    </aop:config>

      <!-- ibatis config -->
      <bean>
        <property name="configLocation">
            <!-- <value>classpath:com\zhu\conf\sql-map-config.xml</value> -->
            <value>classpath:sql-map-config.xml</value>
        </property>
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>

      <!--  User   manage -->
      <bean>
        <property name="dataSource">
          <ref local="dataSource" />
        </property>
        <property name="sqlMapClient">
          <ref local="sqlMapClient" />
        </property>
      </bean>
    <bean>
        <property name="userDAO" ref="userDAO"/>
    </bean>
      <bean scope="prototype">
        <property name="userService" ref="userService"/>
    </bean>

    <!--  Resources   manage -->
    <bean>
        <property name="dataSource">
          <ref local="dataSource" />
        </property>
        <property name="sqlMapClient">
          <ref local="sqlMapClient" />
        </property>
      </bean>
      <bean>
        <property name="resourceDAO" ref="resourceDAO"/>
    </bean>
      <bean scope="prototype">
        <property name="resourceService" ref="resourceService"/>
    </bean>

    <!--  Organization   manage -->
    <bean>
        <property name="dataSource">
            <ref local="dataSource" />
        </property>
        <property name="sqlMapClient">
            <ref local="sqlMapClient" />
        </property>
    </bean>
    <bean>
        <property name="groupDAO" ref="groupDAO"/>
    </bean>
    <bean scope="prototype">
        <property name="groupService" ref="groupService"/>
    </bean>

    <!-- ${object_desc} manage -->
    <!--
    ${object_conf}
    -->

</beans>

applicationContext.xml can needs to be placed elsewhere, usually throwing the next on the list src.

Note that each of the Action bean where scope = "prototype" property is very important because it explains each request to create a new action object, to avoid data sharing.

5. Used in lib

Figure:
Ext2.0 + struts2 + spring2.5 + ibatis2 + Hsqldb (Oracle) + velocity1.5 + semi-automatic "5"

Struts2 in the package in which too many did not begin, all from the official published copy over the jar packages, spring there are many other packages, removing useless to the.

6. This configuration completed, according to the following structure sub -

Ext2.0 + struts2 + spring2.5 + ibatis2 + Hsqldb (Oracle) + velocity1.5 + semi-automatic "5"

Take up the framework, each application are similar, we can create these templates to generate some basic operations such as additions or deletions to change search methods, but only spent writing the logic of complex methods,

This can save a lot of time, improve efficiency, and this is the next section to introduce semi-automatic code generation.

相关文章