欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2框架实现-用户登录功能

程序员文章站 2022-05-28 16:53:53
...

用户登录的实现步骤

  1. 编写login.jsp(用于登录)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
   <div>
       <label>用户名:</label>
       <input type="text" name="userName">
   </div>
   <div>
       <label>密   码:</label>
       <input type="password" name="passWord">
   </div>
   <div>
       <input type="submit" value="登录">
   </div>
</form>


</body>
</html>

	```

2. success.jsp页面(用于展示登录成功的页面)
```java
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>登录成功</title>
</head>
<body>
   <h1>
       欢迎您:${userName}
   </h1>

</body>
</html>

  1. errorLogin.jsp页面(用于展示登录失败后的页面,一些提示信息)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
   <title>登录失败</title>
</head>
<body>
<h1>
   不好意思,用户名或密码错误
</h1>

</body>
</html>

  1. 控制器LoginAction的编写
package com.lty.web;

import com.opensymphony.xwork2.Action;

public class LoginAction implements Action {
 private String userName;
 private String passWord;
 @Override
 public String execute() throws Exception {
   if ("lty".equalsIgnoreCase(userName)&&"123".equalsIgnoreCase(passWord)){
     return "success";
   }else {
     return "error";
   }

 }

 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;
 }
}

  1. struts页面的配置,
    (注意:
    一个struts.xml文件中可以有多个action标签存在的
    )
    <action name="login" class="com.lty.web.LoginAction">
     <result name="success">/success.jsp</result>
     <result name="error">/errorLogin.jsp</result>
   </action>
  1. 登录页面
    struts2框架实现-用户登录功能
    7.登录成功的页面
    struts2框架实现-用户登录功能
    8.登录失败的页面
    struts2框架实现-用户登录功能
相关标签: struts struts2