java struts validate_Java struts2 validate用户登录校验功能实现

首先贴一下搭配的环境:

配置:

eclipse4.3.2

jdk1.7_45

mysql 5.0+

然后切入正题:

1、login.jsp

主要是使用ognl 标签 也可使用html form表单,调用loginaction.action,以post 方式传输。

在loginaaction 经过判断,然后会有提示信息,需要用到 来显示。

登录

请登录

--%>

30364f03779acd73ef3b700f418438c6.png

2、struts.xml

配置

命名空间为“/”,继承”struts-default“

登录成功,则跳转到index.jsp

登录失败,则返回login.jsp

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

/p>

"-//apache software foundation//dtd struts configuration 2.3//en"

"http://struts.apache.org/dtds/struts-2.3.dtd">

/index.jsp

/login.jsp

3、loginaction.java

loginaction

继承 actionsupport 方法

重写 execute()  和 validate() 方法:

execute 方法调用从后台调用的数据库调用的值

validate 方法用于判断 用户名 和 密码输入是否为空,并提示

注意:this.addactionerror();方法的在login.jsp中调用 即可调用,即可将设置的信息默认可以直接调用,不用设置,除非在strtus.xml 中设置 。

package com.tikitoo.action;

import com.opensymphony.xwork2.actionsupport;

import com.tikitoo.service.userinfoservice;

import com.tikitoo.service.userinfoserviceimpl;

/**

* @author tikitoo1

* @see com.opensymphony.xwork2.actionsupport

* @see com.opensymphony.xwork2.actionsupport

*

*/

public class loginaction extends actionsupport {

private static final long serialversionuid = -4760561602154545441l;

/**

* struts2 默认调用方法

* @return struts2 result 返回值

*/

@override

public string execute() throws exception {

userinfoservice userinfoservice = new userinfoserviceimpl();

boolean flag = userinfoservice.loginbyusernameanduserpwd( username, userpwd);

string msg = "";

if ( flag == true) {

this.addfielderror( "true", "登录成功");

msg = "success";

} else {

this.addfieldnerror( "用户名或密码不正确!");

msg = "input";

}

return msg;

}// execute() end

/**

* 登录验证

* 重写 actionsupport 方法

*/

@override

public void validate() {

// 判断 用户名 是否为空

if ( getusername() == null || "".equals( getusername().trim() ) ) {

this.addfielderror( "username", "用户名不能为空");

}

// 判断密码是否为空

if ( getuserpwd() == null || "".equals( getuserpwd().trim() )) {

this.addfielderror("userpwd", "密码不能为空");

}

}// validate() end

private string tip;

public string gettip() {

return tip;

}

private string username;

private string userpwd;

public string getusername() {

return username;

}

public void setusername(string username) {

this.username = username;

}

public string getuserpwd() {

return userpwd;

}

public void setuserpwd(string userpwd) {

this.userpwd = userpwd;

}

}

用户名密码输入不正确:

acd2ad3453e90c8be1a04c70e38cb790.png

用户名输入正确,则登录成功:

c060ec328d01ff64834e91db0499a267.png

以上就是本文的全部内容,希望对大家的学习有所帮助。

希望与广大网友互动??

点此进行留言吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值