国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java struts2 validate用戶登錄校驗功能實現

2019-11-26 14:19:51
字體:
來源:轉載
供稿:網友

首先貼一下搭配的環境:

配置:
Eclipse4.3.2
jdk1.7_45
Mysql 5.0+

然后切入正題:
1、login.jsp
主要是使用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> 

2、struts.xml
配置
命名空間為“/”,繼承”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> 

3、LoginAction.java
LoginAction
繼承 ActionSupport 方法
重寫 execute()  和 validate() 方法:
execute 方法調用從后臺調用的數據庫調用的值
validate 方法用于判斷 用戶名 和 密碼輸入是否為空,并提示
注意: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;  }  }

用戶名密碼輸入不正確:

用戶名輸入正確,則登錄成功:

以上就是本文的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临沂市| 舞钢市| 石河子市| 玉门市| 凌海市| 陇西县| 平遥县| 遂昌县| 精河县| 深水埗区| 固镇县| 广德县| 濉溪县| 且末县| 利辛县| 怀仁县| 台东市| 桓台县| 青铜峡市| 新邵县| 新竹市| 四子王旗| 伊金霍洛旗| 佛坪县| 安徽省| 池州市| 上虞市| 营口市| 涞水县| 张家口市| 邳州市| 朝阳县| 邵东县| 上犹县| 曲阜市| 屏边| 增城市| 乌拉特中旗| 云梦县| 三原县| 泰兴市|