| <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"> <struts-config> <form-beans> <form-bean name="loginForm" type="net.seerlog.vo.LoginForm"/> <form-bean name="replyForm" type="net.seerlog.vo.ReplyForm"/> </form-beans> <global-forwards> <forward name="index" path="/first.jsp"/> </global-forwards> <action-mappings> <action path="/login" type="net.seerlog.action.LoginAction" scope="request" name="loginForm" input="/login.jsp"> <forward name="success" path="/success.jsp"/> </action> <action path="/blog" type="net.seerlog.action.BlogAction" scope="request"> <forward name="success" path="/sublog.jsp"/> </action> <action path="/topic" type="net.seerlog.action.TopicAction" scope="request"> <forward name="success" path="/sutopic.jsp"/> </action> <action path="/posttopic" type="net.seerlog.action.PostTopicAction" scope="request" > </action> <action path="/postreply" type="net.seerlog.action.PostReplyAction" scope="request" name="replyForm" input="/sublog.jsp"> </action> <action path="/index" type="net.seerlog.action.IndexAction" scope="request"> </action> </action-mappings> <message-resources parameter="resouse.application"> </message-resources> </struts-config> |
| //导入所有用的到的包 import java.util.List; import net.seerlog.po.*; import net.seerlog.vo.*; import net.seerlog.util.*; import net.sf.hibernate.*; import org.apache.struts.action.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.IOException; import javax.servlet.ServletException; public class IndexAction extends Action{ //action的主体方法 execute ,注意要抛出异常 public ActionForward execute (ActionMapping mapping,ActionForm form,HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ try{ //开启会话 Session session = Hi.getSession(); Transaction tx= session.beginTransaction(); //查询 Query q = session.createQuery("from Topic as topic order by topic.id desc"); q.setFirstResult(0); q.setMaxResults(5); List newTopicList = q.list(); NewTopicList ntlBean=new NewTopicList(); ntlBean.setList(newTopicList); req.setAttribute("newtopiclist",ntlBean); //事务提交,关闭session tx.commit(); Hi.closeSession(); //捕获异常 }catch(HibernateException e){ Log.error(e.toString()); //日志记录 } //返回一个 actionForward 对象 return mapping.findForward("index"); } } |
关注此文的读者还看过: