STRUTS2 basic Knowledge (i)

Source: Internet
Author: User
Tags abstract access properties file upload

Struts2

Struts2 was developed on the basis of WEBWORK2. Like Struts1, Struts2 also belongs to the MVC framework. One thing to note, though, is that although the differences between Struts2 and struts1 in their names are not very large, Struts2 and struts1 are almost different in code writing styles. So, since there is struts1, why do we have to launch struts2. The main reason is that STRUTS2 has the following advantages:

1 > in Software Design Struts2 is not tightly coupled with SERVLETAPI and struts APIs like STRUTS1, STRUTS2 applications can be independent of the Servlet API and the Struts API. STRUTS2 's design is non-intrusive, while Struts1 is an intrusive design.

Public Classorderlistaction extends Action {

Public Actionforward Execute (actionmapping mapping,actionform form,

HttpServletRequest request,httpservletresponse Response)

Throws Exception {

}

}

The 2> Struts2 provides interceptors that enable AOP programming with interceptors, such as permission interception.

3> Strut2 provides a type converter, and we can convert special request parameters into the desired type. In Struts1, if we are to implement the same functionality, we must beanutil register the type converter to Struts1 's underlying implementation.

4> STRUTS2 provides support for various presentation layer technologies such as JSP, freemarker, velocity, etc.

The input checksum of the 5> Struts2 can be calibrated for the specified method, which solves the Struts1 long-term pain.

6> provides global scope, package scope, and action scope for internationalized resource file management implementations

build STRUTS2 development environment

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

1. Find the jar file you need to use to develop your STRUTS2 application.

2. Writing Struts2 configuration files

3. Add STRUTS2MVC Framework boot configuration to Web. xml

Build STRUTS2 development environment--develop STRUTS2 application dependent jar file

You can go to http://struts.apache.org/download.cgi download Struts-2.x.x-all.zip, currently the latest version of 2.2.1.1. After download, unzip the file, develop the STRUTS2 application need to rely on the jar file in the unpacked directory of the Lib folder. Different applications require a different jar package. The following is a minimum required jar for developing a Struts 2 program.

Core class Library of Struts2-core-2.x.x.jar:struts 2 framework

Xwork-2.x.x.jar:xwork class Library, Struts 2 builds on it

Ognl-2.6.x.jar: Object graph Navigation Language (ObjectGraph Navigation Language), STRUTS2 framework through its properties of read and write objects

Freemarker-2.3.x.jar:struts 2 UI label templates are written using Freemarker

COMMONS-LOGGING-1.1.X.JAR:ASF's log package, the STRUTS2 framework uses this log package to support log4j and JDK 1.4+ log records.

Commons-fileupload-1.2.1.jar File Upload Component, 2.1.6 version must be added after this file

Build STRUTS2 development environment--STRUTS2 application configuration file

Struts2 The default configuration file is Struts.xml, which needs to be stored under web-inf/classes, the configuration template for the file is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE Struts Public

"-//apache software foundation//dtd Struts configuration2.0//en"

"Http://struts.apache.org/dtds/struts-2.0.dtd" >

<struts>

</struts>

build STRUTS2 development environment--STRUTS2 launch configuration in the Web

In struts1.x, the Struts framework is initiated by the servlet. In Struts2, the struts framework is started by using filter. His configuration in Web. XML is as follows:

<filter>

<filter-name>struts2</filter-name>

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

<!--since struts 2.1.3, the following filterdispatcher have been marked as obsolete

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

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

The init () method in Strutsprepareandexecutefilter will read the default configuration file under the Classpath struts.xml complete the initialization operation.

Note: After struts2 reads the contents of Struts.xml, it is stored in memory in JavaBean form, and struts2 each request processing of the user will use the data in memory instead of reading the Struts.xml file every time.

First STRUTS2 application-helloworld

In the default configuration file Struts.xml, add the following configuration:

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE Struts Public

"-//apache software foundation//dtd Struts configuration2.0//en"

"Http://struts.apache.org/dtds/struts-2.0.dtd" >

<struts>

<package name= "csdn" namespace= "/test" extends= "Struts-default" >

<action name= "HelloWorld" class= "cncsdn.action.HelloWorldAction" method= "execute" >

<resultname= "Success" >/WEB-INF/page/hello.jsp</result>

</action>

</package>

</struts>

Introduction to Packages in Struts.xml configuration

<package name= "csdn" namespace= "/test" extends= "Struts-default" >

<actionname= "HelloWorld" class= "cn.csdn.action.HelloWorldAction" method= "execute" >

<resultname= "Success" >/WEB-INF/page/hello.jsp</result>

</action>

</package>

Using packages in the STRUTS2 framework to manage the action, the role of the package is very similar to the class package in Java, and it is primarily used to manage a set of action related to business functions. In practical applications, we should place a set of actions related to a business function under the same package.

When you configure a package, you must specify the Name property, which can be any name, but must be unique, and he does not correspond to the Java class package, which must be referenced by the other package if it is to inherit the package. The namespace property of the package is used to define the namespace of the package, which is part of the path that accesses the action under the package, such as the action for accessing the example above, and the access path is:/test/helloworld.action. The namespace property can not be configured, and for this example, if the property is not specified, the default namespace is "" (an empty string).

Usually each package should inherit the Struts-default package, because many of the core features of Struts2 are interceptors. Such as: Package request parameters from the request to action, file upload and data validation, etc. are implemented through the interceptor. Struts-default defines these interceptors and the result type. You can say this: When a package inherits Struts-default, it can use the core functionality provided by STRUTS2. The Struts-default package is defined in the Struts-default.xml in the Struts2-core-2.x.x.jar file. Struts-default.xml is also the STRUTS2 default profile. STRUTS2 automatically loads the Struts-default.xml file every time.

Packages can also be defined as abstract packages by abstract= "true", which cannot contain an action in the abstract package

First STRUTS2 application-hellworld

The Cn.csdn.action.HelloWorldAction class used in the example is as follows:

Package cn.csdn.action;

public class helloworldaction{

Privatestring message;

Publicstring GetMessage () {

ReturnMessage;

}

Publicvoid setmessage (String message) {

This.message= message;

}

Publicstring Execute () {

This.message= "My first Struts2 application";

Return "Success";

}

}

The/web-inf/page/hello.jsp used in the example are as follows:

<%@ page language= "java" pageencoding= "UTF-8"%>

<! DOCTYPE HTML Public "-//w3c//dtdhtml 4.01 transitional//en" >

<title> First STRUTS2 Application </title>

<body>

${message} <br>

</body>

You can use an EL expression to access properties in the action.

L access HelloWorld app in Struts1, specify the URL path to access the action by <actionpath= the "/test/helloworld" > Node's Path property. In Struts2, this is not the case, the URL path to the action in the Access struts2 consists of two parts: the name of the package's namespace +action, such as the URL path for accessing this example helloworldaction:/test/ HelloWorld (Note: The full path is: http://localhost: Port/content path/test/helloworld). Alternatively, we can add the. Action suffix to access this action.

<package name= "csdn" namespace= "/test " extends= "Struts-default" >

<action name= "HelloWorld" class= "cn.csdn.action.HelloWorldAction" method= "execute" >

<resultname= "Success" >/WEB-INF/page/hello.jsp</result>

</action>

</package>

Access HelloWorld Apps

In Struts1, specify the URL path to access the action by <actionpath= the Path property of the/test/helloworld > node. In Struts2, this is not the case, the URL path to the action in the Access struts2 consists of two parts: the name of the package's namespace +action, such as the URL path for accessing this example helloworldaction:/test/ HelloWorld (Note: The full path is: http://localhost: Port/content path/test/helloworld). Alternatively, we can add the. Action suffix to access this action.

<package name= "csdn" namespace= "/test" extends= "Struts-default" >

<action name= "HelloWorld" class= "cn.csdn.action.HelloWorldAction" method= "execute" >

<resultname= "Success" >/WEB-INF/page/hello.jsp</result>

</action>

</package>

Search order for action names

1. Gets the URI of the request path, for example the URL is: http://server/struts2/path1/path2/path3/test.action

2. First look for the package namespace for/path1/path2/path3, and if this package exists, look for the action with the name test in the package, and if the package does not exist, go to step 3;

3. Look for the package namespace for/path1/path2, if the package exists, look for the action with the name test in the package, and if the package does not exist, go to step 4;

4. Looking for the package namespace for/path1, if the package exists, look for the action with the name test in the package, and if the package still does not exist, Go to the default Namaspace package to find the action with the name test (the default namespace is the empty string ""), and if you cannot find it, the page tip cannot find the action.

The default values in the action configuration

<package name= "csdn" namespace= "/test" extends= "Struts-default" >

<action name= "HelloWorld" class= "cn.csdn.action.HelloWorldAction" method= "execute" >

<resultname= "Success" >/WEB-INF/page/hello.jsp</result>

</action>

</package>

1> if no class is specified for action, the default is Actionsupport.

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

3> if the Name property of result is not specified, the default value is success.

the various forwarding types of result in action

<action name= "HelloWorld" class= "Cn.csdn.action.HelloWorldAction" >

<resultname= "Success" >/WEB-INF/page/hello.jsp</result>

</action>

The result configuration is similar to forward in Struts1, but there are several result types available in struts2, common types are: Dispatcher (default), redirect, Redirectaction, plaintext.

The following is an example of the redirectaction result type if the redirect action is under the same package:

<resulttype= "Redirectaction" >helloworld</result>

If the redirected action is under a different namespace:

<resulttype= "Redirectaction" >

<paramname= "ActionName" >helloworld</param>

<paramname= "namespace" >/test</param>

</result>

PlainText: Displays the original file contents, for example: we can use this type when we need to display the JSP file source code as is.

<result name= "source" type= "plaintext" >

<paramname= "Location" >/xxx.jsp</param>

<paramname= "CharSet" >UTF-8</param><!--Specifies the encoding of the read file--

</result>

In result you can also use the ${property name} expression to access the property in the action, and the property name in the expression corresponds to the property in the action. As follows:

<resulttype= "redirect" >view.jsp?id=${id}</result>

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.