Introduction to struts1 and logon instances

Source: Internet
Author: User

In the previous blog, several servlet versions are used as examples to illustrate the problems to be solved in page Jump Control. In the last article, we mentioned struts1. The purpose of this article is to explain what is struts1, and how to use it.

What is struts1?

My understanding: it conforms to the MVC idea and uses a framework encapsulated by Java secondary development to simplify page request Distribution and redirect. The advantage of stuts1 is that it uses a wide range of configuration files to greatly improve flexibility. Because it has been encapsulated into a framework, it shortens the development time, improves the development efficiency, and uses abstraction in a proper amount, reduced code redundancy, reduced file redundancy, and simpler maintenance.

The interaction between each part of struts1 is as follows:

It is an open-source project. We can go deep into the source code of the framework to further understand struts1 and learn its development and encapsulation ideas, which is very conducive to the improvement of programming capabilities.

Login instance

First, let's take a look at the complete struts1 login instance. The struts1 components involved in this instance are used to explain these components and describe the process of struts1 execution.

File tree


Login. jsp is a simple logon authentication interface, which only verifies the user name and password.

<Form action = "login. do "method =" Post "> User: <input type =" text "name =" username "/> <br/> password: <input type = "text" name = "password"/> <br/> <input type = "Submit" value = "Submit"/> </form>
The username and password used by loginactionform are similar to the entity in Layer 3. This will be detailed in future articles.

Package COM. tgb. struts1; import Org. apache. struts. action. actionform;/******* collect form data * Form attribute values must correspond to get/Set * @ author administrator **/@ suppresswarnings ("serial ") public class loginactionform extends actionform {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 ;}}
Loginaction is used for the logic processing and redirection of Jump requests. Here, it is always redirected to the success tag. From the following configuration file, we can see that success represents login_success.jsp.

Package COM. tgb. struts1; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import javax. swing. actionmap; import Org. apache. struts. action. action; import Org. apache. struts. action. actionform; import Org. apache. struts. action. actionforward; import Org. apache. struts. action. actionmapping;/***** call the business logic and return the redirection information * @ author administrator **/public class loginaction extends action {@ overridepublic actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {loginactionform LAF = (loginactionform) form; string username = LAF. getUserName (); string Password = LAF. getPassword (); usermanager = new usermanager (); try {usermanager. login (username, password);} catch (usernotfoundexception e) {e. printstacktrace (); Request. setattribute ("MSG", "User Name Not Found" + username);} catch (passwordwrongexception e) {e. printstacktrace (); Request. setattribute ("MSG", "Incorrect password");} Return Mapping. findforward ("success ");}}

Usermanager simulates the functions of model and controller.

Package COM. tgb. struts1;/****** logic processing. Here, we use the admin verification example of a short answer * @ author javasab_000 **/public class usermanager {public void login (string username, string password) {If (! "Admin". Equals (username) {Throw new usernotfoundexception ();} If (! "Admin". Equals (password) {Throw new passwordwrongexception ();}}}

Configuration File

Web. XML is the configuration file read by Tomcat. You need to specify the request type to enter struts and the class to process the request.

  <servlet>    <servlet-name>action</servlet-name>    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>    <init-param>      <param-name>config</param-name>      <param-value>/WEB-INF/struts-config.xml</param-value>    </init-param>    <init-param>      <param-name>debug</param-name>      <param-value>2</param-value>    </init-param>    <init-param>      <param-name>detail</param-name>      <param-value>2</param-value>    </init-param>    <load-on-startup>2</load-on-startup>  </servlet>  <!-- Standard Action Servlet Mapping -->  <servlet-mapping>    <servlet-name>action</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping>

Struts-config.xml this is struts read configuration file, in this configuration file, you need to set by intercepting the URL specified processing class, steering information, form encapsulation class and other information, such as scope and so on.

<!DOCTYPE struts-config PUBLIC          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"><struts-config><form-beans><form-bean name="loginForm"  type="com.tgb.struts1.LoginActionForm" /></form-beans><action-mappings><action path="/login" type="com.tgb.struts1.LoginAction"  name="loginForm"  scope="request"><forward name="success"  path="/login_success.jsp"/><forward name="error"  path="/login_error.jsp" /></action></action-mappings></struts-config>

Running result

If all the inputs are admin, the logon is successful:

Process Analysis

The sequence chart analysis process is as follows:

Let me explain this process:

  • Enter the user name and password and submit the request.
  • Tomcat receives the request and determines whether the type specified by struts (*. Do) is determined based on the read web. xml)
  • If it is a type specified by struts, the request is forwarded to the specified class file (actionservlet. Java)
  • The actionservlet intercepts the request URL and matches the form and action classes in the Struts-config file (loginactionform. Java and loginaction. Java)
  • Put the submitted form data into Form (loginactionform)
  • Execute the specified action (loginaction) to obtain the jump information (login_success.jsp)
  • Actionservlet execution jump
  • Rendering pages to users

This is only the execution process of struts from a macro perspective. This is only an external manifestation. How can we implement it internally? The next blog introduces you to the struts1 Framework Code. For more information, see pre-load actinoservlet.

For more related blogs, go to the Summary of progressive struts1 (8).


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.