日期:2014-05-16  浏览次数:20649 次

SSH和Ajax的整合
整合步骤总结:
1,action中利用request.getParameter("*",x)可以获得jsp页面传过来的参数
2,jsp页面中加<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>后
<bean:parameter id="cid" name="companyId" />获得参数,使用方法:${cid}
<bean:define id="c" name="company" ></bean:define>获得对象,使用同上

3,hibernate中:一对多关系中,hibernate默认是懒的初始化(lazy=true),这样当你查询一的一方时,它不会级联查询多的一方,这样单的一方就没办法使用保存在类中多的一方的属性
如果想使用就必须在多的一方中加上lazy=false,如:
<many-to-one name="company" column="company_id"  lazy="false"></many-to-one>
当多的一方是通过外键指向单的一方主键时:默认的

4,jsp页面中实现国际化方法:

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
	<html:messages id="error" name="errorMessages">
		<font color="red">${error}</font>
	</html:messages>	其中name为配置文件中写的
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
	<bean:message key="login.title"/>	key中为配置文件写的

5,页面编码问题:
通常可以通过写过滤器来解决,但是写了过滤器之后,还可能出现页面乱码,url不支持中文传输,所有通常在传中文时最好用post方法(表单实现,get是用url实现),如果要用get也可以在tomcat中conf.server.xml文件中找到修改端口的地方加上URIEncoding="编码方式"即可。

6,级联删除:
如果想删除A表,但是B表的一个外键指向A表,关系维护在B表中,这时要向删除A表中的数据,首先要查出B表中指向A表的数据,然后把B表中的外键设为空,在删除A表中数据。
如果两个表都维护关系,写级联就OK

7,通过hibernate配置文件自动创建表:
(1)写好实体,和详细的xml影射文件
(2)在hibernate配置文件中添加
<!-- 此属性可以自动生成sql语句,自动创建表
<property name="hbm2ddl.auto">create</property>
-->
(3)然后在main方法中执行:ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
加载后即可生成sql语句。

8,利用PowerDesigner通过代码生成关系图:
file--->reverse engineer--->datebase--->弹出对话框
输入名字,选择数据库,单击确定
添加.sql文件后单击确定后可以生产图。

9,以下代码很有效:实现了分页查询所有对象的模板  //  <T>定义泛型

public <T> List<T> selectAllObject(Class<T> clazz,Page page) {
		String hql0 = "select count(*) from " + clazz.getSimpleName();
		String hql = "from " + clazz.getSimpleName();
		List<T> Objects = new ArrayList<T>();
		//  query()是分页实现方法
		for(Object obj : this.query(hql0, null, hql, null, page)){
			Objects.add((T) obj);
		}
		return Objects;
}


实现用对象或对象的属性实现分页查询:
/**实现HibernateCallback()方法所传的参数必须是final的,
*final T exampleEntity : 传过来的实体对象      final String propertyName :实体对象的属性名字
*final Object startData,final Object endData :通过时间查询时需要      final Page page :分页时需要
*/

import org.hibernate.Criteria;
	import org.hibernate.HibernateException;
	import org.hibernate.Query;
	import org.hibernate.Session;
	import org.hibernate.criterion.Criterion;
	import org.hibernate.criterion.Example;
	import org.hibernate.criterion.Expression;
	import org.springframework.orm.hibernate3.HibernateCallback;
	import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

	protected <T> List<T> selectByExampleEntity(final T exampleEntity,final String propertyName,
	  	final Object startData,final Object endData,final Page page) {
		Object object = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Example example = Example.create(exampleEntity); //通过实例创建Example
					//通过session获得标准
				Criteria criteria = session.createCriteria(exampleEntity.getClass());
						//如果以下这3个参数都不为空的时候执行
						//propertyName是两个时间对应的列的名字,两个时间是同一列的不同结果
				if(propertyName != null && startDate != null && endDate != null){
					Criterion c = Expression.between(propertyName, startDate, endDate);
					criteria.add((Criterion) c);
				}
				criteria.add(exa