java web-----MVC设计模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java web-----MVC设计模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4617字,纯文字阅读大概需要7分钟。
内容图文
一,MVC将代码分为三个部分,分别为视图(jsp),模型(javaBean),控制部分(servlet);
视图基本为 jsp 文件,主要内容为界面的html代码,负责显示界面;
模型为 javaBean ,负责与数据库交互;
控制部分为 servlet 充当,负责处理业务逻辑与页面切换。
二,MVC包括 Model1 和 Model2 两个模型;
1. Model1 模型程序流程如下图:
Model1 中界面显示部分与界面跳转,业务逻辑都由 jsp 负责,导致 jsp 中充满大量java脚本代码,
代码重复率高,可用性低,程序功能的微小的修改往往引起大量的修改,优点是容易掌控。
2. Model2 模型程序流程如下图:
Model2 模型中分层更加明显,jsp 负责页面显示,javaBean负责数据库操作与业务逻辑,servlet
则是核心的调度部分,负责调用 javaBean 来处理业务逻辑,同时负责界面调度切换。优点是程序
执行更加清晰,方便后期功能的添加与修改。
三,基于MVC模型的用户登陆实例:
1.界面部分(login.jsp登陆界面,login_success.jsp登陆成功界面,login_failure.jsp登录失败界面)
login.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用户登录</title> </head> <body> <form action="../loginConf" method="post"> <center> <table> <tr> <td>用户名:</td> <td><input type="text" name="uname" /></td> </tr> <tr> <td>密 码:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交" /> <input type="reset" value="重置" /></td> </tr> </table> </center> </form> </body> </html>
效果图:
login_success.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登陆成功</title> </head> <body> <% if(session.getAttribute("login")!=null&&session.getAttribute("login").equals("true")) { %> <center> <h2>登陆成功</h2> </center> <% } else { %> <jsp:forward page="login.jsp"></jsp:forward> <% } %> </body> </html>
效果图:
login_failure.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录失败</title>
</head>
<body>
<center>
<h2>登录失败</h2>
</center>
</body>
</html>
效果图:
2.servlet(controller)
loginConf.java
package com.javaweb.mvc; 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 javax.servlet.http.HttpSession; /** * Servlet implementation class loginConf */ @WebServlet("/loginConf") publicclass loginConf extends HttpServlet { privatestaticfinallong serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */public loginConf() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //设置编码格式 response.setContentType("text/html; charset=utf-8"); request.setCharacterEncoding("utf-8"); //获取参数 String uname = request.getParameter("uname"); String password = request.getParameter("password"); //获取session HttpSession session = request.getSession(); //业务逻辑判断 loginCheck lc = new loginCheck(); if(lc.isLogin(uname, password)) { session.setAttribute("login", "true"); request.getRequestDispatcher("MVC/login_success.jsp").forward(request, response); } else { request.getRequestDispatcher("MVC/login_failure.jsp").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
3.javaBean(业务逻辑Model)
package com.javaweb.mvc; public class loginCheck { public boolean isLogin(String uname,String password) { if(uname.equals("wei")&&password.equals("123")) { returntrue; } else { returnfalse; } } }
原文:http://www.cnblogs.com/weiyikang/p/5638271.html
内容总结
以上是互联网集市为您收集整理的java web-----MVC设计模式全部内容,希望文章能够帮你解决java web-----MVC设计模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。