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

求助!!!关于struts1.2分层,代码已经写好了!
求助!!!关于struts1.2分层,代码已经写好了!

如题,帮忙把这个类分层,就是把逻辑和业务分开写,再写个类。帮帮忙,刚学框架,刚了解mvc。。。~~~~ 还有action代码也帮忙改一改。。。



就是这个,分一下层,不会分啊啊啊啊
Java code
ackage actionform;

import java.util.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;

public class ActionNews {
    public NewsForm form;
    public Connection conn;

    public ActionNews(NewsForm form, DataSource ds) throws Exception {
        super();
        this.form = form;
        this.conn = ds.getConnection();
    }public void save() throws Exception {
        try {
            String newstheme = form.getNews_theme();
            String newsauthor = form.getNews_author();
            String newsdate = form.getNews_date();
            String newsdetail = form.getNews_detail();
            String newsurl = form.getNews_url();
            String sql = "insert into s_news(news_theme,news_author,news_date,news_detail,news_url) values('"
                    + newstheme
                    + "','"
                    + newsauthor
                    + "','"
                    + newsdate
                    + "','"
                    + newsdetail + "','" + newsurl + "')";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.executeUpdate(sql);
            pstmt.close();
            conn.commit();
            conn.close();
        } catch (Exception e) {
            conn.rollback();
            throw new Exception(e.getMessage());
        }
    }




这是action的
Java code
public class AllNewsAction extends DispatchAction

{
    public ActionForward addnews(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        NewsForm AllNewsActionForm = (NewsForm) form;
        DataSource ds = getDataSource(request, "struts");
        try {

            ActionNews actionNews = new ActionNews(AllNewsActionForm, ds);
            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();
                request.setAttribute("info", "保存成功!");
            }
        } catch (Exception e) {
            request.setAttribute("info", e.getMessage());
        }
        return mapping.findForward("save");
    }



------解决方案--------------------
首先要先确定三层mvc的含义是什么

 大概看了一下 你第一个类是一个底层的m 也就是说 是一个访问数据库的模型而已

你缺少的是一个C 控制... 因为你的功能很小. 所以控制体现不出来.

基本上你这个如果分的话就是中间加一个类 进行二次传递而已.

 控制逻辑在比较大的项目里边是很复杂的.


------解决方案--------------------