日期:2014-05-17 浏览次数:20781 次
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"); }
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