Getting started with Struts2 (1) -- configuration file, getting started with struts2

Source: Internet
Author: User

Getting started with Struts2 (1) -- configuration file, getting started with struts2
I. Preface

1. Understand the three frameworks

What is a framework?

A framework is a standard, a rule, and a rule that organizes technology.

 

What are the three major frameworks (SSH), Struts, hibernate, and spring?

Struts is mainly responsible for displaying the layer. spring uses IOC and AOP to process and control the business (for database operations). hibernate is mainly used to ensure data persistence to the database.

Struts processing process:

In the Struts 2 download file Struts-core-2.5.2. (Note: Different jarStruts core controllers have different names), StrutsPrepareAndExecuteFilter is the core controller of the Struts 2 framework,

It intercepts all user requests specified by <url-pattern>/* </url-pattern>. When user requests arrive, the Filter filters user requests.

By default, if the path of the user request does not contain a suffix or ends with a. action, the request will be transferred to the Struts2 framework for processing. Otherwise, the Struts 2 Framework will skip the process of the request.

When a request is transferred to the Struts 2 Framework for processing, it passes through a series of interceptors and then to the Action. Unlike Struts1, Struts 2 creates an Action for each user request, so the Action in Struts2 is thread-safe.

2. Build the Struts 2 Environment

(1) download the jarfile of Struts 2, which uses struts-2.5.2-all.zip. (here)

(2) Compile the Struts 2 configuration file, which is divided into struts. xml

(3) add Struts 2 Framework STARTUP configuration in Web. xml

 

Build the Struts 2 development environment-the required jar package

After downloading, decompress the file and copy the required file to the lib file of the project. The configuration file required by Struts 2.5 is configured here.

Required jar packages: Especially log4j. jar and javassist. jar are required packages.

Note:If clesses does not exist, create it by yourself and then create struts. xml

The configuration template code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- Note: <! DOCTYPE>, so it must be automatically introduced,
If you are using 2.3 or a few o'clock, you just need to change the Confinuration2.5 // EN and the 2.5 in the struts-2.5.dtd below to 2.3 or a few. --> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.5 // EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> </struts>

 

Build Struts 2 Development Environment-Struts 2 STARTUP configuration on the Web

In struts1.x, the struts framework is started through Servlet. In struts2, the struts framework is started through Filter.. The configuration in web. xml is as follows:

<! -- Define a filter for Struts2 --> <filter-name> struts2 </filter-name> <filter-class> org. apache. struts2.dispatcher. filter. strutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name> struts2 </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>

Note:<! -- Since Struts 2.1.3, the following FilterDispatcher has been marked as obsolete

<Filter-class> org. apache. struts2.dispatcher. FilterDispatcher </filter-class> -->

The init method in the StrutsPrepareAndExecuteFilter class reads the configuration file struts. xml in the path to complete initialization.

After reading the content of struts. xml, it will be stored in the content in the form of javabean. Later, struts 2 will use the data in the memory for each request to the user, instead of reading the file.

 

Getting started example: (this example is an official document example)

Add the configuration in the newly created struts. xml configuration file:

 

<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.5 // EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name = "tutorial" extends = "struts-default "> <! -- Name: You can define the name by yourself. class: The exact path of the implementation class --> <action name = "helloword" class = "com. hello. helloWord "method =" execute "> <result name =" success "> HelloWorld. jsp </result> </action> </package> </struts>

In the Struts2 framework, packages are used to manage a group of business-related actions. In actual development, a group of business-related actions should be placed under the same package.

Note: The combination of struts. xm and Actoin will inevitably make the file bloated. We can use <include file = "file name. xml"/> to split struts. xml into multiple.

 

Parse the labels in struts. xml:

Name attribute in Action:

You must specify the name attribute When configuring the package. The name can be any name but must be unique and cannot correspond to the java class name. If you want other packages to inherit the package, you can use this attribute for reference.

Method property:

Specifies the method called by the server. The default value is execute.

Namespace attributes:

Namespace, which is part of the access path. If the path is helloword. action, add/namespace/helloword. action. If not specified, the default value is "" (Null String)

Generally, each package should inherit the struts-default package, because many core functions of Struts2 are implemented by the interceptor.

When using the Action Element, you must specify a series of result elements. Each result element defines the ing between the logical view and the physical view.

In the result element, the type attribute defaults to jsp as a view. You can set the type value to change the view to be displayed in the data.

 

The example uses the HellWord. java implementation class code:

package com.hello;import com.opensymphony.xwork2.ActionSupport;public class HelloWord extends ActionSupport {        public static final String MESSAGE = "Struts is up and running ...";    private String message;    public void setMessage(String message){        this.message = message;    }     public String getMessage() {        return message;    }    public String execute() throws Exception {        setMessage(MESSAGE);        return SUCCESS;    }}

 

The HelloWord. jsp code in the struts. xml configuration file:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %>    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 

The URL path for accessing actions in struts2 consists of two parts:Package namespace + action nameFor example, the URL for accessing HelloWord in this example is/helloworld (Note: The complete path is

Http: // localhost: Port/project name/helloworld ). In addition, you can add the. action suffix to access this Action.

 

The Running Effect of the Getting Started example is as follows:

 

Related Article

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.