Struts2 technical insider Reading Note 3 Presentation Layer confusions

Source: Internet
Author: User
Tags expression engine

Struts2 technical insider Reading Note 3 Presentation Layer confusions
What are the doubts about the presentation layer? It's easy. If we forget all the frameworks for the moment, write a registered servlet.
Index. jsp

 

User. java
Public Class User {private String user; private Date birthday; public User () {}...... // omit get/set}

RegisterServlet. java
Package example; public class RegisterServlet extends HttpServlet {public void destroy () {super. destroy (); // Just puts destroy string in log // Put your code here} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding (UTF-8); // set the response format. setContentType (text/html); PrintWriter out = response. getWriter (); // obtain the String name = new String (request. getParameter (user ). getBytes (ISO8859_1), UTF-8); String birthday = new String (request. getParameter (birthday ). getBytes (ISO8859_1), UTF-8); Date date = null; // parameter type conversion try {date = new SimpleDateFormat (yyyy-MM-dd ). parse (birthday);} catch (ParseException e) {e. printStackTrace ();} User user = new User (); // equivalent to create user in step 2 of the MVC model diagram. setName (name); // equivalent to extract user in step 4 of the MVC model diagram. setBirthday (date); // The data in bean comes from the view UserService us = new UserService (); // The core business logic us. register (user); // return the processing result // equivalent to the third step forward request in the MVC model diagram. getRequestDispatcher (/success. jsp ). forward (request, response);} public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response );}}



Web. xml
     
  
   
Register
          
      
  
   
Example. RegisterServlet
      
    
   
                  
      
  
   
Register
  
      
  
   
/LoginServlet
  
    
 
We compare the previous MVC Elements
Model (Data Model) ---- User. java
View (external interaction) ----- registration. jsp
Control (program execution and Control) ----- RegistrationServlet. java
URL Mapping (request conversion) ---- web. xml
Is there a problem with the above Code?
No problem (except for the parameter part)
Can the above Code be used?
Not good.
Where is the problem?
There are two major disadvantages:
1. For the sake of program readability and maintainability, the program also needs to be restructured.
2. problems also exist in business expansion.
The problems mentioned above are both macro and General. Here are some specific questions.

1. When a browser sends an Http request, how does the Web Container receive the request and specify the corresponding java class to execute the business logic and return the processing result?
This is actually a question about URL Mapping.
If you are interested, read the following article.
How tomcat works Reading Notes 11 StandWrapper
Http://blog.csdn.net/dlf123321/article/details/41247693
The parameters are derived from web. xml.
In fact, how to find the corresponding java class is to put it bluntly that the servlet container will analyze the request url and the web. compare the uri-pattern under the servlet-mapping element in xml. If the uri-pattern is consistent, find the corresponding java class according to the servlet-name.
As for the java object generated based on uri, The digerter library is used in tomcat. For more information, see:
Http://blog.csdn.net/dlf123321/article/details/41802443
It's easy. But there is also a problem here, if a web. 3-5 servlets are written in xml, which is convenient for us. But the problem is that if the program execution and control in a system is composed of dozens or hundreds of servlets, they are all stored on the web. in xml, it is a disaster.
The policy to solve this problem is to establish a rule matching engine from uri to java class.

2. How to smoothly transfer data in web requests with browsers and the java World. Can we achieve automatic matching?
In the browser, the information we see is a string (weak type), but in java, there are different data types such as string, int, boolean (java is a strong type ). In the servlet above, we can see the conversion of the birthday attribute. It does not seem complicated, but how does one attribute mean 100? Is the attribute type no longer a basic type?
To solve this problem, the expression engine is used.

3. multi-threading of servlet.

This is a big problem. For details, see

Thread problems in tomcat

Http://blog.csdn.net/dlf123321/article/details/42222303



4. How can the Control layer, as the core controller of mvc, support function expansion to the maximum extent.
This is a big problem.
This is actually a matter of continuous layering and refinement.
The above sentence must be incomprehensible to everyone. Let me talk about it in detail.
Think about why we used to layer layers before we discussed them? After layering, the same logic functions are put together to ensure readability and scalability.
Can the servlet above be further subdivided?
First, let's look at what the code in that servlet does at the logic level.
1. Obtain parameters.
2 type conversion
3. Execute core business logic
4. Return the processing result
In fact, what a servlet (program execution and control module) does is nothing more than the above four steps. So can we separate the four of them?
Like a car factory, at the very beginning, from raw material (steel) Acquisition, material cutting, welding, spray color to final sales were all owned by one person (in the above example, that is to say, the four points above are all done in a servlet.) is that good? Not good. Why?
If the car was originally sprayed with blue paint, I want to change it to white paint. What should I do if I am in trouble in the architecture of doing things by myself?
One person is responsible for purchasing raw materials, one person is responsible for cutting raw materials, and one person is responsible for painting ......
I want to change the spray paint color now. Just look for that person and you will be OK.
Back to the Code, the difficulty is that it is very troublesome to build an assembly line. In particular, this assembly line can not only produce cars, but also aircraft and artillery. All in all, this pipeline should be a set of rules, not entities. How? It is unrealistic for us to build our own pipeline. So let's take a look at the best practices of our predecessors and see how the frameworks they use build the pipeline.

5. The View layer is represented in a variety of forms. With the development of Web development technology, MVC provides a completely transparent way at the framework level to cope with different View representations.
In the previous servlet, we used hard encoding to control the flow of views. The solution to the above problem is actually the answer to the second question. Build pipeline.

6. Although the MVC pattern intuitively defines various elements of the presentation layer for us, how can we use some mechanism to organically integrate these elements to form a whole.

This problem is too big. I have no way to understand it now.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.