Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of MVC Model layer Separation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points of MVC model layer separation case analysis, with detailed content and clear logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Advanced introduction of MVC

What is the model layer and what is it used for? The model layer can actually be understood as a kind of logic. Just like the problem-solving model, servlet passes the data to the model layer, and the model gives a result and returns it to servlet. Yes, the model layer can also be called the business logic layer (after learning database operations, the model layer is split, such as service layer and dao layer).

Actual combat

Review:

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException {String classname = "; String url ="; String element = ""; classname = request.getParameter ("class"); if (classname.equals ("qd")) {/ / currently only do front-end decisions, and then add url = "https://www.yisu.com/courses?direction=2120&tag=0&type=&condition=&order=";" later. Element = "front-end microclass";} request.setAttribute ("url", url); request.setAttribute ("element", element); RequestDispatcher view = request.getRequestDispatcher ("result.jsp"); try {view.forward (request,response);} catch (ServletException e) {e.printStackTrace ();}}

There is a part of the code here to determine the request parameters, although this part of the code is not much (because it is not fully written), but it is still necessary to split it from the servlet. Next, let's create a new special java class (by the way, write the complete logic to show the difference) to deal with this part of the logic:

Public class model {String url = ""; String element = ""; public void select (String classname) {if (classname.equals ("qd")) {/ / currently only make front-end decisions, and then add url = "https://www.yisu.com/courses?direction=2120&tag=0&type=&condition=&order="; element =" front-end micro-lessons at a later stage. } else if (classname.equals ("hd")) {url = "https://www.yisu.com/courses?direction=2126&tag=0&type=&condition=&order="; element=";} else if (classname.equals ("db")) {url = "https://www.yisu.com/courses?direction=2126&tag=0&type=&condition=&order="; element=" } else if (classname.equals ("cp")) {url = "https://www.yisu.com/courses?direction=2126&tag=0&type=&condition=&order="; element=";} else if (classname.equals ("Android")) {url = "https://www.yisu.com/courses?direction=2126&tag=0&type=&condition=&order="; element=" } else if (classname.equals ("other")) {url = "https://www.yisu.com/courses?direction=2126&tag=0&type=&condition=&order="; element=" backend microlessons;}} public String getUrl () {return url;} public String getElement () {return element;}}

Then modify the servlet code (only show the doGet () method):

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException {String classname = "; classname = request.getParameter (" class "); model select = new model (); select.select (classname); request.setAttribute (" url ", select.url); request.setAttribute (" element ", select.element); RequestDispatcher view = request.getRequestDispatcher (" result.jsp ") Try {view.forward (request,response);} catch (ServletException e) {e.printStackTrace ();}}

As shown in the above code, we split the business logic code into a special model class, and then implement the business logic by calling the method of the model class, so that the business logic is split into the model class. Servlet only needs to know what model method to call when the request arrives, and then forward the processed data returned by model to JSP for view output. The remaining function of servlet, which strips the business logic code and the view presentation code, is as the connection between the view and the model, so it is also called the controller (vividly, servlet controls the invocation of the business logic and the presentation code).

These are all the contents of the article "MVC Model layer Separation case Analysis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report