STRUTS2 Basic Knowledge

Source: Internet
Author: User

A summary of STRUTS2 basic knowledge and STRUTS2

Struts1 is the implementation of the servlet!

Struts2 is the filter implementation (Intercept interceptor)! Meet the MVC Framework!

Note: MVC is the design pattern for the project (usually in accordance with the low-coupling high cohesion)

M:model--Model layer
encapsulated data Type--entity
completing the interaction of the data source-DAO
Encapsulating business Logic-service
Auxiliary Tool Class--Util

V:view--View layer
Show Data
Collect Data

C:control--Control layer
bridges connecting M and V

M1:
C and V are combined together
servlet/jsp do both the C-layer and the V-layer.

M2: MVC in a strict sense
Servlets do only layer C
JSP only does v layer

Struts2 Features:
1, simplified C layer
any javabean can be made into a C layer .
2, encapsulated form data
do not need to see the request object
3, encapsulated more page jump phenomenon
4, Low coupling
5, providing a rich library of tags
Drawing HTML pages
realizing the role of Jstl
6, provides the OGNL
7, the verification of the background data is encapsulated
8, provides token mechanism token
prevent duplicate submissions of forms

Second, build the environment

1. Create a Web project

2. Importing the JAR Package

3. Configuration files

Struts2.xml: Typically the import jar package process is automatically generated. The name cannot be changed (placed in the SRC root directory). However, you can have more than one configuration file with different names. The premise is that there must already be a struts.xml. If the other configuration file is valid, it needs to be imported in the Struts2.xml file, using the Include tag method. Example: <include file= "User.xml" ></include>

The 4.web.xml file must be configured with a filter implemented by the STRUTS2.

<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>*.action</url-pattern>
</filter-mapping>

Iii. Struts2 acts as a C-layer instructions for use 1. Create a new class

    The principle of naming: Usually add action after Changjian Java class

2. Configure this class in Struts.xml

    Purpose: To enable it to be used as a C layer

<package>
<action>

<result></result>

</action>
</package>

3. Configure Label Package

   It is not the package name in front of the class to classify the action.

Include attributes:

A.name: Package Name-the only logo used, not much meaning (generally casually take).

B.namespace: namespace (provides access path). Must start with/.

C.extends: Inheritance (which features you want to use for Struts2). If there is no special action, write directly to default (Struts-default).

4.Action Label

    Configure the written JavaBean (act as c).

Included Properties:

A.name: Access the action's URL (virtual path). Note that you cannot start with/.

B.class: Class full name.

C.method: Specifies the name of the method in the action class. The effect is the method accessed under the URL.

Description of the 5.Action class

  The action class is a normal javabean that can have properties and methods. Methods can have any number, each of which is equivalent to the service method in the servlet and can be used as a service method. However, this method requires two points:

A. The method must have a return value (String). The function of the return value is to implement a corresponding jump or redirect in the configuration file based on the result of the method.

B. The method has no parameters.

6. Ways to access action

   Url:http://serverip:port/webapp/namespace/actionname. suffix

For example, access the Hello method for helloaction:
Http://localhost:8888/Struts2/day01/hello.action

7. Suffix issues

    If the URL does not have a write. Action, it is added by default, and if the suffix is modified, the suffix cannot be omitted;

8.404 questions

1, suffix not correct
2,namespace not correct
There is no action mapped for action name Hello1.
3,action name is incorrect (org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter)
There is no Action mapped for namespace [/day01]
and action name [HELLO3] associated with context path [/struts2].

9.500 questions

Action's name is incorrect--control pin
Org.apache.struts2.dispatcher.FilterDispatcher

   10.result page Jump

Properties:
The return value of the method in Name:action, the default value of success, or a fail is defined.
Type: The way to jump, the default is dispatcher (forwarding)

Iv. Struts2.xml Weight loss (reduced configuration code)

  1. Method matching

Write a good <action>. When visiting, Http://serverIP:PORT/webApp/namespace/actionName!methodName.action

2. Pass-to-match

<action Name= "Basic name _ wildcard character" method={index}>

Basic name _ wildcard character: hello_*
Index: The position of the wildcard character, starting with 1
Hello_* hello_*_*

When accessed, http://serverIP:PORT/webApp/namespace/the base name _ Method name. Action

V. Constant labels in struts2.xml

  Written in the <struts> root tab.

<constant name= "struts.action.extension" value= "Do" ></constant>

To modify a suffix:
The modified suffix overrides the original action,
The system will not be added. Action,url must be suffixed

Vi. default value issues for action tags

Calss:actionsupport
Execute method for the Method:actionsupport class (return "Success")

vii. Action receives page data issues

  The action class will provide some properties. Furthermore, the attributes must satisfy:

1, the property name is the same as the Name property of the form (key of the query string)
2, to provide a set, get method

  Note: A. There is a Actioncontext object in Sturts2--responsible for managing all the action objects.

B. If the property in action is an object (entity Class)--the Name property of the page---object. Property name. Example: UserName----user.username

Eight, redirect

  <result type= "" >

1,redirect (generally used method)
Resources: Static Page Resources
Dynamic servlet path, action path

2,redirectaction
Resource: Can only be an action path

Nine, storage rangemethod One:

Value stack: Stores the properties of an Action object
Request: Actioncontext equivalent to request range
Session:
Application:

For example:
Public class Rangeaction {
String name = "Zhang";//Property--Value Stack
Public String Range () {
//Get Actioncontext object
Actioncontext ac = Actioncontext.getcontext ();
//Request
ac.put ("age", +);
//Session
ac.getsession (). Put ("Sess", "session");
//Application
ac.getapplication (). Put ("app", "Application");
return "Success";
    }
}

  Method Two:

Import Org.apache.struts2.interceptor.SessionAware;

Public class RangeAction1 implements Sessionaware {
Private Map<string, object> session = new hashmap<string, object> ();
Public void Setsession (map<string, object> arg0) {
this.session = arg0;
    }
Public String Range () {
session.put ("Session1", "session");
return "Success";
    }
}

Note: Storing data in any range, similar to the map operation

STRUTS2 Basic Knowledge

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.