Java EE A lot of open source framework, I can only introduce myself familiar with a few, other currently in China IT industry is not a lot of application. Hope that the new framework should not be blindly respected, first of all must be familiar with it than the old in the end good where, new ideas and characteristics is what? And then decide if you want to use it.
The theme of this issue is struts, literal translation is the stent. The first version of Struts, released in May 2001, provides a solution for Web applications, where JSP and servlet coexist to provide a clear framework for separating views and business application logic. Before struts, it is common practice to include business logic in the JSP, or to generate views in the servlet to be forwarded to the foreground. Struts with the new concept of MVC at that time quit almost become the industry recognized Web application standards, so there are many familiar with struts in the modern IT market programmers. Even if there is a new frame to come out, and the reason to continue to use struts added a low risk, because if the developers change, it is easy to recruit new struts IT workers, ^_^!
The author said before is Struts-1, because the new Struts-2, so that every time we talk about Struts must be noted that it is Struts-1 or 2. The author talks about the more familiar Struts-1, the next time to introduce the difference with the Struts-2:
1.Struts Frame Integral structure
The core function of Struts-1 is the front-end controller, and the programmer needs to focus on the back-end controller. The front-end controller is a servlet, in the middle of Web.xml configuration all request must go through the front-end controller, its name is Actionservlet, by the framework to implement and manage. All view and business logic isolation should be for this actionservlet, it is like a traffic policeman, all passing vehicles must pass through its discernment and then be sent to a specific channel. All, the understanding of it is the distributor, we can also be called dispatcher, in fact, understand the servlet programming people can also write a distributor, and to intercept request filter, in fact, it is not very difficult to implement a struts framework. The main purpose is to write the view and background logic can be separated from the tight coupling, the respective synchronization of the completion of their work.
So with the actionservlet in the middle responsible forwarding, the front end of the view, such as JSP, just to submit all the data, the data will reach the appropriate processing of its back-end controller action, and then processed inside, After processing, forward to the foreground of the same or different view JSP in the middle, back to the front is also used in the servlet forward and redirect two ways. So so far, all it has to do is to use the Servlet API to build a convenient framework. Is this the most salient feature of struts? Controller.
Then another feature, which can be said to be a relatively successful concept of Struts-1, is to replace hard-coded configuration information with XML configuration. The previous decision JSP to which servlet to submit, is to write into the JSP code, that is, once the submission path to change, we have to rewrite the code and recompile. And struts proposed the idea is that the code is only a logical name, it corresponds to which class file written in the XML configuration file, this profile records all mapping relationships, once you need to change the path, change the XML file than change the code is much easier. This idea can be said to be quite successful, so that later frameworks continue this way of thinking, and XML plays an increasingly important role.
In general, struts has brought us so much freshness, and all other features are based on convenient control forwarding and extensible XML configuration to accomplish their functions.
The action and Formbean are described below, which are the core two components of struts.
2. Back-end controller action
Action is what we call a back-end controller that must inherit from an action parent, and struts has designed many kinds of action, such as Dispatchaction, Dynavalidationaction. They all have a way to handle business logic execute (), incoming Request,response,formbean and actionmapping four objects, return Actionforward object. Before you arrive at the action, you will be preceded by a requestprocessor to initialize the mapping of the configuration file, here are some points to note:
1 To ensure thread safety, in an application lifecycle, the Struts framework creates only one action instance for each action class, all client requests share the same action instance, and all threads can execute its execute () method at the same time.
So when you inherit the parent action and add the private member variable, keep in mind that the variable can be accessed by multiple threads, and that the synchronization must be the responsibility of the programmer. (All we do not recommend it). When using the action, the important principle of ensuring thread safety is to use only local variables in the action class, and use instance variables sparingly. Local variables are private to each thread, and the Execute method ends up being destroyed, and the instance variable is shared by all threads.
2 When an Actionservlet instance receives an HTTP request, the process () method is invoked in the doget () or Dopost () method to process the request. The Requestprocessor class contains a hashmap that is the cache that holds all action instances, and the property key that each action instance holds in the cache is the action class name. In the Processactioncreate () method of the Requestprocessor class, first check to see if an action instance exists in the HashMap. The code that creates the action instance is in a synchronized code block to ensure that only one thread creates the action instance. Once the thread creates the action instance and stores it in the HashMap, all subsequent threads will use the instance of the cache directly.
3 <action> Element <roles> property specifies the security role that the action user must have to access, separated by commas between multiple roles. The Requestprocessor class invokes its own Processroles () method when preprocessing a request, checks whether the security role is configured for action in the configuration file, and, if so, calls HttpServletRequest's IsUserInRole ( method to determine whether the user has the necessary security role, and if not, return the error directly to the client. (The returned view is specified by the <input> property)