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中为配置文件写的
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;}
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(example);page.setOrderCount(new Long(criteria.list().size()));criteria.setFirstResult((page.getPageNumber()-1)*page.getMax());criteria.setMaxResults(page.getMax());return criteria.list();}});return (List<T>) object;}
<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> <init-param> <param-name>sessionFactoryBeanName</param-name> <!-- 对应spring.xml中的id --> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <!-- 指定要扩大事务的连接(一次请求) --> <url-pattern>*.do</url-pattern> </filter-mapping>
Date d = new SimpleDateFormat("yyyy-mm-dd").parse("1234-25-30");System.out.println(d);String str = new SimpleDateFormat("yyyy-mm-dd").format(d);System.out.print(str);