STRUTS2 Technical Insider Reading notes three expression layer of perplexity

Source: Internet
Author: User
Tags expression engine

What doubts does the presentation layer have? Very simply, we forget all the frames for the time being, just write a registered servlet to see.
index.jsp
<form id= "Form1" Name= "Form1" method= "Post" action= "Loginservlet" ><table width= "357" border= "0" align= " Center >    <tr>      <td width= "$" > User name:</td>      <td width= "219" ><label>        <input name= "user" type= "text" id= "user" value= "DLF"/>      </label></td>    </tr>    <tr>      <td> birthday:</td>      <td>        <input name= "Birthday" type= "text" id= "pwd" Value= "2012-05-04"/>      </td>    </tr>    <tr>      <td>        <input type= " Submit "Name=" submit "value=" Login "/>      </td>    </tr></table></form>

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 respons      E) throws Servletexception, IOException {request.setcharacterencoding ("utf-8");    Set format Response.setcontenttype ("text/html");     PrintWriter out = Response.getwriter ();   Get the parameter 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 the second step in the MVC Model Diagram, create user.setname (name);            Equivalent to the fourth step of the MVC Model Diagram extract User.setbirthday (date); The data in the bean comes from the view UserService Us=neW UserService ();                        Core business logic us.register (user); Returns the result of the processing//equivalent to the third step in the MVC Model Diagram forward request.getrequestdispatcher ("/success.jsp"). Forward (Request,   Response); } public void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti    On {doPost (request,response); }}



Xml
<servlet>    <servlet-name>register</servlet-name>        <!--This is the name of the servlet--    < Servlet-class>example. Registerservlet</servlet-class>    <!--here to write the package path of the Servlet class--  </servlet>  < Servlet-mapping>                 <!--here is the address map--    <servlet-name>register</servlet-name><!-- This and the above name--    <url-pattern>/loginServlet</url-pattern><!--write the servlet map address here--  </ Servlet-mapping>
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 code above?
No problem (except take the parameter part)
Is the code above good?
Not good.
Where's the bad?
The bad place is mainly at the following two points:
1 for procedural readability and maintainability considerations, the program also needs to be refactored.
2 Business expansion also has problems.
The above-mentioned problem is very macro and very broad, the following specific to say a few questions.

1 when the browser sends an HTTP request, how does the Web container receive the request and specify the appropriate Java class to execute the business logic and return the processing results?
This is actually a URL mapping problem.
For this interested friend can read the following article
How Tomcat works reading notes on 11 standwrapper
http://blog.csdn.net/dlf123321/article/details/41247693
The parameters inside are from Web. Xml.
In fact, how to find the corresponding Java class, it is plain that the servlet container will parse the requested URL and the Web. XML inside the servlet-mapping element under the Uri-pattern control, if found consistent, Just follow servlet-name to find the appropriate Java class.
As for this generation of Java objects based on URIs, the Digerter library is used in Tomcat. See the relevant content:
http://blog.csdn.net/dlf123321/article/details/41802443
It's simple. But here is also a problem, if a Web. XML is written in 3-5 servlet, we look at the convenience; The problem is that if the program execution and control part of a system is made up of dozens of hundreds of, all in the Web. XML, it's a disaster.
The strategy to solve this problem is to set up a set of rules matching engine from URI to Java class.

2 in Web requests, how the data flows smoothly with the browser and the Java world. Can we do automatic matching?
The information we see in the browser is a string (weak type), but in Java there are different data types (Java is strongly typed), String,int,boolean, and so on. In the above servlet, we see the conversion of the Birthday property. It doesn't look complicated, but how about a property that's not a 100? The type of the property is no longer the base type?
The strategy for solving this problem is to use the expression engine

3 multithreading issues for Servlets.

This is a relatively large problem, detailed description see

Threading issues in Tomcat

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



4 control layer as the core controller of MVC, how to support the extension on the function point to the maximum extent.
This is a big problem.
This is actually a problem that is constantly layered and refined.
The above sentence is sure to make people do not understand. Let me say it carefully.
Think about how we layered before we talked about layering. Because of layering, the same logical function points are guaranteed for readability and extensibility.
Is it possible for the servlet above to subdivide the function again?
Let's start by looking at what the code in that Servlet did on the logical level.
1 Getting parameters
2 Type Conversions
3 Executing core business logic
4 Returning processing Results
In fact, a servlet (program execution and control module) did nothing but four steps above. So can we split them four?
Like a car factory, in the beginning, from the raw materials (steel), material cutting, welding, color spraying to the final sale is a person responsible (in the above example, that is, the above four points are in a servlet dry), this good? Why?
If the car was sprayed with blue paint, I would now like to spray white paint. In a person's work so the structure of things next very troublesome, then what?
Do a "line", a person responsible for purchasing raw materials, a person responsible for cutting raw materials, a person responsible for painting ...
I want to change the color of the painting now, just look for the person on the OK.
We go back to the code, the problem is that to build a "pipeline" is very troublesome, especially the assembly line can not only produce cars, but also produce aircraft, but also the production of artillery. In a word, this "pipeline" should be a set of rules, not entities. What do we do? We do it by ourselves. The line is unrealistic,so look at the best practices of seniors and see how they use the framework to build "pipelining"。

5 The view layer is represented in a variety of ways, and as web development techniques evolve, how MVC provides a completely transparent way to respond to different view representations at the framework level.
In the previous servlet, we used a hard-coded form to control the flow of the view. The solution to the above problem is actually the answer to the second question.build the pipeline.

6 MVC pattern Although it is very intuitive for us to specify the various elements of the presentation layer, but how to use some mechanism of these elements are organically integrated, thus forming a whole.

This problem is too big, I have no way to understand at present, only then.


Resources

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


STRUTS2 Technical Insider Reading notes three expression layer of perplexity

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.