论坛首页 Java企业应用论坛

T5+spring2.0+hibernate3.2使用OpenSessionInViewFilter

浏览 2508 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-06-17  

按照网上讲的方法,在web.xml文件中加入filter,在页面访问延时加载的数据时,报session关闭的错误,仔细检查代码未发现问题
后又仔细检查了web.xml文件,尝试调整了filter的顺序,居然成功了
现将各部分代码贴出来

例子有三个类,分别是:Man,Address,Phone

分别存放人的信息,人的地址,人的电话号码

详细请看代码
web.xml文件内容:

 

<!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>cs Tapestry Application</display-name>
	
	<context-param>
		<param-name>tapestry.app-package</param-name>
		<param-value>com.me.sys</param-value>
	</context-param>
	<!--
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml </param-value>
	</context-param>	
	-->
	<filter>
		<filter-name>setcharacterencodingfilter</filter-name>
		<filter-class>
			org.springframework.web.filter.CharacterEncodingFilter
		</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-mapping>
		<filter-name>setcharacterencodingfilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!--OpenSessionInViewFilter的filter一定要放在TapestrySpringFilter的前面,否则请求会被Tapestry截获,就会出先session关闭的问题-->
    <filter>
    	<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	
	
	
	<filter>
		<filter-name>sys</filter-name>
		<filter-class>org.apache.tapestry.spring.TapestrySpringFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>sys</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	

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

 

Man类代码:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Proxy;

@Entity
public class Man implements Serializable {
	private static final long serialVersionUID = 1L;

	private int id;

	private String name;

	private Address address;

	private List<Phone> phones = new ArrayList<Phone>();

	public Man() {

	}

	public Man(String name, Address address, List<Phone> phones) {
		this.name = name;
		this.address = address;
		this.phones = phones;
	}

	@OneToOne(fetch = FetchType.LAZY)@JoinColumn(name = "addressId", nullable = true)
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "man", fetch = FetchType.LAZY)
	public List<Phone> getPhones() {
		return phones;
	}

	public void setPhones(List<Phone> phones) {
		this.phones = phones;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 Address类代码:

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
public class Address implements Serializable {
	private static final long serialVersionUID = 1L;

	private int id;

	private String address;

	public Address() {
	}

	public Address(String address) {
		this.address = address;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}

 

Phone类代码:

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Proxy;

@Entity
public class Phone implements Serializable {
	private static final long serialVersionUID = 1L;

	private int id;

	private String code;

	private Man man;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "manId", nullable = false)
	public Man getMan() {
		return man;
	}

	public void setMan(Man man) {
		this.man = man;
	}

	public Phone() {

	}

	public Phone(String code) {
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

 

service部分代码就不贴了,下面贴下TestMan1.tml代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
    <form t:type="form">
        <t:loop source="Mans" value="man">
            ${man.name}
            <t:loop source="Phones" value="phone">
                ${phone.code}
            </t:loop>
            <br/>
        </t:loop>
    </form>
</html>

 

TestMan1.java代码:

public class TestMan1{
	private static final Log log = LogFactory.getLog(TestMan1.class);

	public static final long serialVersionUID = 1L;


	private Man man;

	private Phone phone;

	public Collection<Man> getMans() {
		return(Collection) getTestService().getMans();
	}

	public Collection<Phone> getPhones() {
		return man.getPhones();
	}

	public Man getMan() {
		return man;
	}

	public void setMan(Man man) {
		this.man = man;
	}

	public Phone getPhone() {
		return phone;
	}

	public void setPhone(Phone phone) {
		this.phone = phone;
	}
}

 

 

   发表时间:2008-07-15  
發一個demo 上來。。比較直觀點啦
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics