The first is to build a Struts2 environment.
Step 1 Download Struts2
Go to the Struts official website http://struts.apache.org/ to download the Struts2 component.
As of now, the latest version of struts2 is 2.3.1.3. Download struts-2.3.16.3-all.zip, unzip, and leave it.
The second step is to create a new Web Project and import the jar package. Create a new Web Project in MyEclispe. Then find the unzipped Struts2 package. Find struts2-blank.war in the apps folder. Unzip this WAR file and copy all the jar files in the WEB-INF/lib directory to the WebRoot/WEB-INF/lib directory of the newly created Web Project.
Step 3 to configure web.xml
Find the web.xml file in the project's WebRoot/WEB-INF/ directory. Create a new web.xml file without it. Add the following code to it:
<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>
Here is an example of a complete web.xml file:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>web1</display-name> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list></web-app>
Step 4 : Configure struts.xml
Find the struts.xml file in the project's src directory. If you don't have it, create a new one. The code inside is as follows:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <package name="main" extends="struts-default"> <!-- Configure action here --> </package></struts>
At this point, the Struts2 development environment has been built.
The following demonstrates an instance of the login page .
The first step is to modify Index.jsp
Modify Index.jsp to create a login interface.
Here is the code for index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML><html> <head> <title>Login</title> </head> <body> <form action="login" method="post"> Login<br /> Account: <input type="text" name="username" /><br /> Password: <input type="password" name="password" /><br /> <input type="submit" value="Login" /> </form> </body></html>
Here is the effect of Index.jsp in the browser:
The second step is to write a class that validates the account and password to create a new LogAction class, so that it can inherit the com.opensymphony.xwork2.ActionSupport class. Note that the name attributes of the two input boxes in index.jsp are username and password, respectively, so the LogAction class must contain the following two properties:
private String username
private String password
And they must write their get and set methods.
Then, write the execute method, verify the account and password in the execute method and return the result of String type. The execute method will be automatically executed when the Action class is called.
Here is the complete code of LogAction.java:
package com.lidi.struts.action;import com.opensymphony.xwork2.ActionSupport;public class LogAction extends ActionSupport { private static final long serialVersionUID = 1L; private String username;//Account private String password;//Password//Getters & Setters 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; } /** * The execute method will be automatically executed when the Action class is called, * If the account number="admin" and password="123456", it will return SUCCESS * Otherwise, it will return ERROR */ public String execute(){ if(username.equalsIgnoreCase("admin") && password.equalsIgnoreCase("123456")){ return SUCCESS; } else return ERROR; }}What does the above mean to return SUCCESS and return ERROR? Let's talk about it later.
Step 3 to configure struts.xml
The second step is to write the Action class, and the third step is to configure the Action into struts.xml, open struts.xml, and add the following code to the package tag:
<action name="login"> <result name="success">success.jsp</result> <result name="error">error.jsp</result></action>
The name attribute of the action tag is login, which must be consistent with the action attribute of the form tag in index.jsp. The class attribute fills in the full name of the LogAction class.
<result name="success">success.jsp</result> This tag means that when the execute method of the LogAction class returns SUCCESS, the page jumps to success.jsp; similarly, the <result name="success">success.jsp</result> This tag means that when the execute method of the LogAction class returns ERROR, the page jumps to error.jsp.
The complete struts.xml code is as follows:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <package name="main" extends="struts-default"> <action name="login"> <result name="success">success.jsp</result> <result name="error">error.jsp</result> </action> </package></struts>
Success.jsp and error.jsp are used here. Create these two files in the project. Success.jsp represents the page after login is successful. The account and password for login are displayed. The error.jsp represents the page after login is failed. The error message is displayed. Their codes are as follows:
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML><html> <head> <title>Login successfully</title> </head> <body> Welcome <s:property value="username" />, login successfully! <br /> </body></html>
<%@ taglib prefix="s" uri="/struts-tags"%> means referring to the struts tag library
<s:property value="username" /> is a struts tag, which is used to display the account passed by the login page.
error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML><html> <head> <title>Login failed</title> </head> <body>Login failed! Incorrect username or password! </body></html>
Step 4: After running the configuration struts.xml, you must restart the server and then view the effect in the browser.
Enter your account and password and log in. If the account and password are admin and 123456 respectively, the page will display welcome admin and log in successfully!
Otherwise, the login failure will be displayed! Incorrect username or password!
Step 5: The principle of the program operation is briefly analyzed. After the user fills in the account password and clicks to log in, the browser will request the link in the form tag action attribute, that is, login. In the server, the filter intercepts the login request, and will look for the action of name=login in struts.xml, then find the class corresponding to the class attribute of this action, that is, com.lidi.struts.action.LogAction, then instantiate a LogAction object, and assign the parameters username and password to the username and password of this object respectively (this is why LogAc The two attribute names of the tion class must be the same as the name attributes of the two text boxes in index.jsp, and their get and set methods must be added), then execute the execute method of this object and return a string. If the SUCCESS string is returned, look for the name attribute in the corresponding action <result> tag in struts.xml that is equal to the success <result> tag, and turn the page to the page configured in the tag.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.