源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Java struts2 validate用户登录校验功能实现

  • 时间:2021-07-08 01:54 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Java struts2 validate用户登录校验功能实现
首先贴一下搭配的环境: [b]配置: Eclipse4.3.2 jdk1.7_45 Mysql 5.0+[/b] 然后切入正题: [b]1、login.jsp[/b] 主要是使用OGNL 标签 也可使用html form表单,调用LoginAction.action,以post 方式传输。 在LoginaAction 经过判断,然后会有提示信息,需要用到 <s:fielderror/>来显示。
<%@ taglib uri="/struts-tags" prefix="s"%> 
<!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> 
 <center> 
  请登录 
  <!-- 
   this.addActionError( "用户名或密码不正确!"); 
   Action 中设置的显示信息 
   要在html 中引用 OGNL 表达式 
   --> 
  <s:actionerror/> 
  <%-- <s:fielderror/> 
   --%> 
  <s:form action="LoginAction.action" method="post"> 
   <s:label value="用户名:" /> 
   <s:textfield name="userName" /> 
   <br /> 
   <s:label value="密码" /> 
   <s:textfield name="userPwd" /> 
   <br /> 
   <s:submit value="登录" /> 
  </s:form> 
 </center> 
</body> 
[img]http://files.jb51.net/file_images/article/201605/201605181300416.png[/img] [b]2、struts.xml[/b] [b]配置[/b] 命名空间为“/”,继承”struts-default“ 登录成功,则跳转到index.jsp 登录失败,则返回login.jsp
<pre name="code" class="html"><?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> 
 <!-- 默认 使用 Struts2 OGNL 标签,Struts2 会经过处理 会是更加格式化,如果不行引用可以添加此属性 --> 
 <constant name="struts.ui.theme" value="simple" /> 
  
 <package name="default" namespace="/" extends="struts-default"> 
  <action name="LoginAction" class="com.tikitoo.action.LoginAction"> 
   <result name="success">/index.jsp</result> 
   <result name="input">/login.jsp</result> 
  </action> 
 </package> 
</struts> 
[b]3、LoginAction.java[/b] LoginAction 继承 ActionSupport 方法 重写 execute()  和 validate() 方法: [b]execute 方法[/b]调用从后台调用的数据库调用的值 [b]validate 方法[/b]用于判断 用户名 和 密码输入是否为空,并提示 注意:this.addActionError();方法的在login.jsp中调用<s:fielderror/> 即可调用,即可将设置的信息<s:fielderror/>默认可以直接调用,不用设置,除非在strtus.xml 中设置<constant name="struts.ui.theme" value="simple" /> 。
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; 
 }  
}
用户名密码输入不正确: [img]http://files.jb51.net/file_images/article/201605/201605181300417.png[/img] 用户名输入正确,则登录成功: [img]http://files.jb51.net/file_images/article/201605/201605181300428.png[/img] 以上就是本文的全部内容,希望对大家的学习有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部