STRUTS2 development environment to build a simple login function example _java

Source: Internet
Author: User

The first is to build Struts2 environment.

First step download Struts2
Go to struts website http://struts.apache.org/download STRUTS2 components.
Up to now, the latest version of STRUTS2 is 2.3.1.3, download Struts-2.3.16.3-all.zip, unzip, put.

Step two to create a new Web project and import a jar package
Create a new Web Project in Myeclispe, then find the unpacked Struts2 package, find Struts2-blank.war inside Apps folder, unzip the war file, and web-inf\ All the jar files under the Lib directory are copied to the Webroot\web-inf\lib directory of the new WEB project.

Third Step configuration Web.xml
Locate the Web.xml file in the project's webroot\web-inf\ directory without creating a new Web.xml file, adding the following code inside:

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

An example of a complete Web.xml file is given below:

<?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/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> &LT;WELCOME-FILE&GT;default.jsp</welcome-file> </welcome-file-list> </web-app>
 

Fourth Step configuration Struts.xml
In the project's SRC directory to find the Struts.xml file, there is no new one, the following code:

<?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 in this-->
 </package>
</struts>

To this end, the STRUTS2 development environment is built.

A login Page instance is shown below.

The first step is to modify index.jsp
Modify the index.jsp to make the login interface.
Here is the code for INDEX.JSP:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <!

DOCTYPE html>
 
 

The following are the effects of index.jsp in the browser:

The second step is to write a class that verifies the account and password
New Logaction class, Let it inherit the Com.opensymphony.xwork2.ActionSupport class, noting that the name property of the two input boxes in index.jsp is 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, set methods.

Then, write the Execute method, validate the account and password in the Execute method, and return the string type result, which executes automatically when the action class is invoked.

Here is the complete code for 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 is automatically executed when the action class is invoked,
  * if the account = "admin" and the password = "123456", return success
  * Otherwise return error *
  * Public
 String Execute () {
  if (username.equalsignorecase ("admin") && password.equalsignorecase (" 123456 ")) {return
   SUCCESS;
  }
  else return
   ERROR;
 }
}

The above returns the success and returns the error what meaning, after again said.

Third Step configuration Struts.xml
The second step is to write the action class, step three to configure the action into 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 property of the action label is login, which must match the action attribute of the form tag in index.jsp, and the class attribute fills in the full name of the Logaction class.

<result name= "Success" >success.jsp</result> this label means that when the Execute method of the Logaction class returns to success, Page jumps to success.jsp; Similarly, <result name= "Success" >success.jsp</result> This tag means that when the Execute method of the Logaction class returns an 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 "class=" com.lidi.struts.action.LogAction ">
   <result name= "Success" >success.jsp</result>
   <result name= "error" >error.jsp</result>
  </action >
 </package>

</struts>

Here to use success.jsp and error.jsp, in the project to create the two files, success.jsp means that after the successful login page, the above display login account and password, error.jsp sign the failure of the page, the above display error prompts on the good, 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"%> represents a reference to the Struts tag library
<s:property value= "username"/> is a struts tag that displays the account number that the login page passes over.

error.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <!

DOCTYPE html>
 
 

Step Fourth run
After you configure Struts.xml, restart the server, and then view the effect in the browser.
Enter account and password separately and log in, if the account number and password are admin and 123456 respectively, the page will show
Welcome Admin, Login success!
Otherwise it will show
Login failed! User name or password is wrong!

Brief analysis of the fifth Step program operation principle
Users fill in the account password and click Login, the browser will request form tag action attribute inside the link, that is, login. In the server, the filter intercepts the login request, finds the Name=login action in Struts.xml, and then finds the class of the action's class attribute. That is, com.lidi.struts.action.LogAction, then instantiate a Logaction object, and assign parameters username and password to the object's username and Passwrod attributes (which is why logaction The two property names of the class must be the same as the Name property of the two text boxes in index.jsp and must add their get and set methods, and then execute the object's Execute method and return a string that, if returned success string, looks for the corresponding acti in the Struts.xml On <result> label the Name property equals the success <result> tag, and the page is transferred to the page configured in the label success.jsp.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.