Getting started with struts2 (Environment setup, basic case implementation)

Source: Internet
Author: User

Getting started with Struts2

1. struts2 Overview

2. Build the struts2 environment (the first struts2 Application)

3. Detailed description of struts. xml file configuration

4. Overview of the Order of the search paths for the URLs requested by struts2

5. struts2 working principle Overview

6. struts2 multiple configuration files

7. struts2 dynamic method call

8. Define action with wildcards

 

 

I. Introduction

 

Struts 2 is the next-generation product of Struts. It is a new struts 2 framework that is merged Based on Struts 1 and WebWork technologies. The new architecture of Struts 2 is very different from that of Struts 1. Struts 2 takes WebWork as the core and uses an interceptor mechanism to process user requests. This design also enables the business logic controller to completely remove from the ServletAPI, therefore, Struts 2 can be understood as a WebWork update product. Although there are many changes from Struts 1 to Struts 2, the changes in Struts 2 are very small compared to WebWork.

II. Environment Construction

When constructing the Struts2 environment, we generally need to do the following steps:

1. Find the jar file to be used to develop the Struts2 application.

Download the official website:

Http://struts.apache.org/download.cgi#struts221

After decompression:

Start to learn how to use the most basic jar package of dependency

2: Compile the Struts2 configuration file

Extract one of the examples. Find the struts. xml file in this directory and copy it to the src file of the project. And then modify.

Delete the remaining Code as follows:

 

<?xml version="1.0"encoding="UTF-8" ?><!DOCTYPE struts PUBLIC   "-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"   "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> </struts>

 

3: Add Struts2MVC framework STARTUP configuration to web. xml

 

Copy the following code to the web. xml configuration file (pasted from the downloaded example)

 

<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>    </filter-mapping>

 

III. Basic Case: ---------- HellowWorld

 

The java class is under src.

Package action; public classHelloAction {private String message; public String getMessage () {return message;} public void setMessage (Stringmessage) {this. message = message;} public String execute () {this. message = "My first struts program"; return "success";} public String other () {this. message = "second method"; return "success ";}}


Configure the struts2 file ------ Struts. xml code:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD StrutsConfiguration 2.3 //" http://struts.apache.org/dtds/struts-2.3.dtd "> <struts> <! -- Set constant to implement dynamic call --> <constant name = "struts. enable. DynamicMethodInvocation" value = "true"/> <! -- The name must be unique to the corresponding module --> <package name = "hello" namespace = "/hello" extends = "struts-default"> <action name = "test" class = "action. helloAction "method =" execute "> <! -- Put it for security, WEB-INF/pages do not understand can also be placed under the webRoot directory --> <result name = "success">/WEB-INF/pages/index. jsp </result> </action> </package> </struts>


Running result


4. Note:

 

 

Default Value

If no class is specified for action, ActionSupport is used by default.

2> if no method is specified for the action, the execute () method in the action is executed by default.

3> If the name attribute of result is not specified, the default value is success.

Http: localhost: Port/content path/package name/action name! Method Name

 

 

 

Dynamic method call:

You can find that there is another method ------ other () in the java class above ()

The call method is as follows:

 

Tip: This call method brings security risks!

Use the latest struts2 (2.3.15.2) and cannot use DMI dynamic call Methods

 

<! -- Set constant to implement dynamic call --> <constant name = "struts. enable. DynamicMethodInvocation" value = "true"/>

 

Wildcard usage:

Modify the action of the struts. xml file

<! -- Use of wildcards --> <action name = "test _ *" class = "action. HelloAction" method = "{1}">

 

The access method is package name + namespace + test _ + method name.

 

The above content is my personal summary. If there are some omissions, we hope you can correct them.

You may have some good ideas or opinions to discuss with each other.

QQ: 897982714

 

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.