简单的MVC模式struts1.2添加hibernate3的问题!!!!
[color=#FF0000]程序本来是单独的struts1.2,在struts.config.xml里配置数据源操作数据库的,现在添加了hibernate3作为持久层来操作数据库。hibernate的配置文件已经配置好了。<session-factory> 和C3P0连接池也弄好了。数据库表的xml文件也创建好了,映射文件也注册了。 相应的DAO接口,实现类,都写了。
但是ActionForward不会改了~~~具体代码该怎么写。。。注意是struts1.2的,给几个例子吧。 我把我原来的代码发一下,你们看一下怎么改,哪里需要改。
原ActionForward类:
public class AllNewsAction extends DispatchAction { public ActionForward addnews(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { NewsForm AllNewsActionForm = (NewsForm) form; try { Service actionNews = new Service(AllNewsActionForm); if (((NewsForm) form).getNews_theme().trim().equals("") || ( (NewsForm) form).getNews_author().trim().equals("") || ((NewsForm) form).getNews_date().trim().equals("")) throw new Exception("主题,作者,日期,三者不允许为空!"); else { actionNews.save((NewsForm)form); request.setAttribute("info", "保存成功!"); } } catch (Exception e) { request.setAttribute("info", e.getMessage()); } return mapping.findForward("save"); } public ActionForward showall(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { NewsForm AllNewsActionForm = (NewsForm) form; try { Service actionNews = new Service(AllNewsActionForm); List<String[]> result = actionNews.search((NewsForm)form); if (result.size() > 0) { request.setAttribute("result", result); request.setAttribute("info_all", "总记录数:" + String.valueOf(result.size())); } else request.setAttribute("info_all", "没有符合要求的记录!"); } catch (Exception e) { request.setAttribute("info_all", e.getMessage()); } return mapping.findForward("searchAll"); }
public class Service { public NewsForm form; public Service(NewsForm form) throws Exception { super(); this.form=form; } public List<String[]> search(NewsForm form) throws Exception { try{ List<String[]> result = new LinkedList<String[]>(); String sql = "select * from s_news"; SQLBean Bean=new SQLBean(); Bean.searchall(sql,result,form); Bean.close(); return result; } catch (Exception e) { throw new Exception(e.getMessage()); } public void save(NewsForm form) throws Exception { //封装到JavaBean对象中 try{ String news_theme=form.getNews_theme(); String news_author=form.getNews_author(); String news_date=form.getNews_date(); String news_detail=form.getNews_detail(); String news_url=form.getNews_url(); NewsForm bean=new NewsForm(); bean.setNews_theme(news_theme); bean.setNews_author(news_author); bean.setNews_date(news_date); bean.setNews_detail(news_detail); bean.setNews_url(news_url); String sql="insert into s_news(news_theme,news_author,news_date,news_detail,news_url)values(?,?,?,?,?)"; //调用模型层 SQLBean Bean=new SQLBean(); Bean.add(sql,form); Bean.close(); } catch (Exception e) { throw new Exception(e.getMessage()); } } }
public class SQLBean{ public NewsForm form; public Connection conn; public PreparedStatement pstat; public ResultSet rs; public Statement stat; public DataSource ds;public void add(String sql,NewsForm form) throws Exception{ try { this.conn = ds.getConnection(); pstat=conn.prepareStatement(sql); pstat.setString(1, form.getNews_theme()); pstat.setString(2, form.getNews_author()); pstat.setString(3, form.getNews_date()); pstat.setString(4, form.getNews_detail()); pstat.setString(5, form.getNews_url()); pstat.executeUpdate(); conn.commit(); } catch (Exception e) { // TODO 自动生成 catch 块 throw new Exception(e.getMessage()); } }public List<String[]> searchall(String sql,List<String[]> result,NewsForm form) throws Exception{ try{ this.conn = ds.getConnection(); pstat=conn.prepareStatement(sql); ResultSet rs=pstat.executeQuery(); conn.commit(); while(rs.next()) { String[] row = new String[6]; row[0] = rs.getString(1); row[1] = rs.getString(2); row[2] = rs.getString(3); row[3] = rs.getString(4); row[4] = rs.getString(5); row[5] = rs.getString(6); result.add(row); } return result; } catch (Exception e) { // TODO 自动生成 catch 块 throw new Exception(e.getMessage()); } }
HttpServletRequest request, HttpServletResponse response)
throws IOException {
DynaActionForm memberForm = (DynaActionForm) form;
try{
Member member=new Member();
member.setUname(memberForm.getString("uname"));
member.setPassword(memberForm.getString("password"));
member.setEmail(memberForm.getString("email"));
member.setMemberregdate(new Date());
member.setConsumeramount(Float.parseFloat("0"));
member.setAccountbalance(Float.parseFloat("0"));
member.setMembergrade("普通");
memberService.save(member);
}catch(Exception e){
e.printStackTrace();
return mapping.findForward("RegisterFailure");
}
return mapping.findForward("RegisterSuccess");
}
struts1-config.xml配置:
<struts-config>
<data-sources />
<form-beans >
<form-bean name="memberForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="password" type="java.lang.String" />
<form-property name="phone" type="java.lang.String" />
<form-property name="documentnumber" type="java.lang.String" />
<form-property name="address" type="java.lang.String" />
<form-property name="remark" type="java.lang.String" />
<form-property name="memberimage" type="java.lang.String" />
<form-property name="uname" type="java.lang.String" />
<form-property name="sex" type="java.lang.String" />
<form-property name="age" type="java.lang.Integer" />
<form-property name="truename" type="java.lang.String" />
<form-property name="postcode" type="java.lang.String" />
<form-property name="email" type="java.lang.String" />
<form-property name="birthday" type="java.lang.String" />
<form-property name="validatenumber" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<!-- 用户操作 -->
<action name="memberForm" parameter="param" path="/member" scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="RegisterSuccess" path="/index.jsp" />
<forward name="RegisterFailure" path="/JspPage/memberpage/register.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.cloth.struts.ApplicationResources" />
<!-- 模板框架 -->
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
</plug-in>
</struts-config>
service配置文件:
<!-- 会员表 -->
<bean id="memberService" class="com.cloth.service.MemberServiceImpl">
<property name="memberDAO" ref="memberDAO" />
</bean>
dao配置文件:
<!-- 会员表 -->
<bean id="memberDAO" class="com.cloth.dao.MemberDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
action配置文件:
<!-- 用户操作 -->
<bean name="/member" class="com.cloth.action.MemberAction">
<property name="memberService" ref="memberService" />
</bean>
jsp页面请求:
<form action="<%=request.getContextPath()%>/member.do?param=memberregister" id="regform" method="post" onsubmit="return allvalidate()" >
......................
</form>