Note: all the images in this article are clearly indicated in the attachment.
Install jdk1.6, Tomcat 6, and myeclipse.
Second, download struts2
Step 1: Go to struts21 official website http://struts.apache.org/2.1.6/index.html
Click download now in Figure 1 below to download the full output in Figure 2. Decompress the package after download;
Step 2;
The integration of myeclipse tomcat is relatively simple, not to mention; create a web project named struts2;
Step 3;
Decompress the following six files in lib under the downloaded struts2-1-6 directory:
Commons-logging-1.0.4.jar
Freemarker-2.3.8.jar
Ognl-2.6.11.jar
Struts2-core-2.0.6.jar
Xwork-2.0.1.jar
And (because it is a struts2-1-6 version. So this file is also essential)
Commons-fileupload-1.2.1
Then paste it to WebRoot/WEB-INF/lib;
Step 4:
Create a login. jsp file in the WebRoot directory.
The Code is as follows:
Login. jsp
Jsp code
- <% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
- <%
- String path = request. getContextPath ();
- String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
- %>
-
-
-
-
-
-
- My JSP 'login. jsp 'starting page
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Step 5:
Modify the web. xml file under the WEB-INF
The Code is as follows:
Web. xml
Xml Code
-
- 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>
-
-
- Struts2
-
-
- Org. apache. struts2.dispatcher. FilterDispatcher
-
-
-
-
-
- Struts2
-
- /*
-
-
Step 6:
Create action
Create the com. test. action package in the src directory.
Create an action in the package as follows:
LoginAction. java
Java code
- Package com. test. action;
-
- Public class LoginAction
- {
-
-
- // The getter and setter methods match the client information based on the method name.
- 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;
- }
-
- Public String execute () throws Exception
- {
- Return "success ";
- }
-
- // Corresponding to
- Private String username;
- Private String password;
- }
Step 7:
Struts configuration file
The following code creates a struts. xml file in the src directory:
Xml Code
-
- "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
- Http://struts.apache.org/dtds/struts-2.0.dtd>
-
-
-
-
-
-
- /Result. jsp
-
-
-
Note: The sentence in struts. xml is
! DOCTYPE struts PUBLIC
"-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN"
Http://struts.apache.org/dtds/struts-2.0.dtd>
The red sentence may report an error. The solution is to remove the "http: //" text. I don't know what else to do. If you have any knowledge, please give me some advice;
Step 8;
Create result File
The following code creates a result. jsp file in the WebRoot directory:
Jsp code
- ]
- <% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
- <%
- String path = request. getContextPath ();
- String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
- %>
-
-
-
-
-
-
- My JSP 'result. jsp 'starting page
-
-
-
-
-
-
-
-
-
-
-
- Helloworld
- Username: $ {requestScope. username}
- Password: $ {requestScope. password}
-
-
So far, the code writing has been completed. Next is release;
Right-click the name of the struts2 project, select myeclipse from the menu, and select add and remove project ...... Then, figure 3 is displayed.
Select a project and click add to publish to the specified tomcat,
Finally, open the browser. In the browser http: // localhost: 8080/struts2/login. jsp