Use Struts1.x to implement user login instance

1. First, we need to import the jar package required by Struts1 into the project. The required jar package is as follows:

write picture description here


2. Create a new Login.jsp and place it in the WebContent directory.


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%  
    String basePath = request.getContextPath();  
%> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>
    <h1>登陆页面</h1>  
    <hr>  
    <form action="<%=basePath %>/Login.do" method="post" >  
        userName:<input id="name" name="name" type="text" /><br>  
        passWord:<input id="password" name="password" type="password" /><br>  
        <input type="submit" id="submit" name="submit" value="submit" />  
    </form> 
</body>
</html>

3. Create a new LoginSuccess.jsp and place it in the WebContent directory as well.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>登录成功</title>
</head>
<body>
    <h1>登录成功页面!</h1>
    <h3>欢迎[<%=request.getAttribute("name") %>]</h3>
</body>
</html>

4. Create a new LoginFailed.jsp and place it in the WebContent directory as well.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>登录失败</title>
</head>
<body>
    <h1>登录失败!</h1>
    请返回<a href="/StrutsDemo/Login.jsp">登录页面。</a>
</body>
</html>

5. Write AactionForm to store the name and password of the logged-in user. Note that inheritance must be made here org.apache.struts.action.ActionForm. Mainly the form corresponding to Login.jsp.
But it must be noted that the field name in this Form must be consistent with the name of the form in Login.jsp in the foreground!

code show as below:

package com.lanp.webapp.form;

import org.apache.struts.action.ActionForm;  

public class LoginActionForm extends ActionForm{

    private String name;
    private String password;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

6. Write Action, named LoginAction, which is the Action for the user to log in, and must inherit the Action class. In this LoginAction, we rewrite the execute() method. This execute() method is actually similar to the Service() method in Servlet, or the DoPost() and DoGet() methods.

code show as below:

package com.lanp.webapp.action;

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

import org.apache.struts.action.Action;  
import org.apache.struts.action.ActionForm;  
import org.apache.struts.action.ActionForward;  
import org.apache.struts.action.ActionMapping;  

import com.lanp.webapp.form.LoginActionForm;  

public class LoginAction extends Action{

    @Override  
    public ActionForward execute(ActionMapping mapping, ActionForm form,  
            HttpServletRequest request, HttpServletResponse response)  
            throws Exception {  
        String path = "error";  
        LoginActionForm loginActionForm = (LoginActionForm)form;  
        String userName = loginActionForm.getName();
        String passWord = loginActionForm.getPassword();

        if("admin".equals(userName) && "admin".equals(passWord)) {  
            path = "success";  
            request.setAttribute("name", userName);  
        } else {  
            path = "error";  
        }  
        return mapping.findForward(path);  
    } 
}

7. The next step is to configure the two files, web.xml and struts-config.xml, which are the core of the entire Struts.

  • Configure struts-config.xml. Create a new conf file in the WEB-INF directory and put struts-config.xml in it. struts-config.xml mainly configures the correspondence between Action and ActionForm. All configuration parameters must be placed under <struts-config>this root tag.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">  
<struts-config>
    <form-beans>
        <!-- name是ActionForm的名字,是唯一标示。在action中会用到,是关联action的唯一标示。 -->
        <!-- type是ActionForm所在路径,是由包名+类名组成。 -->
        <form-bean name="LoginActionForm" type="com.lanp.webapp.form.LoginActionForm">
        </form-bean>
    </form-beans>

    <action-mappings>
        <!-- path是Login.jsp中表单action提交的地址,也就是将要访问的action资源。
        name是action的名字,用来关联某个表单,是唯一标示。
        type是action的路径,由包名+类名组成。
        scope表示ActionForm存在的范围,有session和request两种,默认是session。
        forward是路径跳转 -->
        <action path="/Login" 
        name="LoginActionForm" 
        type="com.lanp.webapp.action.LoginAction"
        scope="request">
        <forward name="login" path="/Login.jsp"></forward>
        <forward name="success" path="/LoginSuccess.jsp"></forward>
        <forward name="error" path="/LoginFailed.jsp"></forward>
        </action>
    </action-mappings>

</struts-config>
  • Configure web.xml, this file is mainly to configure ActionServlet. ActionServlet is also a Servlet in essence, but the ActionServlet here is the general controller of Struts1, which comes with Struts1, so we don't need to write it manually. ActionServlet is also the entry point of the entire Struts. It is created when Struts is accessed for the first time, and calls the init method in ActionServlet to initialize some parameters. In fact, this process is to read the parameters configured in struts-config.xml to initialize resources.
<?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>StrutsDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

<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/conf/struts-config.xml</param-value>  
    </init-param>     
    <load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

8. Finally, paste a screenshot of the entire project:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325476764&siteId=291194637