Struts2 Development Environment setup, and a simple login function instance, struts2 instance

Source: Internet
Author: User

Struts2 Development Environment setup, and a simple login function instance, struts2 instance

The first step is to build the Struts2 environment.

Step 1 download Struts2
Go to Struts official website Struts2 components.
To date, the latest version of struts2is Download, decompress it, and put it.

Step 2: create a Web Project and import the jar package
Create a Web Project in MyEclispe, then find the decompressed Struts2 package, find the struts2-blank.war In the apps folder, decompress the WAR file, copy all the jar files under the WEB-INF \ lib directory to the WebRoot \ WEB-INF \ lib directory of the new Web Project.

Step 3 configure web. xml
Find the web. xml file in the WebRoot \ WEB-INF \ directory of the project, create a new web. xml file, add the following code in it:

<filter>    <filter-name>struts2</filter-name>    <filter-class></filter-class></filter><filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern></filter-mapping>

The following is an example of a complete web. xml file:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=""    xmlns=""    xsi:schemaLocation=""    id="WebApp_ID" version="3.1">    <display-name>web1</display-name>    <filter>        <filter-name>struts2</filter-name>        <filter-class></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 src directory of the project and create a new file. The Code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" ""> <struts> <package name = "main" extends = "struts-default "> <! -- Configure action here --> </package> </struts>

At this point, the Struts2 development environment has been set up.

The following shows an instance on the logon page.

Step 1 modify Index. jsp
Modify Index. jsp to make the logon interface.
The code for index. jsp is as follows:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html> 

The following figure shows the effect of Index. jsp in the browser:

Step 2: Write the account and password verification class
Create a LogAction class to inherit from the com. opensymphony. xwork2.ActionSupport class. Note that the name attributes of the two input boxes in index. jsp are username and password. Therefore, the LogAction class must contain the following two attributes:
Private String username
Private String password
They must also write their get and set methods.

Then, compile the execute method, verify the account and password in the execute method, and return the String type result. The execute method is automatically executed when the Action class is called.

The complete code of LogAction. java is as follows:

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 is automatically executed when the Action class is called. * If the account is "admin" and the password is "123456 ", SUCCESS * is returned. Otherwise, ERROR */public String execute () {if (username. equalsIgnoreCase ("admin") & password. equalsIgnoreCase ("123456") {return SUCCESS;} else return ERROR ;}}

What does the above return SUCCESS and Return ERROR mean? I will talk about it later.

Step 3 configure struts. xml
Step 2: Write the Action class. Step 3: configure the Action to struts. xml, open struts. xml, and add the following code to the package Tag:

<action name="login" class="com.lidi.struts.action.LogAction">    <result name="success">success.jsp</result>    <result name="error">error.jsp</result></action>

The name attribute of the action label is login, which must be consistent with the action attribute of the form tag in index. jsp. The class attribute is the full name of the LogAction class.

<Result name = "success"> success. jsp </result>: When the execute method of the LogAction class returns SUCCESS, the page jumps to success. jsp; similarly, <result name = "success"> success. jsp </result> indicates 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"    ""><struts>    <package name="main" extends="struts-default">        <action name="login" class="com.lidi.struts.action.LogAction">            <result name="success">success.jsp</result>            <result name="error">error.jsp</result>        </action>    </package></struts>

Success is used here. jsp and error. jsp: create these two files in the project, success. jsp indicates the page after successful logon. The Logon account and password are displayed, error. jsp indicates the page after logon failure. The error prompt is displayed. Their code is as follows:
Success. jsp

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! Doctype html> 

<% @ Taglib prefix = "s" uri = "/struts-tags" %> indicates referencing the struts tag Library
<S: property value = "username"/> is a struts tag used to display the account passed on the logon page.

Error. jsp

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html> 

Step 4 run
After configuring struts. xml, restart the server and view the effect in the browser.
Enter the account and password respectively and log on. If the account and password are respectively admin and 123456, the page will display
Welcome to admin. logon successful!
Logon Failed! Incorrect username or password!

Step 5 program running Principle Analysis
After you enter the account password and click log on, the browser will request the link in the form tag action attribute, that is, login,
In the server, when the filter intercepts the login request, it will look for struts. in xml, find the class corresponding to the class attribute of this action, that is, com. lidi. struts. action. logAction, instantiate a LogAction object, and assign the username and password parameters to the username and passwrod attributes of the object respectively (that is why the two attribute names of the LogAction class must be the same as the index. in jsp, the name attributes of the two text boxes are the same, and their get and set methods must be added.) then, execute the execute method of the object and return a string. If the SUCCESS string is returned, search for struts. the <result> tag in the <result> tag corresponding to the action in xml is the <result> tag with the name attribute equal to success, and the page is forwarded to the success page configured in the tag. jsp.

Related Article

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