"Struts2" Struts2 Pure manual installation, configuration and HelloWorld, with the latest version of Struts 2.3.20 GA as an example

Source: Internet
Author: User

Many online tutorials on the configuration of Struts2, installation is unclear, a lot of experts think that little white is what understand, many details above the place did not explain clearly, and even in Maven explained above, to know that Struts2 and Maven have no slightest relationship, Can be like other Javaweb plug-ins in Web. XML and start, while copying those packages are not clear, so that in accordance with the various tutorials in Web. XML, then, run tomcat a bunch of errors, always stuck in dispatcher Initialization failed above, half a day can not come. Had to have been configured in the struts of the project, directly copy a web-inf come over, eventually did not learn how to configure STRUTS2. Then, Struts2 's example is a mixture of many international things in the inside, let people confused.


I. BASIC OBJECTIVES

Use eclipse for Java EE to create a Struts2 simple project that, after entering a text box, immediately displays:


It's impossible to do this with you. A user logs in to the system and then checks the table to illustrate Struts2, because STRUTS2 is just a framework of the servlet, which represents just the role of playing JSP with the Java file interaction.


II. Basic Preparation

1. Create a new Web project in Eclipse for Java EE with the. xml file. Do not understand can refer to the "Javaweb" Eclipse for Java EE new Web project Auto-generated XML (click to open link)

2, directly in Struts2 's official website (click Open link) to download the latest version of the STRUTS2, select the full version, in theory, only download lib version can also, but do not know will be less things, and the most important is the author of the network download LIB version no resources, So directly download full version, open speed may be slow, also can put http://222.200.129.45/mirrors.cnnic.cn/apache//struts/2.3.20/ Struts-2.3.20-all.zip This copy to download tool directly inside download.


2. After downloading, unzip the following jar package in Lib into your Web project lib file, do not hold in order to save time, or whatever, the Lib all the package, copied to the network engineering fantasy. This can not start the project, even if the Web. XML is written, there will be dispatcher initialization failed problem. It's probably the reason for the packet collision. Only copy the following nine jars in the past, the author's pro-Test can be successfully started the project.


3, to the Web project, XML file to write the following code, the use of filters, not to say what page to intercept, meaning that all pages are required to implement STRUTS2 features, the role of the Interceptor, specifically, you can see the "Filter" Interceptor filter (click on the Open link), This code is not provided in the official documentation provided by STRUTS2, from the Internet, Struts2 with the Web. Xml method to do, not maven, all this writing, and please identify the online struts1 methods, Struts1 of Web. XML is not able to start struts2:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "><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></web-app>

Get here, struts is configured, configure it to Tomcat inside, you can start the server.


Three, the basic idea

The following is the beginning of the establishment of a HelloWorld Struts2, the final directory structure of the project, such as:



Iv. Process of production

1, first set up a index.jsp page, very simple, a simple can not be a simple form, here is worth noting is the action point is calculation, and then use to El expression, take a moment to pass over the ID value, el expression, you can refer to the " Filter "To solve the encoding problem of post transmission and to use El Expression to simplify the transfer of parameters" (click Open link), very simple thing, just to not write so long JSP code

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
2. Right click the SRC folder and select New file.


3. Enter the XML file in the popup window, locate and must be named Struts.xml, which will be the file that controls the action jump later


4, in Struts.xml write the following code, meaning in the test package, just calculate that action will jump to the test package under the A.java of the B method processing. After processing, if B is success, then jump back to index.jsp

Struts requires that all Java files must be under the package, not exposed to the root directory, and then have to create a new test package, in which a file is written. Some of the books about struts2 are irresponsible, saying that every Java must have a default Execute method, which is not, and you can specify the method yourself.

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts public        "-//apache software foundation//dtd struts Configuration 2.0//en"        
5, A.java code as follows, this Java file I do not want to write so complex, but struts required this to write.

Package Test;import Com.opensymphony.xwork2.actionsupport;public Class A extends Actionsupport {private String ID; Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String B () {System.out.println ("Hello," + ID); return SUCCESS;}}
First, all the Java files corresponding to the action must be introduced into the STRUTS2 package, indicating that this is a struts2 action:

Import Com.opensymphony.xwork2.ActionSupport;
Next, all methods must inherit the Actionsupport class,

And, in order to catch the text of the name= "id" passed by index.jsp, you must define a static member for the ID, and then set the appropriate getter and setter method, which is equivalent to the inside of the servlet

Request.getparameter ("id");

The Getter,setter method is not used in eclipse for Java EE, shortcut key alt+shift+s, or right-click to select Source can also:


Then the shortcut key R, the following interface appears, for the static member automatically create setter and getter as shown below:


Then we define a method B with a string return value, and write a statement that prints to the console, and when the project executes, it prints the variables passed in the console:


A success variable is then returned, which is equivalent to a "success" string, which is already defined in the parent class.


V. Summary

Do here, the whole Struts2 simple HelloWorld finished, return to success, STRUTS2 will put this ID into the request container, the foreground value can be immediately taken. The entire pass value process is as follows, a bit chaotic, but the synthesis of the above production process, we should be able to understand ...


"Struts2" Struts2 Pure manual installation, configuration and HelloWorld, with the latest version of Struts 2.3.20 GA as an example

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.