Java struts2 validate用戶登錄校驗功效完成。本站提示廣大學習愛好者:(Java struts2 validate用戶登錄校驗功效完成)文章只能為提供參考,不一定能成為您想要的結果。以下是Java struts2 validate用戶登錄校驗功效完成正文
起首貼一下搭配的情況:
設置裝備擺設:
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;
}
}
用戶名暗碼輸出不准確:
用戶名輸出准確,則登錄勝利:
以上就是本文的全體內容,願望對年夜家的進修有所贊助。