日期:2014-05-17  浏览次数:20704 次

简单的MVC模式struts1.2添加hibernate3的问题!!!!
[color=#FF0000]程序本来是单独的struts1.2,在struts.config.xml里配置数据源操作数据库的,现在添加了hibernate3作为持久层来操作数据库。hibernate的配置文件已经配置好了。<session-factory> 和C3P0连接池也弄好了。数据库表的xml文件也创建好了,映射文件也注册了。 相应的DAO接口,实现类,都写了。


但是ActionForward不会改了~~~具体代码该怎么写。。。注意是struts1.2的,给几个例子吧。 我把我原来的代码发一下,你们看一下怎么改,哪里需要改。



原ActionForward类:
Java code
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");
    }




原Service类:
Java code
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=n