Java struts2 Validate user Login check function to achieve _java

Source: Internet
Author: User
Tags html form

First of all, put together a matching environment:

Configuration:
Eclipse4.3.2
Jdk1.7_45
Mysql 5.0+

And then cut to the chase:
1, login.jsp
The main use of the OGNL tag is also to use the HTML form form, call the Loginaction.action, and post the transmission.
After the loginaaction is judged, then there is a hint that needs to be used <s:fielderror/> to display.

<%@ taglib uri= "/struts-tags" prefix= "s"%> 
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 
 
 

2, Struts.xml
Configuration
namespace is "/", Inherit "Struts-default"
Login successful, then jump to index.jsp
Login failed, return 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> 
 <!--default use of Struts2 OGNL tags, Struts2 will be processed will be more formatted , if not, references can add this property--> 
 <constant name= "Struts.ui.theme" value= "simple"/> <package 
  
 "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
Inherit Actionsupport method
Override the Execute ()   and Validate () method: br> Execute method calls the value of a database call from the background
Validate method is used to determine the user name and The password entry is null and prompts the
Note that the This.addactionerror () method can be invoked in login.jsp to invoke <s:fielderror/>, and the information set will be <s:fielderror/ > The default can be called directly without setting, unless you set <constant name= "Struts.ui.theme" value= "simple"/> in 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 =-4760561602154 
 545441L; 
   
  /** * STRUTS2 Default invocation method * @return STRUTS2 result return value */@Override public String Execute () throws Exception { 
  Userinfoservice userinfoservice = new Userinfoserviceimpl (); 
   
  Boolean flag = Userinfoservice.loginbyusernameanduserpwd (UserName, userpwd); 
   
   
  String msg = ""; 
   if (flag = = True) {This.addfielderror ("true", "Login succeeded"); 
  msg = "Success"; 
   else {this.addfieldnerror ("username or password incorrect!"); 
  msg = "Input"; 
 return msg; }//execute () End/** * Login Verification * Rewrite Actionsupport method * * @Override public void Validate () {//To determine whether the user name is an empty if (getusername () = NULL | | 
    
  '. Equals (GetUserName (). Trim ())) {This.addfielderror ("UserName", "User name cannot be empty"); //Determine if the password is empty if (getuserpwd () = NULL | | 
  '. Equals (Getuserpwd (). Trim ())) {This.addfielderror ("userpwd", "Password cannot be blank"); 
 }//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;
 }  
}

User name password entered incorrectly:

User name entered correctly, Login succeeded:

The above is the entire content of this article, I hope to help you learn.

Related Article

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.