当前位置:网站首页>Custom MVC principle
Custom MVC principle
2022-07-25 23:06:00 【A Nara Senyu】
Catalog
One : Customize mvc Working principle diagram of the frame :
3、 ... and . Concrete realization :
3. Realization Action Interface definition of :
4. Implement sub controllers :
5. Request distribution function
7.1 Use profile configuration action
One : Customize mvc Working principle diagram of the frame :

ActionSevlet: The central controller ( It is mainly used to control one or more sub controllers Action To deal with requests )
Core component description :
- The central controller (ActionServlet): Responsible for receiving all requests , And give specific treatment to the controller .
- Sub controller (Action): Responsible for processing requests allocated by the central processing unit
- View (view): jsp page , Responsible for the display
- Model (Model): Responsible for business processing logic
Two . What is? MVC:
MVC The full name is Model View Controller, It's a model (model)- View (view)- controller (controller) Abbreviation . It's a software design paradigm .
Using a business logic 、 data 、 The interface displays the separated method organization code , Gather business logic into a component , While improving and personalizing the interface and user interaction , No need to rewrite business logic .
Improved program maintainability 、 Portability 、 Scalability and reusability , It reduces the difficulty of program development . It is mainly divided into models 、 View 、 Controller layer 3 .
- Model (model): It is the main part of the application , It mainly includes business logic module (web In the project dao class ) And data module (pojo class ).pojo Generally, it can be called entity domain model ,dao and service It is called process domain model .
- View (view): The interface with which the user interacts 、 stay web The middle view is generally composed of jsp,html form , Others include android,ios wait .
- controller (controller): Receive requests from the interface And give it to the model for processing In this process, the controller does not do any processing, but plays a connecting role .
What's not enough :
- Increase the complexity of system structure and implementation . For a simple interface , Strictly observe MVC, Need to make the model 、 View separate from controller , Increase system complexity
- The relationship between view and controller is too close
3、 ... and . Concrete realization :
1. Writing CPU servlet
/** * Each sub controller must implement this interface , Responsible for processing requests allocated by the central processing unit * @author Administrator */ public interface Action { /** * Processing requests * @param request request * @param response Respond to * @return String Return forwarded or redirected jsp Page name */ String exeute(HttpServletRequest request, HttpServletResponse response); }2.Action Interface definition
/** * Each sub controller must implement this interface , Responsible for processing requests allocated by the central processing unit * @author Administrator */ public interface Action { /** * Processing requests * @param request request * @param response Respond to * @return String Return forwarded or redirected jsp Page name */ String exeute(HttpServletRequest request, HttpServletResponse response); }/** * Each sub controller must implement this interface , Responsible for processing requests allocated by the central processing unit * @author Administrator */ public interface Action { /** * Processing requests * @param request request * @param response Respond to * @return String Return forwarded or redirected jsp Page name */ String exeute(HttpServletRequest request, HttpServletResponse response); }3. Realization Action Interface definition of :
/** * Each sub controller must implement this interface , Responsible for processing requests allocated by the central processing unit * @author Administrator */ public interface Action { /** * Processing requests * @param request request * @param response Respond to * @return String Return forwarded or redirected jsp Page name */ String exeute(HttpServletRequest request, HttpServletResponse response); }4. Implement sub controllers :
public class BookAction implements Action { @Override public String exeute(HttpServletRequest request, HttpServletResponse response) { return "bookList"; } }public class StudentAction implements Action { @Override public String exeute(HttpServletRequest request, HttpServletResponse response) { // TODO Auto-generated method stub return "students"; } }5. Request distribution function
** * The central controller , Be responsible for receiving all requests and giving them to the controller for specific processing * @author Administrator */ @WebServlet("*.action") public class ActionDispatchServlet extends HttpServlet { // Used to hold path And action Mapping of sub controllers public static Map<String, Action> actionMap = new HashMap<>(); static { actionMap.put("/studentAction", new StudentAction()); actionMap.put("/bookAction", new BookAction()); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) { doPost(request, response); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) { String servletPath = request.getServletPath(); String path = servletPath.split("\\.")[0]; Action action = actionMap.get(path); String rpath = action.exeute(request, response); System.out.println(rpath); } }6. Running results :
7. Code perfect :
7.1 Use profile configuration action
studentAction Is defined above , As shown in the figure :
type It's a brand new name , As shown in the figure :、
name yes jsp Name :
hinder path Namely jsp Permission naming for
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE config[ <!ELEMENT config (action*)> <!ELEMENT action (forward*)> <!ELEMENT forward EMPTY> <!ATTLIST action path CDATA #REQUIRED type CDATA #REQUIRED > <!ATTLIST forward name CDATA #REQUIRED path CDATA #REQUIRED redirect (true|false) "false" > ]> <config> <action path="/studentAction" type="org.lisen.mvc.action.StudentAction"> <forward name="students" path="/students/studentList.jsp" redirect="false"/> </action> </config>7.2. At night, CPU :
@WebServlet("*.action") public class ActionDispatchServlet extends HttpServlet { // Used to hold path And action Mapping of sub controllers //public static Map<String, Action> actionMap = new HashMap<>(); private static ConfigModel configModel; static { //actionMap.put("/students", new StudentAction()); //actionMap.put("/books", new BookAction()); configModel = ConfigModelFactory.getConfigModel(); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doPost(request, response); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String servletPath = request.getServletPath(); String path = servletPath.split("\\.")[0]; Action action = getActionByPath(path); String name = action.exeute(request, response); ForwardModel forwardModel = getForwardModel(path, name); if (forwardModel.isRedirect()) { response.sendRedirect(request.getContextPath() + "/" + forwardModel.getPath()); } else { request.getRequestDispatcher(forwardModel.getPath()).forward(request, response); } } // Get the corresponding... Through the request path action example private Action getActionByPath(final String path) { ActionModel action = configModel.find(path); try { Class<?> clazz = Class.forName(action.getType()); return (Action)clazz.newInstance(); } catch (Exception e) { throw new RuntimeException(" establish Action Instance exception "+e.getMessage(), e); } } public ForwardModel getForwardModel(String path, String name) { return configModel.find(path).find(name); } }
边栏推荐
猜你喜欢

Opencv compile and call GPU version

Take root downward, grow upward, and explore the "root" power of Huawei cloud AI

How painful is it to write unit tests?

Basic knowledge of radar

Network Security Learning (16)

【论文笔记】A Meta-Reinforcement Learning Algorithm for Causal Discovery

The third experiment OSPF

Mongodb的特点、与MySQL的差别、以及应用场景

Session and cookie, token and storage
![[natural language processing] [vector representation] augsbert: improve the data enhancement method of Bi encoders for paired sentence scoring tasks](/img/9a/9bb00abf7804d61d3408143e5e4bda.png)
[natural language processing] [vector representation] augsbert: improve the data enhancement method of Bi encoders for paired sentence scoring tasks
随机推荐
CTS测试方法「建议收藏」
通用分页功能
Expression of directional signal -- complex exponential signal
Shanghai Second Polytechnic University - Health Daily autocheck
uvm_ HDL -- implementation of DPI in UVM (4)
2021-09-30
[PTA] 7-24 minimum fraction (15 points)
Network Security Learning (XV) ARP
Kibana~ the process number cannot be found after kibana is started in the background
码蹄集 精准弹幕
Similarities and differences between equals and "= ="
The difference between "= =" and equals
r语言绘图参数(R语言plot画图)
[文献阅读] - HRL -[HRL with Universal Policies for Multi-Step Robotic Manipulation]
Ip--- ia review
Common software shortcuts
Check code generation
栈与Stack类
Flight control implementation of four rotor aircraft "suggestions collection"
Network Security Learning (11) scanning and blasting


