Struts2 Summary 1

Source: Internet
Author: User

Why use struts and other framework technologies?
It simplifies our development and saves project costs.
Basic Development Mode (without framework)
1. Data Table 2, Java entity class (encapsulate data queried from the database) 3. Data Access Object Dao 4, JSP/Servlet
MVC:
1. Model: The model is responsible for business logic, including business data and business processing logic. For example, Entity classes, services, and Dao all belong to the model layer.
2. view: the role of a view is to display the interface and user interaction. The View class is a JSP that does not contain the business logic and control logic: if the page contains <%>, it cannot be regarded as a view-layer class, or the control logic of forwarding or redirection in JSP is not acceptable.
3. Controller: the control layer is a bridge between the view layer and the model for controlling the process. For example, acctionservlet
Configure the struts2 front-end controller in Web. xml: Implemented by filter

<Filter> <filter-Name> struts2 </filter-Name> <filter-class> Org. apache. struts2.dispatcher. ng. filter. strutsprepareandexecutefilter </filter-class> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/* </url-Pattern> indicates that all requests must pass the filter. </filter-mapping>

Advantages of struts:
1. Automatic Data encapsulation: The data is automatically encapsulated into the corresponding name attribute in the action according to the name attribute of the page component, for example: in JSP, <input name = 'name' type = 'text'> in action, the private string name attribute is automatically assigned a value.
2. Automatic Data Transmission: attributes in the action can be directly obtained from the JSP page using the El expression, for example, $ {name}
Configuration File struts. xml:

 <Struts>        <package name="test" extends="struts-default" namespace="/test">            <action name="welcome" class="com.cn.WelcomeAction" method="">            <result name="success">/jsp/welcome.jsp</result>            <result name="fail>/jsp/nameform.jsp</result>            </action>        </package>    </Struts>

Method: Specifies the name of the method to be executed in the corresponding class. The return value of this method must be of the string type. If no method = "" property exists, the execute method is executed by default.
Extends = "struts-Default" refers to the name of the inherited package, which generally inherits the default Struts-default package provided by struts2. This package defines many required components (such as interceptor) of the struts2 application)
<Action name = "nameform"> only when the name attribute is in, the Struts framework without the class attribute adds a class for this <action> by default to forward to the page specified in the corresponding <result name = "success">.

8. ognl expression:
Ognl is encapsulated in the ognl. jar package.
El expression: the El expression extracts data from four aspects (pagecontenxt, request, session, and application) for display or calculation. The El expression solves the mismatch between Java code and HTML. The El expression replaces the Java code with a string expression to indicate data acquisition and calculation.

Ognl engine access object format: ognl. getvalue ("ognl expression", root object); root object is the main object for ognl operations
1. Basic type attributes ognl. getvalue ("ID", foo); ognl. getvalue ("name", foo );
2. array and list attributes: attribute name [subscript] ognl. getvalue ("array [0]", foo) ognl. getvalue ("list [1]", foo );
3. Map attributes ognl. getvalue ("map. One", foo); ognl. getvalue ("map ['two'], foo); one and two are key values.
4. Basic operation: ognl. getvalue ("ID + 100", foo );
5. Call method: ognl. getvalue ("name. touppercase ()," foo); ognl. getvalue ("list. Size ()", foo );
6. Call a static method. The format of calling a static method is @ Class Name @ method name based on the retrieved property.
Ognl. getvalue ("@ java. util. arrays @ tostring (arry)", foo)
7. ognl can only create map and List objects
Note the following when creating a map object: # ognl. getvalue ("# {1: 'java', 2: 'javajava', 3: 'javajava'}", null) cannot be lost );
9. Basic Structure of the ognl engine: ognl engine, root object (ognl operation object), and context object (a map used to store public data related to the entire system );
With the ongl engine, each access request needs to use some data during access, and the data can be stored in the context object.
Ognl. getvalue ("name", root); without #, the data is retrieved from the root of the business object.
Ognl. getvalue ("# num", root); add # to retrieve data from the common object context

10. xwork extension to ognl:
Compoundroot object; In xwork, the root object is changed to a bucket similar to the "stack", where multiple root objects can be stored;
When the de-data conforms to the "stack" rule, if the ognl expression is name in compoundroot, check whether the name attribute exists from the top of the stack.
Context (MAP) object

11. Basic valuestack Structure
Struts encapsulates the previously mentioned xwork extension mechanism for ognl. This object is called valuestack.
When struts2 is started, a valuestrack object is created. When a user sends a request to the corresponding action, struts2 puts the currently requested Action1 to the "stack space" Stack Top Of The commpoundroot object, when the request ends, action1 is cleared. When another request arrives, struts2 puts the Action2 corresponding to the request to the top of the stack. Therefore, we can access the action at the top of the compoundroot object stack through the ognl expression.

12. core concepts of valuestack
When the request arrives, struts2 will first create a valuestack; then, put the current action object to the top of the stack (compoundroot); struts2 will put the valuestack in the request and the attribute is "struts. valuestack ", so the tag library can access valuestack
Many struts2 tags obtain data by accessing valuestack:
Use ognl to retrieve data from valuestack and display:
<S: property value = "ognl..."/>
Omit value and retrieve the top stack of valuestack <s: property/>
Use ognl to retrieve the set from valuestack and place the objects in the Set in sequence at the top of the stack. in a loop, the top of the valuestack stack is the data to be displayed.

 <s:iterator value = "ognl...list">                                            <s:property value = “name"/>                        </s:iterator>

13. The El expression searches for the name attribute from the pagecontext, request, session, and application ranges, but the name attribute is placed in the action; the action is stored in the valuestack, And the valuestack is stored in the request, in other words, we cannot directly find the name in the request, but why can we use the El expression? This is because struts supports the El expression, which overwrites the getattribute method through strutsrequestwrapper. In this way, if the getattribute method is used during programming, the use of request is no different from that before callback.
If you use the getattribute method, the method rewritten by the struts2 framework is called.

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.