JavaEE第四次实验

实验四 Struts2的工作流程——登录用户的高级功能

一、基础实验——拦截器与过滤器

(一)实验目的

  1. 掌握Struts2自定义拦截器的基本开发步骤和配置方法;
  2. 掌握Struts2自定义过滤器的基本开发步骤和配置方法;
  3. 理解拦截器和过滤器的特点和区别;
  4. 了解Struts2默认拦截器栈中包含的主要拦截功能;
  5. 深入理解Struts2的工作原理和基本工作过程

(二)基本知识与原理

  1. Struts2的控制器主要由三个层次组成,分别时过滤器、拦截器和业务控制器Action;
  2. 过滤器是Struts2 控制器的最前端控制器,过滤器的使用需要在web.xml 中进行配置;FilterDispatcher 是Struts2 应用中必须配置使用的过滤器,该过滤器的主要功能包括执行Action、清空ActionContext 对象等;
  3. 拦截器是Struts2 中第二个层次的控制器,能够在Action 执行前后完成一些通用功能;
  4. Struts2 内建了大量拦截器, 这些拦截器以name-class 对的形式配置在struts-default.xml 文件中,如果struts.xml 中定义的package 继承了Struts2 默认的struts-default 包,便可以直接使用默认拦截器栈defaultStack;
  5. Struts2 也允许自定义拦截器,自定义拦截器类须实现Interceptor 接口,并覆盖接口中的intercept 方法用于实现拦截器的主要功能;自定义拦截器须在struts.xml 中进行配置才能使用;
  6. 若在struts.xml 中为Action 指定了一个拦截器,则默认拦截器栈defaultStack将会失效,为了继续使用默认拦截器,必须将其进行显式地配置。

(三)实验内容及步骤

  1. 新建工程struts2_prij3,并导入Struts2的8个核心包

image-20211013140531210

  1. 新建login.jsp页面,作为用户登录的视图;新建loginSuccess.jsp页面,作为登录成功的视图,重用实验三中页面代码;

    #login.jsp
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <html>
    <head>
        <title>登录</title>
    </head>
    <body>
    <s:fielderror/>
    <s:actionerror/>
    <s:form method="post" action="login">
        <s:textfield name="loginUser.account" key="login.account.lable"/>
        <s:password name="loginUser.password" key="login.password.lable"/>
        <s:submit name="submit" key="login.submit.button"/>
    </s:form>
    </body>
    </html>
    
    #loginSuccess.jsp
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%--
      Created by IntelliJ IDEA.
      User: yiyi
      Date: 2021/10/11
      Time: 20:05
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登录成功</title>
    </head>
    <body>
    本站访问次数为:<s:property value="#application.counter"/>
    <s:property value="#session.user"/>
    <s:property value="#request.tip"/>
    <br>
    <table border="1">
        <s:iterator value="#session.shoppingcart.itemsOrdered">
            <tr>
                <th>编号</th><th>名称</th><th>说明</th><th>单价</th><th>数量</th>
            </tr>
            <tr>
                <td><s:property value="item.itemID"/> </td>
                <td><s:property value="item.name"/> </td>
                <td><s:property value="item.description"/> </td>
                <td><s:property value="item.cost"/> </td>
                <td><s:property value="numItems"/> </td>
            </tr>
        </s:iterator>
    </table>
    </body>
    </html>
    
  2. 新建cn.edu.zjut.bean包,创建UserBean.java,用于记录用户信息(重用实验三代码)

    package cn.edu.zjut.bean;
    
    import java.util.Date;
    
    public class UserBean {
         
        private String account="";
        private String password="";
        private String repassword="";
        private String name="";
        private String sex="";
        private Date birthday;
        private String address="";
        private String phone="";
        private String email="";
    
        public String getAccount() {
         
            return account;
        }
    
        public void setAccount(String account) {
         
            this.account = account;
        }
    
        public String getPassword() {
         
            return password;
        }
    
        public void setPassword(String password) {
         
            this.password = password;
        }
    
        public String getRepassword() {
         
            return repassword;
        }
    
        public void setRepassword(String repassword) {
         
            this.repassword = repassword;
        }
    
        public String getName() {
         
            return name;
        }
    
        public void setName(String name) {
         
            this.name = name;
        }
    
        public String getSex() {
         
            return sex;
        }
    
        public void setSex(String sex) {
         
            this.sex = sex;
        }
    
        public Date getBirthday() {
         
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
         
            this.birthday = birthday;
        }
    
        public String getAddress() {
         
            return address;
        }
    
        public void setAddress(String address) {
         
            this.address = address;
        }
    
        public String getPhone() {
         
            return phone;
        }
    
        public void setPhone(String phone) {
         
            this.phone = phone;
        }
    
        public String getEmail() {
         
            return email;
        }
    
        public void setEmail(String email) {
         
            this.email = email;
        }
    }
    
  3. 新建cn.edu.zjut.service包,并创建UserService.java,用于实现登录逻辑和注册逻辑(重用实验三代码)

    package cn.edu.zjut.serviece;
    
    import cn.edu.zjut.bean.UserBean;
    
    public class UserService {
         
        public boolean login(UserBean loginUser){
         
            if(loginUser.getAccount().equals(loginUser.getPassword())){
         
                return true;
            }
            return false;
        }
        public boolean register(UserBean registerUser){
         
            if(registerUser.getAccount().equals(registerUser.getPassword()) && registerUser.getPassword().equals(registerUser.getRepassword()) && !registerUser.getAccount().equals("")){
         
                return true;
            }
            return false;
        }
    }
    
  4. 新建cn.edu.zjut.action包,并创建UserAction.java,定义login()方法用于调用登录逻辑(重用实验三代码)

    package cn.edu.zjut.action;
    
    import cn.edu.zjut.bean.UserBean;
    import cn.edu.zjut.serviece.UserService;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import org.apache.struts2.ServletActionContext;
    import org.apache.struts2.interceptor.*;
    import org.apache.struts2.util.ServletContextAware;
    
    import javax.servlet.ServletContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.util.Map<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiyiqwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值