2_struts2_简单用户登录的实现

一、struts2简单登录的实现

  1.1新建一个web项目

  1.2导入所需jar包

  1.3在web.xml配置文件中配置struts过滤器  

<?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>1_struts_hello</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>
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>

  1.4编写处理类(action)  

package edu.aeon.struts2.action;

public class LogonAction {
    private String username;
    private String pw;
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPw() {
        return pw;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }

    public String execute(){
        if(username.equals("aeon")&&pw.equals("aeon")){
            System.out.println("logonSuccess...");
            return "logonSuccess";
        }else{
            System.out.println("logonSuccessFailed...");
            return "logonFailed";
        }
    }
}

   1.5在struts.xml文件中配置提交到处理类处理方法的的映射  

<?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="logonAction" class="edu.aeon.struts2.action.LogonAction">
            <result name="logonSuccess">/index.jsp</result>
            <result name="logonFailed">/logon.jsp</result>
        </action>
    </package>
</struts>

  1.6登录页面  

<%@ 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>
    <form action="logonAction.action">
    用户名:<input name="username" type="text" /><br>
    密码:<input name="pw" type="password" /><br>
    <button type="submit">登录</button>
    </form>
</body>
</html>

  1.7登录成功显示界面  

<%@ 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>
${username},恭喜您登录成功!
</body>
</html>

  1.8测试

    1.8.1用户登录界面

    

    1.8.2输入非正确密码后结果会跳转到上面登录界面

    1.8.3输入正确密码后结果界面截图

     

 

    

      

猜你喜欢

转载自www.cnblogs.com/aeon/p/10231277.html