This article describes a workflow of Struts2 through an example.
First define a login page login. jsp:
[Java]
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> logon page </title>
</Head>
<Body>
<S: form action = "loginUser">
<S: label value = "Logon page"> </s: label>
<S: textfield name = "user. userName" label = "userName:"> </s: textfield>
<S: textfield name = "user. name" label = "name:"> </s: textfield>
<S: password name = "user. password" label = "Logon password:"> </s: password>
<S: submit value = "Logon"> </s: submit>
</S: form>
</Body>
</Html>
Define a welcome page for successfully logging on to welcome. jsp:
[Java]
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<% @ Taglib prefix = "s" uri = "/struts-tags" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> welcome page </title>
</Head>
<Body>
Welcome <FONT color = "red" style = "font-weight: bold"> "<s: property value =" user. name "/>" </FONT> to log on to the system
</Body>
</Html>
Define a User entity class User. java:
[Java]
Package org. ml. bean;
/**
* User entity class
*
* @ Author MuLing
*
*/
Public class User {
Private String userName; // User Name
Private String name; // User name
Private String password; // User 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;
}
Public void setName (String name ){
This. name = name;
}
Public String getName (){
Return name;
}
}
Define an Action: UserAction. java
[Java]
Package org. ml. action;
Import org. ml. bean. User;
Import com. opensymphony. xwork2.ActionSupport;
/**
* Actions of user entities
*
* @ Author MuLing
*
*/
Public class UserAction extends ActionSupport {
Private static final long serialVersionUID = 1L;
Private User user;
/**
* Custom login Method
*
* @ Return
*/
Public String login (){
If ("admin". equals (user. getUserName () & "admin". equals (user. getPassword ())){
Return SUCCESS;
} Else {
Return INPUT;
}
}
/**
* @ Param user the user to set
*/
Public void setUser (User user ){
This. user = user;
}
/**
* @ Return the user
*/
Public User getUser (){
Return user;
}
}
The core configuration of Struts2 is as follows:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<Struts>
<Package name = "user" extends = "struts-default">
<Action name = "loginUser" class = "org. ml. action. UserAction" method = "login">
<Result name = "success">/welcome. jsp </result>
<Result name = "input">/login. jsp </result>
</Action>
</Package>
</Struts>
The web. xml configuration is as follows:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
<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> </web-app>
The above is the content of all the files involved in this example. The following describes the effect:
To log on:
Logon successful:
The above logon has been successful. Let's take a look at the logon process:
The above is a personal understanding. You are welcome to correct it!