Struts2 Study Notes (1): setting up the struts2 Development Environment

Source: Internet
Author: User

Struts2YesWebwork2The Foundation developed from. Like struts1, struts2 also belongsMVCFramework. However, it should be noted that, although the name differences between struts2 and struts1 are not very largeCodeThe writing style is almost different. So why should we release struts1?

Struts2. It is mainly because struts2 has the following advantages:

1In terms of software design, struts2 is not closely coupled with Servlet APIs and Struts APIs as struts1 does. The application of struts2 does not depend on servlet APIs and Struts APIs. This design of struts2 is a non-intrusive design, while struts1 is an intrusive design.

2. Struts2 provides an interceptor that can be programmed with AOP to implement functions such as permission interception.

3 . Strut2 provides a type converter. We can convert special request parameters to the desired type. In struts1, if we want to implement the same function, we must register the type converter with the underlying implementation of beanutil in struts1.

4. Struts2 supports multiple presentation layer technologies, such as JSP, freemarker, and velocity.

5. Struts2 input validation can be performed on the specified method, solving the long-term pain of struts1

6 Provides global resource file management, including the scope of the package, and the scope of the action.

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

Find the JAR file to be used to develop the struts2 application.

    • Struts2-core-2.x.x.jar: Core class library of struts 2 Framework

    • Xwork-core-2.x.x.jar: Xwork class library, built on Struts 2

    • Ognl-2.6.x.jar: Object graph Navigation language. The struts2 framework reads and writes the attributes of an object.
    • Freemarker-2.3.x.jar: Struts 2 UI tag template written using freemarker
    • Commons-logging-1.x.x.jar: The log package produced by ASF. Struts 2 framework uses this log package to support logging of log4j and JDK 1.4 +.
    • Commons-fileupload-1.2.1.jarFile Upload Component. This file must be added after version 2.1.6.

2. Compile the struts2 configuration file

Struts2 default configuration file is struts. XML, the file needs to be stored in the WEB-INF/classes (Put it under the SRC file and automatically load it to the classes during deployment.), The configuration template of this file is as follows:

<?XML version = "1.0" encoding = "UTF-8"?>
<!Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd">
<Struts>
<! --Structs2 configuration Template-->
</Struts>

 

3. Add struts2 MVC Framework STARTUP configuration to Web. xml

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

In the init () method of strutsprepareandexecutefilter, the default configuration file struts. XML in the class path is read to complete initialization.

Note: struts2 reads struts. the XML content is stored in the memory in the form of JavaBean. In the future, struts2 will use the data in the memory for each request processing, instead of reading struts every time. XML file

Finally, it is deployed to Tomcat for testing. once started, the building is successful.

In Struts-2.3.1.2, the minimum jar package is:

Commons-fileupload-1.2.2.jar

Commons-io-2.0.1.jar

Commons-logging-1.1.1.jar

Freemarker-2.3.18.jar

Ognl-3.0.4.jar

Struts2-core-2.3.1.2.jar

Xwork-core-2.3.1.2.jar

Commons-lang-2.5.jar

Javassist-3.11.0.GA.jar
Refer to the war package configuration in the app directory

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.