spring的OpenSessionInViewFilter配置

本文介绍了一个具体的web.xml配置示例,包括过滤器、监听器、servlet等元素的定义,并展示如何通过Spring框架进行单元测试,利用Spring的上下文加载及Hibernate会话管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
    
<filter>
        
<filter-name>hibernateFilter</filter-name>
        
<filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        
</filter-class>
        
<init-param>
            
<param-name>singleSession</param-name>
            
<param-value>false</param-value>
        
</init-param>
    
</filter>
    
<filter-mapping>
        
<filter-name>hibernateFilter</filter-name>
        
<url-pattern>/*</url-pattern>
    
</filter-mapping>


    
<filter>
        
<filter-name>webwork</filter-name>
        
<filter-class>
            com.opensymphony.webwork.dispatcher.FilterDispatcher
        
</filter-class>
    
</filter>
    
<filter-mapping>
        
<filter-name>webwork</filter-name>
        
<url-pattern>*.action</url-pattern>
    
</filter-mapping>
    
<listener>
        
<listener-class>
            org.springframework.web.context.ContextLoaderListener
        
</listener-class>
    
</listener>
  
<servlet>
    
<servlet-name>GetStaffService</servlet-name>
    
<servlet-class>ht.gisoa.ajaxservice.GetStaffService</servlet-class>
  
</servlet>

  
<servlet-mapping>
    
<servlet-name>GetStaffService</servlet-name>
    
<url-pattern>/servlet/GetStaffService</url-pattern>
  
</servlet-mapping>
    
<context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>classpath:applicationContext.xml</param-value>
    
</context-param>
    
<welcome-file-list>
        
<welcome-file>login.jsp</welcome-file>
    
</welcome-file-list>
    
<jsp-config>
        
<taglib>
            
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
            
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
        
</taglib>

        
<taglib>
            
<taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
            
<taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
        
</taglib>

        
<taglib>
            
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
            
<taglib-location>/WEB-INF/c.tld</taglib-location>
        
</taglib>

        
<taglib>
            
<taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
            
<taglib-location>/WEB-INF/c-rt.tld</taglib-location>
        
</taglib>

        
<taglib>
            
<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
            
<taglib-location>/WEB-INF/sql.tld</taglib-location>
        
</taglib>

        
<taglib>
            
<taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
            
<taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
        
</taglib>

        
<taglib>
            
<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
            
<taglib-location>/WEB-INF/x.tld</taglib-location>
        
</taglib>

        
<taglib>
            
<taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
            
<taglib-location>/WEB-INF/x-rt.tld</taglib-location>
        
</taglib>
    
</jsp-config>

</web-app>

如果要在TestCase里测试用例的话,则用创建如下类:
public abstract class AbstractTestBean extends TestCase {   
  
    
protected ApplicationContext applicationContext;
  
    
private SessionFactory sessionFactory;
  
    
private Session session;   
  
    
protected void setUp() throws Exception {
        String configFile 
= "applicationContext.xml"// spring/*.xml   
        applicationContext = new ClassPathXmlApplicationContext(configFile);   
        sessionFactory 
= (SessionFactory) applicationContext.getBean("sessionFactory");   
        session 
= SessionFactoryUtils.getSession(sessionFactory, true);   
        session.setFlushMode(FlushMode.NEVER);   
        TransactionSynchronizationManager.bindResource(sessionFactory, 
new SessionHolder(session));   
    }   
  
    
protected void tearDown() throws Exception {   
        TransactionSynchronizationManager.unbindResource(sessionFactory);   
        SessionFactoryUtils.releaseSession(session, sessionFactory);   
    }   
}   
然后再将自己的类继承自上类即可,如:
public class TestDB extends AbstractTestBean{
    
public void test1() {
        ApplicationContext ctx 
= new ClassPathXmlApplicationContext(
                
"applicationContext.xml");
        BuginfoDAO buginfoDAO 
= (BuginfoDAO) ctx.getBean("BuginfoDAO");
        BugtypeinfoDAO bugtypeinfoDAO 
= (BugtypeinfoDAO) ctx
                .getBean(
"BugtypeinfoDAO");
        Bugtypeinfo bugtype 
= bugtypeinfoDAO.findById(new Long(3502));
        Buginfo buginfo 
= new Buginfo();
        buginfo.setBugtypeinfo(bugtype);
        buginfo.setSvTitle(
"title");
        buginfoDAO.save(buginfo);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值