Struts2+Spring2+Hibernate3 web应用示例(六)

 

八、       配置Struts2

 

Struts的配置文件都会在web.xml中注册的。

a)        Struts的配置文件如下:

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.enable.DynamicMethodInvocation"  value ="false"   />
    
< constant  name ="struts.devMode"  value ="true"   />
    
< constant  name ="struts.i18n.encoding"  value ="GBK"   />    

    


struts >

Src/struts.xml

b)        struts_book.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 >

    
< package  name ="products"  extends ="struts-default" >
        

         

        
< action  name ="list"  class ="bookAction"  method ="list" >             
            
< result > /list.jsp result >
        
action >

    
< action  name ="delete"  class ="bookAction"  method ="delete" >             
            
< result  type ="redirect" > list.action?queryMap=${queryMap} result >
        
action >

        
< action  name ="*"  class ="com.sterning.commons.AbstractAction" >
            
< result > /{1}.jsp result >
        
action >
        
    
< action  name ="edit"  class ="bookAction"  method ="load" >
            
< result > /editBook.jsp result >
        
action >
       
       
< action  name ="save"  class ="bookAction"  method ="save" >
           
< interceptor-ref  name ="params" />
           
< interceptor-ref  name ="validation" />
            
< result  name ="input" > /editBook.jsp result >
            
< result  type ="redirect" > list.action?queryMap=${queryMap} result >
              
        
action >
    
package >
struts >

 

文件中的,使用了struts2自己的拦截器,拦截器在AOPAspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。

Struts 2已经提供了丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.6.jarstruts2-core-2.0.6.jar包的struts-default.xml查看关于默认的拦截器与拦截器链的配置。

struts-default.xml中已经配置了大量的拦截器。如果您想要使用这些已有的拦截器,只需要在应用程序struts.xml文件中通过struts-default.xml文件包含进来,并继承其中的struts-default包(package),最后在定义Action时,使用引用拦截器或拦截器栈(interceptor stack)。一旦您继承了struts-default包(package),所有Action都会调用拦截器栈 ——defaultStack。当然,在Action配置中加入可以覆盖defaultStack

作为框架(framework,可扩展性是不可或缺的,因为世上没有放之四海而皆准的东西。虽然,Struts 2为我们提供如此丰富的拦截器实现,但是这并不意味我们失去创建自定义拦截器的能力,恰恰相反,在Struts 2自定义拦截器是相当容易的一件事。所有的Struts 2的拦截器都直接或间接实现接口com.opensymphony.xwork2.interceptor.Interceptor。除此之外,大家可能更喜欢继承类com.opensymphony.xwork2.interceptor.AbstractInterceptor


 

九、       配置Spring

 

1Spring的配置文件如下:

xml version="1.0" encoding="UTF-8" ?>
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
 
< beans >
    

    
< bean  id  ="dataSource"  class  ="org.apache.commons.dbcp.BasicDataSource"  destroy-method ="close" >  
        
< property  name ="driverClassName"  value ="com.mysql.jdbc.Driver"   />  
        
< property  name ="url"  value ="jdbc:mysql://localhost:3306/game"   />  
        
< property  name ="username"  value ="root"   />  
        
< property  name ="password"  value ="root" />  
    
bean >  
    
    

    
< bean  id ="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >

        
< property  name ="dataSource" >
            
< ref  bean ="dataSource" />
        
property >
        
< property  name ="configLocation" >
            
< value > classpath:com/sterning/bean/hibernate/hibernate.cfg.xml value >
        
property >         
    
bean >
    
    

    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory" >
            
< ref  local ="sessionFactory" />
        
property >
    
bean >
    
    

    
< bean  id ="booksDao"  class ="com.sterning.books.dao.hibernate.BooksMapDao" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory" />
        
property >
    
bean >
    
    

    
< bean  id ="booksService"  class ="com.sterning.books.services.BooksService" >
        
< property  name ="booksDao" >
            
< ref  bean ="booksDao" />
        
property >
    
bean >
    
    
< bean  id ="pagerService"  class ="com.sterning.commons.PagerService" />
    
    

    
< bean  id ="bookAction"  class ="com.sterning.books.web.actions.BooksAction"  singleton ="false" >
        
< property  name ="booksService" >
            
< ref  bean ="booksService" />
        
property >
        
< property  name ="pagerService" >
            
< ref  bean ="pagerService" />
        
property >
    
bean >   
    
beans >

  WebRoot/WEB-INF/srping-content/applicationContent.xml

2Struts.properties.xml

本来此文件应该写在struts 配置一节,但主要是考虑这体现了集成spring的配置,所以放在spring的配置这里来讲。

struts.objectFactory = spring  
struts.locale=zh_CN
struts.i18n.encoding = GBK
 

struts.objectFactoryObjectFactory 实现了 com.opensymphony.xwork2.ObjectFactory接口(spring)。struts.objectFactory=spring,主要是告知Struts 2运行时使用Spring来创建对象(如Action等)。当然,SpringContextLoaderListener监听器,会在web.xml文件中编写,负责SpringWeb容器交互。

struts.localeThe default locale for the Struts application 默认的国际化地区信息。

struts.i18n.encoding:国际化信息内码。

十、       Web.xml配置


xml version="1.0" encoding="GB2312" ?>
DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd"
>

< web-app >
    
< display-name > 图书管理系统 display-name >
    
< context-param >
        
< param-name > log4jConfigLocation param-name >
        
< param-value > /WEB-INF/classes/log4j.properties param-value >
    
context-param >
    

    
< context-param >
        
< param-name > contextConfigLocation param-name >
        
< param-value > /WEB-INF/spring-context/applicationContext.xml param-value >
      
context-param >
    
    
< filter >
        
< filter-name > encodingFilter filter-name >
        
< filter-class > com.sterning.commons.SetCharacterEncodingFilter filter-class >
        
< init-param >
            
< param-name > encoding param-name >
            
< param-value > UTF-8 param-value >
        
init-param >
        
< init-param >
            
< param-name > forceEncoding param-name >
            
< param-value > true param-value >
        
init-param >
    
filter >
     
< filter >
        
< filter-name > struts2 filter-name >
        
< filter-class > org.apache.struts2.dispatcher.FilterDispatcher filter-class >
        
< init-param >
            
< param-name > config param-name >
            
< param-value > struts-default.xml,struts-plugin.xml,struts.xml,struts_books.xml param-value >
        
init-param >
    
filter >     

    
< filter-mapping >
        
< filter-name > encodingFilter filter-name >
        
< url-pattern > /* url-pattern >
    
filter-mapping >
    
< filter-mapping >
        
< filter-name > struts2 filter-name >
        
< url-pattern > /* url-pattern >
    
filter-mapping >         
    
    

      
< listener >
        
< listener-class > org.springframework.web.context.ContextLoaderListener listener-class >
      
listener >
    

      
< listener >
        
< listener-class > org.springframework.web.util.Log4jConfigListener listener-class >
      
listener >
 
    

    
< welcome-file-list >
        
< welcome-file > index.jsp welcome-file >
    
welcome-file-list >
web-app >

 

Struts 2中,配置有少许改变,最明显的是分发器(dispatcher)已由Servlet转为Servlet Filter, 其配置和Servlet一样简单。和Servlet配置一样,Filter配置中定义了Filter的名称(作为引用)和类名。Filter Mapping通过URI和名称匹配来调用相应的Filter。默认情况下,扩展名为“.action”,这是在default.properties文件(在Struts 2 JAR文件里)的“struts.action.extension”属性定义的。

待续.....

你可能感兴趣的:(Struts,struts,spring,hibernate,web,class,servlet)