I. Overview
Model is in the entire control of user requests, the real processing of user requests and save the processing of the results of the object, throughout the process, we generally use JavaBean to save some information in order to pass between the objects. Because in the framework, the model object is the object that really deals with the function of business logic, so that is the part of the framework where application requirements are implemented with the greatest relevance. In the implementation of struts, model's concrete manifestation is the Actionform object and its corresponding action object. The user submits the form data to verify, even to the data preprocessing can complete in the actionform. Generally, a model object and a request page corresponding to the relationship, but also can be a model object corresponding to multiple page requests. If the Struts-config.xml configuration file does not specify an action for the Model object, the controller will direct the request (encapsulated by the model object completion data) to a View object. The following figure represents the hierarchy of model layer.
Model in struts exists in the form of one or more Java beans. These beans fall into three categories: action Form, Action, JavaBean or EJB. The Action form, commonly referred to as Formbean, encapsulates user request information from the client, such as form information. Action is usually called Actionbean, get the Formbean from the Actionsevlet, take out the relevant information in the Formbean, and make related processing, generally call javabean or EJB, etc.
Many requirements documents focus on the view to build Web applications. We must make sure that each submitted request is already defined in the Model view. Typically, developers focus on developing JavaBean classes in the model component to achieve all the functional requirements. Applications should accurately use which beans vary greatly according to their requirements, but are usually divided into several categories after being differentiated.
Second, create model components
1, JavaBeans
In a web based application, you can save (and access) JavaBeans by using a number of different "properties (attributes)" collections. Each collection has its own different lifecycle and the visibility of where the beans is stored. At the same time, beans defines life cycle and visibility rules through scopes. Scope selection in the JavaServer Pages (JSP) specification is defined using the following items (in parentheses, the equivalent concept definition in the Servlet API).
Page:beans will only be visible in one JSP page, only in the current request cycle. (local variable in the service method)
Request:beans will only be visible in one JSP page, the same as page or the servlet contains this page, or forwarded to this page. (Request property)
Session:beans can be used by all JSP pages and servlet through a specific user session, which can span one or more requests. (Session property)
Application:beans can be used by all JSP pages and Servlets in Web applications. (Servlet context property)
What we need to remember is that in a Web application, the JSP page and the servlet share the settings for the Bean collection. For example, a bean is stored in an attribute in a servlet as follows:
Mystudy mystudy = new Mystudy (...);
Request.setattribute ("cart", mystudy);
After this servlet forwards the request to a JSP page, we can immediately use the standard action tag (tag) to see the corresponding value:
<jsp:usebean id= "cart" scope= "request" class= "Com.mycompany.MyApp.MyStudy"/>
2, Actionform Beans
In actionform beans frequently have attributes equivalent to attributes in our model beans, that form beans themselves should consider becoming a controller component. Similarly, they can pass data between the model and the view layer.
The struts framework usually assumes that we have defined a actionform beans for the input in our application (in short, a Java class that extends from the Actionform class). Actionform beans sometimes simply invoke form beans (form beans). This could be a fine-grained pair of pixels, with each form corresponding to a bean, and a bean serving several forms or even all of them to form a coarse-grained situation.
If the Bean,struts controller servlet is defined in our struts configuration file, it will automatically provide us with the following services before invoking the appropriate Action method:
Use the appropriate keyword to check for an instance of a bean of the appropriate class in the appropriate scope of the user (request or session).
If no such instance is available, a new instance of the bean is automatically created and the period is added to the appropriate scope (request or session).
Each request parameter is mapped to one of the bean's properties by its name, and the corresponding setter method is invoked to set the property value. This method is similar to using the <jsp:setproperty > tag in a standard JSP to use the wildcard "*".
The updated Actionform Bean is passed to the Execute method of the Action class [org.apache.struts.Action] so that these values can be used by our system state and business logic bean.