MVC mode

zhaozj2021-02-16  126

MVC mode

MVC is the abbreviation of three words, which are: models, views (View), and Controller. The purpose of the MVC mode is to achieve the functional division of Web systems. The following is an introduction to J2EE development.

The MODEL layer realizes business logic in the system, usually can be implemented with JavaBean or EJB.

The View layer is used to interact with the user, usually implemented with JSP.

The Controller layer is a bridge that the Model communicates with the View, which can assign users to request and select the appropriate view for display, and it can also explain the user's input and map them to the model layer can be performed.

Now let's take an example to see how the MVC mode works.

1.1.1 an instance

Example 1-A:

controller

nepalon.simplestruts.controller

controller

/ simplestruts / servlet / control / controller

The above is a clip of the web.xml file, where a servlet is defined here to process the request.

Example 1-B (Test.jsp file):

<% @ Page ContentType = "text / html; charset = GB2312"%>

Sample Home </ TITLE></p> <p></ hEAD></p> <p><body></p> <p><Table Border = "0" width = "100%"></p> <p><tr></p> <p><TD> <DIV Align = "Center"></p> <p><a href="/simplestruts/servlet/control/controller?Command=showArticle"> Display Article </A></p> <p></ div> </ td></p> <p></ TR></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>In this JSP, we didn't call JSP or JavaBean directly, but split the request to the servlet. Below, let's take a look at the code of the servlet.</p> <p>Example 1-C:</p> <p>Package nepalon.simplesplestruts;</p> <p>/ **</p> <p>* <P> Title: MVC Framework </ P></p> <p>* <P> Description: Controller <p></p> <p>* <P> Copyright: R2003 </ P></p> <p>* @Author nepalon</p> <p>* @Version 1.0</p> <p>* /</p> <p>Import javax.servlet. *;</p> <p>Import javax.servlet.http. *; import java.io. *;</p> <p>Import java.util. *;</p> <p>Public Class Controller Extends Httpservlet</p> <p>{</p> <p>Public Void Init (ServletConfig Config) Throws ServleTexception</p> <p>{</p> <p>Super.init (config);</p> <p>}</p> <p>Public void destroy () {}</p> <p>/ ** Function for processing the GET and POST requests for HTTP</p> <p>* @Param Request Servlet Request</p> <p>* @Param Response Servlet Response</p> <p>* /</p> <p>Protected Void ProcessRequest (httpservletRequest Request, HttpservletResponse Response)</p> <p>THROWS servletexception, java.io.ioException</p> <p>{</p> <p>// Code (1) Implement the distribution of different requests through IF</p> <p>IF (Request.getParameter ("Command"). Equals ("ShowArticle")))</p> <p>{</p> <p>ArticleCommand command = new articlecommand ();</p> <p>Next = Command. getAllarticle (Request, Response);</p> <p>}</p> <p>// Code (2)</p> <p>Dispatch (Request, Response, Next);</p> <p>}</p> <p>Protected Void Doget (httpservletRequest Request,</p> <p>Httpservletresponse response</p> <p>THROWS servletexception, java.io.ioException</p> <p>{</p> <p>ProcessRequest (Request, Response);</p> <p>}</p> <p>Protected Void Dopost (httpservletRequest Request,</p> <p>Httpservletresponse response</p> <p>THROWS servletexception, java.io.ioException</p> <p>{</p> <p>ProcessRequest (Request, Response);</p> <p>}</p> <p>/ ** A function that implements the distributor mode</p> <p>* @Param Request Servlet Request</p> <p>* @Param Response Servlet Response</p> <p>* /</p> <p>Protected Void Dispatch (httpservletRequest Request,</p> <p>HTTPSERVLETRESPONSE RESPONSE,</p> <p>String Page)</p> <p>Throws javax.servlet.servletException, java.io.ioException</p> <p>{</p> <p>RequestDispatcher dispatcher =</p> <p>GetServletContext (). getRequestDispatcher (Page);</p> <p>Dispatcher.Forward (Request, Response);</p> <p>}</p> <p>}</p> <p>In the servlet, there is no direct processing request, but the processed process is pushed into the ArticleCommand class, executed by ArticleCommand object, such as code (1). After processing the request, go to the corresponding page, such as code (2). Below, let's take a look at the code of the ArticleCommand class.</p> <p>Example 1-D:</p> <p>Package nepalon.simplesplestruts;</p> <p>/ **</p> <p>* <P> Title: MVC Framework </ P></p> <p>* <P> Description: Article business class <p></p> <p>* <P> Copyright: R2003 </ P></p> <p>* @Author nepalon</p> <p>* @Version 1.0</p> <p>* /</p> <p>Import java.util. *;</p> <p>Import javax.servlet. *;</p> <p>Import java.io. *;</p> <p>Import java.lang. *;</p> <p>Import java.sql. *;</p> <p>Import javax.sql. *;</p> <p>Public Class Contribute</p> <p>{</p> <p>Public control () {}</p> <p>Public String getAllarticle (httpservletRequest request, httpservletResponse response)</p> <p>Throws javax.servlet.servletException, java.io.ioException</p> <p>{</p> <p>Connection conn = NULL;</p> <p>String con_user = "eXample1";</p> <p>String con_password = "eXample1";</p> <p>String con_dburl = "JDBC: Oracle: Thin: @localhost: iasdb";</p> <p>String con_driver = "Oracle.jdbc.driver.OracleDriver";</p> <p>PreparedStatement PSTMT = NULL;</p> <p>ResultSet RScomment = NULL;</p> <p>Vector vectorcomment = new vector ();</p> <p>String selectsql = "SELECT Content, Time from Article Order by Time DESC";</p> <p>Try</p> <p>{</p> <p>DriverManager.RegisterDriver (new oracle.jdbc.driver.OracleDriver ());</p> <p>Class.Forname (con_driver);</p> <p>Conn = drivermanager.getConnection (con_dburl, con_user, con_password);</p> <p>PSTMT = conn.preparestatement (SELECTSQL);</p> <p>RScomment = pstmt.executeQuery ();</p> <p>While (RScomment.next ())</p> <p>{</p> <p>CommentItem CommentItem = New CommentItem ();</p> <p>CommentItem.SetContent (RScomment.getstring (1));</p> <p>CommentItem.Settime (RScomment.getdate (2));</p> <p>Vectorcomment.add (CommentItem);</p> <p>}</p> <p>Vectorcomment.trimtosize ();</p> <p>}</p> <p>Catch (Exception E) {// Doing the corresponding processing}</p> <p>// Code (1) Save the processing result and return the jump page</p> <p>Request.setattribute ("Vectorcomment", Vectorcomment;</p> <p>Return "/simplesplestruts/showallarticle.jsp";</p> <p>}</p> <p>......</p> <p>Public String getNewarticle (httpservletRequest request, httpservletResponse response)</p> <p>THROWS JAVAX.SERVLET.SERVLETEXCEPTION, JAVA.IOEXCEPTION {...}</p> <p>}</p> <p>In this class, it is to obtain the business of all articles, and finally returns the page to jump to the page if the operation is successfully executed. Of course, there may be a corresponding function of other services in this class, readers can be implemented. Let's take a look at the code you want to jump to.</p> <p>Example 1-E (Showallarticle.jsp file):</p> <p><html></p> <p><% @ Page ContentType = "text / html; charset = GB2312"%></p> <p><% @ Page Import = "java.util. *, Java.lang. *"%></p> <p><jsp: usebean id = "vectorcomment" type = "java.util.vector" scope = "request" /></p> <p><HEAD></p> <p><meta http-equiv = "content-type" content = "text / html; charSet = GB2312"></p> <p><Title> Display Article </ Title></p> <p></ hEAD></p> <p><body></p> <p><Table Border = "0" width = "100%"></p> <p><tr></p> <p><TD> Published in </ TD></p> <p><TD> Article content </ td></p> <p></ TR></p> <p><%</p> <p>IF (Vectorcomment! = null && vectorcomment.size ()> 0)</p> <p>{</p> <p>Int counter = vectorcomment.size ();</p> <p>CommentItem CommentList = NULL;</p> <p>For (int i = 0; i <counter; i )</p> <p>{</p> <p>CommentList = NULL;</p> <p>CommentList = (CommentIth) (VectorComment.get (i));</p> <p>%></p> <p><tr></p> <p><TD> <% = commentList.getcmtime ()%> </ td></p> <p><TD> <% = CommentList.getCmContent ()%> </ td></p> <p></ TR></p> <p><%</p> <p>}</p> <p>}</p> <p>%></p> <p></ TABLE></p> <p></ body></p> <p></ html></p> <p>In this JSP, we have to do just the results and display, and there is no corresponding business logic.</p> <p>1.1.2 instance analysis</p> <p>First, let's take a look at the sequence diagram of this example.</p> <p>Figure 1.1.2-1</p> <p>1) First submit a request / SimpleStruts / Servlet / Control / Controller? Command = ShowArticle;</p> <p>2) In the controller object of the Controller layer, the corresponding service processing class is called according to the type of request. Here, the business handler of the COMMAND value is the service processing class for the ShowArticle request class, so the corresponding function of the object is called;</p> <p>3) In the MODEL layer's ArticleCommand class, the request to obtain the business function of all articles, save the results in the request, and return the jump page as the return value; 4) Back to the Controller layer of the Controller layer, according to the previous steps The return value is page forward.</p> <p>5) Forward to the showallarticle.jsp page of the View layer, this page acquires results from the request and display. Although there is also a JAVA code in this JSP, these codes are just used to display the results and do not involve any business logic.</p> <p>1.1.3 Benefits of MVC mode</p> <p>This simple feature we only need 2 JSP files to be implemented. Why should you be so troublesome to achieve it in several classes? Take a look at the benefits of MVC mode.</p> <p>1) Each is a matter, and it does not interfere.</p> <p>In the MVC mode, each of the three layers is applied, so if the demand of which layer has changed, only the code in the corresponding layer is needed without affecting the code in the other layer. If the business changes, if WebMaster may have invalid sign when you take a article, you can't take all articles, you can only take out a valid article, and the business changes. Imagine if this business logic is used in 100 pages, then the MVC mode reflects its flexibility. We can do not change any JSP, just change the SQL statement in the corresponding class in the Model layer.</p> <p>2) Help the division of labor in the development</p> <p>In MVC mode, the division of labor can be better implemented because of the lamination of the system. Web designers can develop JSP in the development view layer to develop business layers for business familiar developers, while other developers can develop control.</p> <p>3) Contribute to the reuse of components</p> <p>After the layering, it is more conducive to the reuse of the components. If the control layer can be used independently, the vision layer can also be a universal operating interface.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-8657.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="8657" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.056</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'rqGIuo4w925ElaLOKluLbYKd9F4cGd5aFP0RhUjikytHbUxO5hans71WWwCSKfVXsLe9Qxn9QoMzXRdyyV5Fsg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>