|
求助!!!关于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");
} |
|