Recently I learned the struts2 framework. In fact, many technologies are not difficult. The reason why I think it is difficult is that I have been wandering around the periphery and cannot find the entrance to open it. It is easy to learn after finding an entrance. This article uses the most detailed steps to open this portal for you in the most foolish way. As long as you have little java basics, you can do it with me!
Prepare three items:
My eclipse 8.5
Tomcat 6.0
Struts2.0.14 my version is earlier. You can use the latest version. There may be some differences.
========================================================== ==================================
The overall project directory structure is as follows:
========================================================== ======================================
Step 3:
Create a web project in myeclipse, named struts2 in new ---- web project ----
Step 1:Import related packages
After downloading the struts2 directory, find several jar packages, because there are many jar packages under \ Lib under struts2. We only need a few common ones for the time being.
Struts2-core-2.1.6.jar
Freemarker-2.3.13.jar
Commons-logging-1.0.4.jar
Ognl-2.6.11.jar
Xwork-2.1.2.jar
Commons-fileupload-1.2.1.jar
To import the package, copy the preceding files, find the struts2 -- webroot --- lib folder, and right-click --- paste (paste Ctrl + V) OK.
Step 2To import the Web. xml file.
Configure the Web. xml file. Expand the project and you will see this file. Webroot/lib/Web. xml
Note that different struts versions are slightly different. This configuration is in struts2.0.14.
<? XML version = "1.0" encoding = "UTF-8" ?>
< Web-app ID = "Webapp_9" Version = "2.4" Xmlns = "Http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "Http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< Display-name > Struts Blank </ Display-name >
< Filter >
< Filter-name > Struts2 </ Filter-name >
< Filter-class > Org. Apache. struts2.dispatcher. filterdispatcher </ Filter-class >
</ Filter >
< Filter-Mapping >
< Filter-name > Struts2 </ Filter-name >
< URL-Pattern > /* </ URL-Pattern >
</ Filter-Mapping >
</ Web-app >
If the struts package you downloaded is complete, it contains an instance. See the web. xml file configuration in the instance.
Step 3: Create a struts. xml file
Create the strutx. xml file under the SRC folder.
<?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>
Add content here later
</Struts>
Step 1The login. jsp file configuration has been completed. The following describes how to compile the login function.
To create a login. jsp file in the webroot folder, write the user logon page.
< % @ Taglib Prefix = "S" Uri = "/Struts-tags" % > <! -- The following content is written using the strurs2 tag, so the struts2 tag library should be introduced here -- >
</ Body >
< S: Form Action = "Login" >
< S: textfield Name = "Username" Label = "Username" > </ S: textfield >
< S: Password Name = "Password" Label = "Password" > </ S: Password >
< S: Submit Label = "Submit" > </ S: Submit >
</ S: Form >
</ Body >
Step 2Write loginaction. JavaProgram
Create a package in the SRC Folder: COM. Test. Action
Create the loginaction. Java file under the package. The content is as follows:
Package Com. Test. Action;
Import Com. opensymphony. xwork2.actionsupport;
// First, this class inherits the actionsupport class.
Public Class LoginactionExtends Actionsupport {
Private String username;
Private String password;
Public String GetUserName (){
Return Username;
}
Public Void Setusername (string username ){
This . Username = username;
}
Public String GetPassword (){
Return Password;
}
Public Void Setpassword (string password ){
This . Password = password;
}
// Verify that the user must wait for the hello password to log on to word to return to the success page. Of course, it will not be written in real development. Here is just a simulation.
Public String execute () Throws Exception
{
If ("Hello". Equals ( This . GetUserName (). Trim () & "word". Equals ( This . GetPassword (). Trim ()))
{
Return "Success ";
}
Else
{
This . Addfielderror ("username", "usernmae or Password error ");
Return "Failer ";
}
}
// The following content determines that the user name cannot be blank
@ Override
Public Void Validate ()
{
If ( Null = This . GetUserName () | "". Equals ( This . GetUserName (). Trim ()))
{
This . Addfielderror ("username", "username error ");
}
If ( Null = This . GetPassword () | "". Equals ( This . GetPassword (). Trim ()))
{
This . Addfielderror ("password", "username error ");
}
}
}
Step 3, Success. jsp
Create a success. jsp page in webroot. If the user logs on successfully, the page is displayed.
<Body>
Success !!
</Body>
Step 4, Configure my struts2.xml file below
Insert the following configuration between <struts>... </struts>:
< Package Name = "Struts2" Extends = "Struts-Default" Namespace = "/" >
< Action Name = "Login" Class = "Com. Test. Action. loginaction" >
< Result Name = "Success" > /Result. jsp </ Result >
< Result Name = "Failer" > /Login. jsp </ Result >
</ Action >
</ Package >
========================================================== ============
The above process is completed. Configure Tomcat below
Menu Bar windows ----> preferences ----> Search for Tomcat
Click Browse..., find the Tomcat directory, and click OK.
In the myeclipse window, click the servers tab to start Tomcat 6.
Note: We may create multiple projects here. How can we let Tomcat identify the projects we need to start.
Right-click Tomcat and choose add deployment
In the displayed dialog box, select the name of the project to be started (struts2)
Okay! Now we start Tomcat, open the browser and enter: http: // localhost: 8080/struts2/login. jsp
If there is no error, the page will be displayed as follows:
Process Analysis:
Next let's take a look at the processing process of the program.
1. on the user logon page (login. JSP), find the configuration file (struts. XML) corresponding action attribute. The form action = "login" must be the same as the action name = "login.
2. Then find the loginaction. Java program with the class = "com. Test. Action. loginaction" of the action.
3. After loginaction. Java is processed, the success and failer results are returned to the Struts. xml file.
4. Struts. XML according to the configuration in <result> </result>, if the result is success, the result. jsp page is displayed. If the result is failer, the login. jsp page is displayed.