SSH框架 (Spring、Status、Hibernate)
运行流程 :
前台页面发送请求到status.xml根据请求的前缀(email) 例如:请求路劲emailselectFirstComment
找到所对应的action的class的例如:EmailAction
action中返回的字符串和result的name值比对 跳转页面或者重定向到某个方法上
<action name="email*" class="EmailAction" method="{1}">
<result name="recipients">/E_write.jsp</result>
<result name="add">/E_write.jsp</result>
<result name="input" type="redirectAction">emailrecipients</result>
<result name="selectFirstPage">/E_details.jsp</result>
<result name="selectNextPage">/E_details.jsp</result>
<result name="delete" type="redirectAction">emailselectFirstPage</result>
<result name="spam">/E_delete.jsp</result>
<result name="find">/E_view.jsp</result>
<result name="reduction" type="redirectAction">emailspam</result>
<result name="deleteReal" type="redirectAction">emailspam</result>
<result name="error">/error.jsp</result>
</action>
,在action控制层中找到除去前缀(email)的方法名 selectFirstComment 和mvc一样调用service和dao层方法
/**
* 分页查询
* @return
*/
public String selectFirstComment(){
HttpServletRequest request1 = ServletActionContext.getRequest();
Map<String,Object> request = (Map<String, Object>)ActionContext.getContext().get("request");
int pageSize = 5;
Integer page = 1;
if(request1.getParameter("page")!=null){
page = Integer.parseInt(request1.getParameter("page"));
}
try{
List list = commentBiz.selectByPage(page, pageSize);
int totalPage = commentBiz.totalPage();
int zcount = totalPage%pageSize==0 ? totalPage/pageSize : totalPage/pageSize+1;
request.put("commentList", list);
request.put("totalPage", zcount);
request.put("currentPage", page);
}catch(Exception e){
e.printStackTrace();
}
return "selectFirstComment";
}
在dao层执行sql语句
/**
* 分页查询,获取每页的记录
*/
@SuppressWarnings("unchecked")
@Override
public List<Email> selectByPage(String className, int pageNo, int pageSize,String userName) {
Query query = this.getSession().createQuery("from "+ className + " where RECIPIENTS =:userName and isDeleted =1")
.setString("userName", userName);
query.setFirstResult((pageNo - 1) * pageSize);//每页显示的第一条记录
query.setMaxResults(pageSize);//每页显示的记录数
return query.list();
}
/**
* 分页查询邮件,获取总页数
*/
@Override
public int totalPage(String className,String userName) {
String hql = "select count(*) from "+ className + " where RECIPIENTS =:userName and isDeleted =1";
Query query = this.getSession().createQuery(hql).setString("userName", userName);
int total = Integer.parseInt(query.list().get(0).toString());
return total;
}
注意的时在SSH中要配置bean 需要在applicationContext.xml中配置
<!-- Email dao -->
<bean id="EmailDaoImpl" class="com.accp.dao.impl.EmailDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- biz -->
<bean id="EmailBizImpl" class="com.accp.biz.impl.EmailBizImpl">
<property name="emailDao" ref="EmailDaoImpl" />
</bean>
<!-- action -->
<bean id="EmailAction" class="com.accp.action.EmailAction" scope="prototype">
<property name="emailBiz" ref="EmailBizImpl" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="mappingResources">
<list>
<value>com/accp/pojo/Email.hbm.xml</value>
</list>
</property>
</bean>