求助!!!关于struts1.2分层,代码已经写好了!如题,帮忙把这个类分层,就是把逻辑和业务分开写,再写个类。帮帮忙,刚学框架,刚了解mvc~~~~ 还有action代码也帮忙改一改就是这个,分一下层,不会分啊啊啊啊
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的
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");
}

解决方案 »

  1.   

    首先要先确定三层mvc的含义是什么 大概看了一下  你第一个类是一个底层的m  也就是说 是一个访问数据库的模型而已你缺少的是一个C  控制...  因为你的功能很小. 所以控制体现不出来.基本上你这个如果分的话就是中间加一个类 进行二次传递而已. 控制逻辑在比较大的项目里边是很复杂的.  
      

  2.   

    好吧,没人帮我,自己写出来了一个增加,但是插入数据又出错了。求解!!!
    控制层代码:
    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(ds);
    request.setAttribute("info", "保存成功!");
    }
    } catch (Exception e) {
    request.setAttribute("info", e.getMessage());
    }
    return mapping.findForward("save");
    }
    逻辑层代码:
    public void save(DataSource ds) throws Exception {
    //封装到JavaBean对象中
    try{
    NewsForm bean=new NewsForm();
    bean.setNews_theme(form.getNews_theme());//action获取的表单数据
    bean.setNews_author(form.getNews_author());
    bean.setNews_date(form.getNews_date());
    bean.setNews_detail(form.getNews_detail());
    bean.setNews_url(form.getNews_url());
    String sql="insert into s_news(news_theme,news_author,news_date,news_detail,news_url)values(?,?,?,?,?)";
    //调用业务层
    StrutsBean Bean=new StrutsBean(ds);
    Bean.add(sql,bean);
    } catch (Exception e) 
    {
    throw new Exception(e.getMessage());
    }
    }
    这是业务层代码:
    public StrutsBean(DataSource ds) throws SQLException
    {
    this.conn = ds.getConnection();
    }
    public void add(String sql,NewsForm form){
    try {
    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(sql); 
    } catch (SQLException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    }然后提交就会报错。
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?,?)' at line 1
    求修改!~~~