Struts2 simple login validate

Source: Internet
Author: User

validate()方法验证

 


一个ACTION类继承ActionSupport有何用处?


答:在ActionSupport中实现了很多接口,就拿一个来讲,validate()方法,是默认的验证。我们可以在我们的action类中重写一下此方法。比方说验证用户名是否为空。这个方法不需要调用,当S:FORM表单提交了,到了action类中会首先执行此方法。对数据进行验证。

 

 

  用   el 表达式  或   struts 标签   读取错误信息

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2     pageEncoding="ISO-8859-1"%> 3 <%@ taglib uri="/struts-tags" prefix="s"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 

 

validate()方法校验  
 1 package com.helen.action; 2  3 import com.opensymphony.xwork2.ActionSupport; 4  5 public class LoginAction extends ActionSupport { 6     private String username; 7     private String password; 8  9     public void validate() {10 11         if (username == null || username.equals("")) {12             this.addFieldError("username", "please enter your username!");13         } else if (!username.equals("admin")) {14             this.addFieldError("username", "username is not right!");15         }16         if (password == null  || password.equals("")) {17 18             this.addFieldError("password", "please enter your password!");19         } else if (!password.equals("123")) {20             this.addFieldError("password", "password is not right!");21         }22 23     }24 25     public String getUsername() {26         return username;27     }28 29     public void setUsername(String username) {30         this.username = username;31     }32 33     public String getPassword() {34         return password;35     }36 37     public void setPassword(String password) {38         this.password = password;39     }40 41 }

返回值为  input

<action name="login" class="com.helen.action.LoginAction">            <result name="input">/index.jsp</result>            <result>/success.jsp</result>

</action>

 

 

Struts2 Simple login Validate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.