使用idea 用strusts框架写一个简单的登录验证

看了很多csdn上面的文章,知道了strusts框架其实就是一个过滤器,通过自己定义的拦截去返回你所需要的内容
从某种意义上面来说,是先从浏览器发出请求 - Tomcat 的web.xml 过滤 - struts.xml 匹配action -然后 action 接收参数并传数据到struts.xml 结果集(result) 指定的jsp 页面
从而实现所需要的在页面上面的功能

本文的具体代码实现是依据了https://blog.csdn.net/hunter_max/article/details/80558678 的内容,从而理解了strust的具体的实现形式

首先strusts里面要配置相应的实现文件`

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>

    <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开-->
    <constant name="struts.action.extension" value="do,action,htm" />
    <!-- strtus2 开放动态方法访问 DMI-->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>

</struts>

然后写所需要的action,例子里面写的是简单的登录验证就是

    package com.xfc.actions;
    
    import com.opensymphony.xwork2.ActionSupport;
    import org.apache.struts2.convention.annotation.*;
    
    public class UserAction extends ActionSupport {
        private String account;
        private String password;
    
        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;
        }
    // 1.Action要必须继承ActionSupport父类; 
    // 2.Action所在的包名必须以 .action 结尾
        @Action(value = "login", results = {

>    @Action,这个注解对应节点 
>      ①.value(),表示action的请求名称,也就是节点中的name属性; 
>      ②.results(),表示action的多个result;这个属性是一个数组属性,因此可以定义多个Result; 
>      ③.interceptorRefs(),表示action的多个拦截器。这个属性也是一个数组属性,因此可以定义多个拦截器; 
>      ④.exceptionMappings(),这是异常属性,它是一个ExceptionMapping的数组属性,表示action的异常,在使用时必须引用相应的拦截器

--------------------- 

                @Result(name = "success", location = "/success.jsp"),
                @Result(name = "error", location = "/error.jsp")
        })
        public String login() {
            if (account.equals("sunxiaochuan") && password.equals("666666")) {
                System.out.println("登陆成功");
                return SUCCESS;
            } else {
                return ERROR;
            }
        }
    }

然后是具体实现的跳转页面login.jsp success.jsp error.jsp
login.jsp

  <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <form method="post" action="${pageContext.request.contextPath}/login.action">
        账号:<input type="text" name="account" placeholder="输入账号" />
        密码:<input type="password" name="password" placeholder="输入密码" />
        <input type="submit" name="btn1" value="提交">

~
success.jsp

   </form>
    </body>
    </html>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    <h1>登陆成功!</h1>
    <%--获取输入的账号和密码--%>
    <h1>账号:${requestScope.account}</h1>
    <h1>密码:${requestScope.password}</h1>
    
    </body>
    </html>

error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登陆失败!</h1>
<h1>账号:${requestScope.account}</h1>
<h1>密码:${requestScope.password}</h1>
</body>
</html>

从而去利用strusts来实现简单的登录验证

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值