标签:图标 ecb set 文件 swa 调试 request rect this
MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种业务逻辑、数据与界面显示分离的方法来组织代码,将众多的业务逻辑聚集到一个部件里面,在需要改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,达到减少编码的时间。
MVC开始是存在于桌面程序中的,M是指业务模型,V是指用户界面,C则是控制器。
使用的MVC的目的:在于将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。比如Windows系统资源管理器文件夹内容的显示方式,下面两张图中左边为详细信息显示方式,右边为中等图标显示方式,文件的内容并没有改变,改变的是显示的方式。不管用户使用何种类型的显示方式,文件的内容并没有改变,达到M和V分离的目的。
在网页当中,
V即View视图是指用户看到并与之交互的界面。比如由html元素组成的网页界面,或者软件的客户端界面。MVC的好处之一在于它能为应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,它只是作为一种输出数据并允许用户操纵的方式。
M即model模型是指模型表示业务规则。在MVC的三个部件中,模型拥有最多的处理任务。被模型返回的数据是中立的,模型与数据格式无关,这样一个模型能为多个视图提供数据,由于应用于模型的代码只需写一次就可以被多个视图重用,所以减少了代码的重复性。
C即controller控制器是指控制器接受用户的输入并调用模型和视图去完成用户的需求,控制器本身不输出任何东西和做任何处理。它只是接收请求并决定调用哪个模型构件去处理请求,然后再确定用哪个视图来显示返回的数据。
下图说明了三者之间的调用关系。
1.耦合性低
视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样,一个应用的业务流程或者业务规则的改变只需要改动MVC的模型层即可。因为模型与控制器和视图相分离,所以很容易改变应用程序的数据层和业务规则。
2.重用性高
MVC模式允许使用各种不同样式的视图来访问同一个服务器端的代码,因为多个视图能共享一个模型,它包括任何WEB(HTTP)浏览器或者无线浏览器(wap),比如,用户可以通过电脑也可通过手机来订购某样产品,虽然订购的方式不一样,但处理订购产品的方式是一样的。由于模型返回的数据没有进行格式化,所以同样的构件能被不同的界面使用。
3.部署快,生命周期成本低
MVC使开发和维护用户接口的技术含量降低。使用MVC模式使开发时间得到相当大的缩减,它使程序员(Java开发人员)集中精力于业务逻辑,界面程序员(HTML和JSP开发人员)集中精力于表现形式上。
4.可维护性高
分离视图层和业务逻辑层也使得WEB应用更易于维护和修改。
1.完全理解MVC比较复杂。
由于MVC模式提出的时间不长,加上同学们的实践经验不足,所以完全理解并掌握MVC不是一个很容易的过程。
2.调试困难。
因为模型和视图要严格的分离,这样也给调试应用程序带来了一定的困难,每个构件在使用之前都需要经过彻底的测试。
3.不适合小型,中等规模的应用程序
在一个中小型的应用程序中,强制性的使用MVC进行开发,往往会花费大量时间,并且不能体现MVC的优势,同时会使开发变得繁琐。
4.增加系统结构和实现的复杂性
对于简单的界面,严格遵循MVC,使模型、视图与控制器分离,会增加结构的复杂性,并可能产生过多的更新操作,降低运行效率。
5.视图与控制器间的过于紧密的连接并且降低了视图对模型数据的访问
视图与控制器是相互分离,但却是联系紧密的部件,视图没有控制器的存在,其应用是很有限的,反之亦然,这样就妨碍了他们的独立重用。
依据模型操作接口的不同,视图可能需要多次调用才能获得足够的显示数据。对未变化数据的不必要的频繁访问,也将损害操作性能。
用户及新闻
package bean; public class News { String title; String datetime; String content; String type; int id; public News(String title,String datetime,String content,String type,int id){ super(); this.title = title; this.datetime = datetime; this.content = content; this.type = type; this.id = id; } public int getId(){ return id; } public void setId(int id){ this.id = id; } public News(){ } public String getTitle(){ return title; } public void setTitle(String title){ this.title = title; } public String getDatetime(){ return datetime; } public void setDatetime(String datetime){ this.datetime = datetime; } public String getContent(){ return content; } public void setContent(String content){ this.content = content; } public String getType(){ return type; } public void setType(String type){ this.type = type; } public News(String title,String datetime,String content,String type){ super(); this.title = title; this.datetime = datetime; this.content = content; this.type = type; } }
package bean; public class User { String name; String password; public User(){ } public String getName(){ return name; } public void setName(String name){ this.name = name; } public String getPassword(){ return password; } public void setPassword(String password){ this.password = password; } public User(String name,String password){ super(); this.name = name; this.password = password; } }
package com.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import bean.News; public class Newsdao { @SuppressWarnings("unused") private static final long serialVersionUID = 1L; static Connection con = null; public static Connection getCon(){ String driverName = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/newsdata?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"; String name = "root"; String pwd = "123456"; try{ Class.forName(driverName); try{ con = DriverManager.getConnection(url,name,pwd); }catch(SQLException e){ e.printStackTrace(); } }catch(ClassNotFoundException e){ e.printStackTrace(); } return con; } //添加新闻 public boolean addNews(News news){ con = Newsdao.getCon(); boolean flag = false; String sql = "insert news(title,type,datetime,content) values(‘"+news.getTitle()+"‘,‘"+news.getType()+"‘,‘"+news.getDatetime()+"‘,‘"+news.getContent()+"‘)"; try{ PreparedStatement ps = con.prepareStatement(sql); int i = ps.executeUpdate(); ps.close(); con.close(); if(i>0){ flag = true; } }catch(SQLException e){ System.out.println("添加出错!"); e.printStackTrace(); } return flag; } public List<News> selectByID(int id){ con = Newsdao.getCon(); List<News> list = new ArrayList<>(); String sql = "select * from news where id="+id+""; System.out.println(sql); Statement state; ResultSet rs; try{ state = con.createStatement(); rs = state.executeQuery(sql); while(rs.next()){ News news = new News(); news.setTitle(rs.getString("title")); news.setDatetime(rs.getString("content")); news.setContent(rs.getString("content")); news.setType(rs.getString("type")); news.setId(id); list.add(news); } rs.close(); state.close(); con.close(); }catch(SQLException e){ System.out.println("查询出错!"); e.printStackTrace(); } return list; } //删除新闻 public List<News> selectAll(){ con = Newsdao.getCon(); List<News> list = new ArrayList<News>(); String sql = "select * from news"; Statement state; ResultSet rs; try{ state = con.createStatement(); rs = state.executeQuery(sql); while(rs.next()){ News news = new News(); news.setId(rs.getInt("id")); news.setTitle(rs.getString("title")); news.setDatetime(rs.getString("datetime")); news.setContent(rs.getString("content")); news.setType(rs.getString("type")); list.add(news); } rs.close(); state.close(); con.close(); }catch(SQLException e){ System.out.println("查询出错!"); e.printStackTrace(); } return list; } //修改新闻 public boolean update(News news){ con = Newsdao.getCon(); boolean flag = false; String sql = "update news set title=‘"+news.getTitle()+"‘,type=‘"+news.getType()+"‘,datetime=‘"+news.getDatetime()+"‘,content=‘"+news.getContent()+"‘ where id = "+news.getId()+""; System.out.println(sql); Statement state = null; try{ state = con.createStatement(); int i=state.executeUpdate(sql); System.out.println(i); if(i>0){ flag = true; } }catch(SQLException e){ System.out.println("更新错误!"); e.printStackTrace(); } return flag; } //删除新闻 public boolean delete(int id){ con = Newsdao.getCon(); boolean flag = false; String sql = "delete from news where id=‘"+id+"‘"; PreparedStatement ps; try{ ps = con.prepareStatement(sql); int i = ps.executeUpdate(); if(i>0){ flag = true; } }catch(SQLException e){ System.out.println("删除错误!"); e.printStackTrace(); } return flag; } }
package com.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import bean.User; public class Userdao { private static final long serialVersionUID = 1L; private String driverName="com.mysql.cj.jdbc.Driver"; private String url="jdbc:mysql://localhost:3306/newsdata?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"; private String name="root"; private String pwd="123456"; public boolean login(User u) { boolean flag=false; String sql="select * from user where uname=‘"+u.getName()+"‘ and password=‘"+u.getPassword()+"‘"; System.out.println(sql); Connection connection=null; Statement statement=null; ResultSet resultSet=null; try { //System.out.println("******************"); Class.forName(driverName); try { //System.out.println("##################3"); connection = DriverManager.getConnection(url, name, pwd); //System.out.println("******************"); statement = connection.createStatement(); resultSet = statement.executeQuery(sql); flag=resultSet.next(); resultSet.close(); statement.close(); connection.close(); //System.out.println(flag+"**********"); } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } return flag; } }
增
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.Newsdao; import bean.News; @WebServlet("/AddServlet") public class AddServlet extends HttpServlet { public AddServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); String title=request.getParameter("title"); String type=request.getParameter("type"); String datetime=request.getParameter("datetime"); String content=request.getParameter("ueditor"); System.out.println("这是ueditor的内容"+title+content); News news=new News(); news.setTitle(title); news.setContent(content); news.setDatetime(datetime); news.setType(type); Newsdao nd=new Newsdao(); try{ nd.addNews(news); response.sendRedirect("newslistServlet"); //request.getRequestDispatcher("/newslist.jsp").forward(request, response); }catch(Exception e) { System.out.println("添加失败"); } } }
删
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.Newsdao; @WebServlet("/DeleteServlet") public class DeleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; public DeleteServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String id=(String)request.getParameter("id"); System.out.println("被删除的新闻"); int nid=Integer.parseInt(id); Newsdao nd=new Newsdao(); nd.delete(nid); response.sendRedirect("newslistServlet"); } }
改
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.Newsdao; import bean.News; @WebServlet("/UpdateServlet") public class UpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; public UpdateServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("utf-8"); String id=request.getParameter("id"); System.out.println("id的值为"+id); int nid=Integer.parseInt(id); String title=request.getParameter("title"); String type=request.getParameter("type"); String datetime=request.getParameter("datetime"); String content=request.getParameter("editor"); News news=new News(); news.setId(nid); news.setTitle(title); news.setContent(content); news.setDatetime(datetime); news.setType(type); Newsdao nd=new Newsdao(); try{ nd.update(news); response.sendRedirect("newslistServlet"); }catch(Exception e) { System.out.println("修改失败"); } } }
查
package servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.Newsdao; import bean.News; @WebServlet("/SelectServlet") public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; public SelectServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;cahrset=utf-8"); String id=request.getParameter("id"); int nid=Integer.parseInt(id); Newsdao nd=new Newsdao(); List<News> list=nd.selectByID(nid); request.setAttribute("news", list); System.out.println(list.size()); request.getRequestDispatcher("/content.jsp").forward(request, response); } }
登录
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.Userdao; import bean.User; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { public LoginServlet() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean flag=false; String username = request.getParameter("username"); String password = request.getParameter("password"); response.setContentType("text/html;charset=UTF-8"); Userdao ud=new Userdao(); User u=new User(); u.setName(username); u.setPassword(password); flag=ud.login(u); System.out.println(flag); if(flag) { response.sendRedirect("newslistServlet"); }else { System.out.println("登录错误"); response.sendRedirect("/newsManage/login.jsp"); } } }
链接:https://pan.baidu.com/s/1dsZcYp_Gv2EfMDGm0GCRmw
提取码:rdtu
复制这段内容后打开百度网盘手机App,操作更方便哦
标签:图标 ecb set 文件 swa 调试 request rect this
原文地址:https://www.cnblogs.com/June-6th/p/10885835.html