Simple Struts2 login function process

development environment

eclipse,jdk1.7.0_55,tomcat7.0.54,struts2.3.16

Download struts2 full jar package

Enter the download address: http://struts.apache.org/download.cgi

If you need other releases then pull down the page to Prior releases

After downloading, unzip it

 Generally, you only need commons-fileuploed-1.3jar, freemarker-2.3.19, ognl-3.0.6.jar, struts2-core-2.3.16.jar, xwork-core-2.3 in the lib folder of the downloaded jar package .16 etc. file

Functional flow chart

Flow Description:

1. Through the browser, run the login interface, click the "Login" button, and submit the user name and password information entered by the user to the server.

2. Read the web.xml configuration file, load the StrutsPrepareAndExecuteFilter, the core controller of Struts2, and intercept user requests.

3. According to the Action in the form submitted by the user, find the corresponding Action configuration in the struts.xml configuration file. Here, the Action configuration with the name attribute value of login will be found, and the intercepted request will be sent to the corresponding LoginAction business class to deal with.

4. The method attribute value of the Action element is not specified in the struts.xml configuration file. At this time, the system will call the default method execute() to complete the processing of the client's login request. If the login is successful, return the success string, otherwise return the input string

5. Find the corresponding mapping in the struts.xml configuration file according to the returned result. In struts When configuring LoginAction in the XML file, <result name= "success" > /success Jsp</result>, therefore, when the execute() method of the LoginAction class returns the "success" string, it turns to success.Jsp page, otherwise go to the login jsp page.

Function realization

1. Create a new Web project LoginDemo

2. Add Struts2jar package

Put commons-fileuploed-1.3jar, commons-logging-1.1.3.jar, commons-io-2.2.jar, commons-lang3-3.1.jar, freemarker-2.3.19, javassist-3.11 in the downloaded jar package. 0.GA.jar, ognl-3.0.6.jar, struts2-core-2.3.16.jar, xwork-core-2.3.16 and other files are copied to the WebRoot/WEB-INF/lib path in the Web application. Yes, then refresh the project

3. Create a new jsp page

Create login.jsp login page and success.jsp success page in WebRoot root directory

login.jsp (login interface)

<%@ page language="java" import="java.util.*"  pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags" %><!-- import struts2 Standard library -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login interface</title>
</head>
<body>
	<!-- submit a request -->	
	<s:form name="form1" action="login"  method="post">
		<s:textfield name="username" label="username"></s:textfield>
		<s:textfield name="password" label="password"/>
		<s:submit value="Log in"/>
	</s:form>
</body>
</html>

success.jsp (success interface)

<%@ page language="java" import="java.util.*"  pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags" %><!-- import struts2 Standard library -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login success interface</title>
</head>
<body>
	<div>
		welcome<s:property value="username"/>,login successful.
	</div>
</body>
</html>

4. Configure web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://JAVA.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Logindemo</display-name>
  <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>
  <!-- configure Struts2 core controller -->
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  	<!-- filter block -->
  	<filter-name>struts2</filter-name>
  	<!-- configure Strust2 The core filter intercepts all user requests -->
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

5. Create a new LoginAction class (Action business class) under the src file

package com.yzpc.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String username;
	private String 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;
	}

	@Override
	public String execute() throws Exception{
//Account is: admin Password: 123, if it is correct, it will return the string success, if it is wrong, it will return input
		if ("admin".equals(username)&&"123".equals(password)) {
			return SUCCESS;
		}else {
			return INPUT;
		}
	}
}

6. Write struts.xml configuration file

Create a struts.xml file in the src folder of the project

<?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="default" namespace="/" extends="struts-default">
		
		<action name="login" class="com.yzpc.action.LoginAction">
			<result name="success">/success.jsp</result>
			<result name="input">/login.jsp</result>
		</action>
		
	</package>
</struts>

  Renderings show

login interface

success interface

  Summarize

After the implementation of the project, I have a more detailed understanding of the specific implementation of the mvc idea of ​​struts2, the basic process and related components

Tags: Java struts2

Posted by dkode on Tue, 26 Jul 2022 22:02:45 +0530