Use Cases to explain struts2 Workflow

Source: Internet
Author: User

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!

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.