Struts2 Study (i)

Source: Internet
Author: User
Tags to domain

I. Result:

1). Result is a child node of the action node

2). Result represents a destination that may go after the action method executes

3). A single action node can configure multiple result child nodes.

4). The Name property value of result corresponds to a return value that the action method may have.

<result name= "Index" >/index.jsp</result>

5). Result has a total of 2 attributes, and one is type: The response type that represents the result

6). The Type property value of result is defined in the Name property of the Result-types node of the Struts-default package.
Commonly used to have
> Dispatcher (Default): Forward. Forwarding in the same Servlet.
> Redirect: redirect
> redirectaction: Redirect to an Action
Note: The response type of redirect can also be easily implemented Redirectaction function!

<result name= "index" type= "Redirectaction" >
<param name= "ActionName" >testAction</param>
<param name= "namespace" >/atguigu</param>


<result name= "index" type= "redirect" >/atguigu/</result>

> Chain: Forward to an Action
Note: You cannot forward to an Action by Type=dispatcher way

can only be:

<result name= "Test" type= "Chain" >
<param name= "ActionName" >testAction</param>
<param name= "namespace" >/atguigu</param>

Cannot be:

<result name= "Test" >/atguigu/</result>

Two. Actionsupport

1). Actionsupport is the default action class: If an action node does not have a class attribute configured, Actionsupport is
The action class to execute. The Execute method is the action method that you want to execute by default

<action name= "Testactionsupport" >

Equivalent to

<action name= "Testactionsupport"
Class= "Com.opensymphony.xwork2.ActionSupport"
Method= "Execute" >

2). In the case of manual completion of field validation, display error messages, internationalization, etc., it is recommended to inherit Actionsupport.

Three. Question about the extension of the STRUTS2 request

1). Some constants of the STRUTS2 application are configured in the under the ORG.APACHE.STRUTS2 package

2). struts.action.extension defines the extension of the request that the current STRUTS2 application can accept.

3). The constants configured by can be modified in a constant configuration in the Struts.xml file.

<constant name= "struts.action.extension" value= "Action,do," ></constant>

Four. Access the WEB resource in Action:

1). What is a WEB resource?

HttpServletRequest, HttpSession, ServletContext, and other native Servlet APIs.

2). Why Access WEB resources?

B\s's application Controller must access WEB resources: Read and write properties to domain objects, read and write cookies, get Realpath ....

3). How to access?

Five. How to decouple from the Servlet API: only limited Servlet API objects can be accessed, and only limited methods can be accessed (reading request parameters, reading and writing the properties of the domain object, invalidating the session ...).

> Using Actioncontext

> Implement Xxxaware interface

> Recommendation: If you have more than one action method in an action class, and multiple methods require a MAP or parameters of the domain object, it is recommended to use the
How to Aware interfaces

The map for > session is actually SESSIONMAP type! If you call its invalidate () method after a strong turn, you can invalidate its session!

II. The way it is coupled to the Servlet API: more servlet API objects can be accessed, and their native methods can be called.

> Using Servletactioncontext

> Implement Servletxxxaware interface.

Focus on reviewing the development environment of building STRUTS2:3 steps

2. Action VS Action Class

1). Action: Represents a STRUTS2 request.

2). Action class: The class that can handle the STRUTS2 request.

The name of the > attribute must follow the same naming rules as the JavaBeans property name.
The type of the property can be any type. Data conversions from string to non-string (base database type) can occur automatically

> must have a constructor with no parameters: Create an instance from reflection

> At least one method for struts to invoke when executing this action

> The same action class can contain more than one action method.

> Struts2 creates a new action instance for each HTTP request, that is, the action is not singleton and is thread-safe.

Struts2 Study (i)

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: 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.