ssm简介:
ssm框架就是:spring+springmvc+mybits。
Spring:
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。SpringMvc:
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybits:
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
导入jar包
控制类代码
package com.hedong.controller; import java.io.IOException;import java.io.PrintWriter;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.List; import javax.annotation.Resource;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping; import com.hedong.pojo.News;import com.hedong.pojo.User;import com.hedong.service.INewsService;import com.hedong.service.IUserService; @Controller @RequestMapping("/news") public class operateController { @Resource private IUserService userService; @Resource private INewsService newsService; @RequestMapping("showNews") public String goPage(HttpServletRequest request,Model model){ //获取flag参数,判断是否是管理员进入页面 String flag=request.getParameter("flag"); Listnews=this.newsService.getAllNews(); model.addAttribute("news", news); if(flag!=null && flag.equals("admin")) { //若是管理员,则进入管理页面 return "ShowList.jsp"; }else{ //否则进入新闻首页 return "NewsCenter.jsp"; } } @RequestMapping("Login") public String Login() { return "Login.jsp"; } @RequestMapping("operateNews") public String operateNews(HttpServletRequest request) { //获取action参数,判断是显示增加新闻页面还是编辑新闻页面 String action=request.getParameter("action"); if(action.equals("add")) { return "operateNews.jsp?action=add"; }else{ return "operateNews.jsp?action=edit"; } } @RequestMapping("operate") public String OperateCenter(HttpServletRequest request,HttpServletResponse response,Model model) throws IOException, ParseException, ServletException { //获取参数 PrintWriter out=response.getWriter(); String action=request.getParameter("action"); String Account=request.getParameter("userName"); String Password=request.getParameter("passWord"); String myType=request.getParameter("newsType"); String myTitle=request.getParameter("newsTitle"); String myContent=request.getParameter("editorValue"); String writer=request.getParameter("newsWriter"); String myID=request.getParameter("newsID"); if(action.equals("login")) { User user=this.userService.getUserById(Account); if(user.getPassword().equals(Password)) { out.write("1"); }else if(Account==""||Password==""){ out.write("2"); }else { out.write("3"); } out.close(); return ""; }else if(action.equals("add")) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式 String time= df.format(new Date());// new Date()为获取当前系统时间 //生成新闻对象 News news=new News(); news.setNewstitle(myTitle); news.setNewstype(myType); news.setNewscontent(myContent); news.setNewswriter(writer); news.setNewstime(df.parse(time)); int result=this.newsService.insertNews(news);//调用服务层添加新闻 response.sendRedirect("showNews?flag=admin");//重新打开新闻管理页面 if(result>0) { System.out.println("add news success!"); } return ""; }else if(action.equals("delete")) { int result=this.newsService.deleteNews(Integer.parseInt(myID));//调用服务层删除新闻 response.sendRedirect("showNews?flag=admin");//重新打开新闻管理页面 if(result>0) { System.out.println("delete news success!"); } return ""; }else if(action.equals("edit")) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式 String time= df.format(new Date());// new Date()为获取当前系统时间 //生成新闻对象 News news=new News(); news.setNewsid(Integer.parseInt(myID));//因使用updateByPrimaryKeySelective()函数,修改必须携带id主键 news.setNewstitle(myTitle); news.setNewstype(myType); news.setNewscontent(myContent); news.setNewswriter(writer); news.setNewstime(df.parse(time)); int result=this.newsService.updateNews(news);//调用服务层修改新闻 response.sendRedirect("showNews?flag=admin");//重新打开新闻管理页面 if(result>0) { System.out.println("update news success!"); } return ""; }else if(action.equals("select")) { //获取参数flag,若为edit则将新闻信息发送到新闻编辑页面,若为check则发送带新闻详情页面 String flag=request.getParameter("flag"); News selectNew=this.newsService.selectNews(Integer.parseInt(myID));//调用服务层查询指定新闻 model.addAttribute("selectNew", selectNew); if(flag.equals("edit")) { return "operateNews.jsp?action=edit"; }else { return "NewsInfo.jsp"; } }else { return ""; } } }