求助!!!关于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");
}
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");
}
解决方案 »
- jre7 jasperreport 打印字符串的时候重叠
- 在linux中如何设置crontab来定时运行servlet
- struts分发action与普通的Action的性能哪个好?
- 请教大虾们 怎么在 JSP 页面上点一个按钮 把页面的报表生成 EXCEL 文件,谢谢.在线等.急~~~
- 一个小虾的疑惑,,,麻烦大虾们帮忙顶!!!!!!!
- 大家帮我一下
- 关于路径怎么不对了?
- 高分求大虾相助!!一个看上去并不难的问题--数据库异常的处理问题。成功解决分全送,请大虾进来说话,不胜感激!!
- 接收中文参数,字符转换出错
- 请问如何在JSP中已存在的TABLE里显示SELECT出来的数据?谢谢!
- jsf绑定问题
- 有没有用dtree的啊?问几个问题
控制层代码:
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求修改!~~~