Struts checks the input data

Source: Internet
Author: User

When we are logged in or are required to register the user input data validation, before the browser to send data to the background, the background to verify the data, if there is an error with the error message forward with the login or registration page,

Struts makes it easy to verify the input data

First, let's start with a input.jsp. Used as login page below is the source code JS code is not optimized, if you feel uncomfortable, I hope you are very grateful

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<base href= "<%=basePath%>" >

<title>my JSP ' input.jsp ' starting page</title>

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Cache-control" content= "No-cache" >
<meta http-equiv= "Expires" content= "0" >
<meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >
<meta http-equiv= "description" content= "This is my page" >

<style type= "Text/css" >
*{
font-family: "Arial", "Microsoft Yahei", "Black Body", "Arial", Sans-serif;
}
. input{
border-width:0px;
Outline:none;
width:280px;
margin-left:10px;
height:36px;
Color: #888;
font-size:18px;

}
. lab{
Display:block;
height:36px;
width:300px;
Border:solid 1px #ccc;
position:relative;
}
#login {

Display:block;
Position:absolute;
width:302px;
height:38px;
Background-color: #1C86EE;
Margin-top: -20px;
Text-align:center;
line-height:36px;
size:21px;
Color: #FFF;
font-family: "Arial", "Microsoft Yahei", "Black Body", "Arial", Sans-serif;
Text-decoration:none;

}
#login: HOVER {

Background: #1E90FF;
}

span{

Position:absolute;
Float:left;
line-height:40px;
left:12px;
Color: #CDCDCD;
Cursor:text;
font-family: "Arial", "Microsoft Yahei", "Black Body", "Arial", Sans-serif;
font-size:18px;
}
</style>

<script type= "Text/javascript" >
First input box gets focus
function Change1 (INPUT1) {
Document.onkeydown = function () {

if (input1.value== "") {
document.getElementById ("Span1"). style.display= "Block";
}else{
document.getElementById ("Span1"). style.display= "None";
}
};
Document.onkeyup = function () {

if (input1.value== "") {
document.getElementById ("Span1"). style.display= "Block";
}else{
document.getElementById ("Span1"). style.display= "None";
}
};
}



function Change2 (INPUT1) {
Document.onkeydown = function () {

if (input1.value== "") {
document.getElementById ("Span2"). style.display= "Block";
}else{
document.getElementById ("Span2"). style.display= "None";
}
};
Document.onkeyup = function () {

if (input1.value== "") {
document.getElementById ("Span2"). style.display= "Block";
}else{
document.getElementById ("Span2"). style.display= "None";
}
};
}

</script>

<body>
<div style= "width:300px;height:200px;margin:50px auto;" >
<form action= "" method= "" id= "" Name= "" >
<label class= "Lab" id= "LAB1" >
<span id= "Span1" > Username/email account/Mobile phone number </span>
<input type= "text" name= "username" class= "input" id= "INPUT1" onfocus= "Change1 (This)" autocomplete= "Off"/>
</label><br/>
<label class= "Lab" id= "LAB2" >
<span id= "span2" > User password </span>
<input type= "Password" name= "Userpass" class= "input" id= "Input2" onfocus= "Change2 (This)"/>
</label><br/>
</form>
<a href= "#" id= "Login" > Login &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; record </a>
</div>

</body>

Run the following effect

Let's set up the action here.

Package com.day06;

public class Validate {

Private String username;
Private String Userpass;

public void Setusername (String username) {
This.username = Username;
}
public void Setuserpass (String userpass) {
This.userpass = Userpass;
}
Public String GetUserName () {
return this.username;
}
Public String Getuserpass () {
return this.userpass;
}

Public String Login () {
Return "Success";
}
}

Configure Struts.xml

<package name= "day06" namespace= "/day06" extends= "Struts-default" >
<action name= "Login" class= "com.day06.Validate" method= "Login" >
<result name= "Success" >/success.jsp</result>
</action>
</package>

Let's check the input.

First, the action class inherits the Actionsupport class override Validate () method

@Override
public void Validate () {
if (Username.trim (). Equals ("") | | Username.trim () ==null) {
This.addfielderror ("username", "user name cannot be empty");
}
if (Userpass.trim (). Equals ("") | | Userpass.trim () ==null) {
This.addfielderror ("Userpass", "Password cannot be empty");
}
}

Then we add input.jsp to the Struts,xml.

<package name= "day06" namespace= "/day06" extends= "Struts-default" >
<action name= "Login" class= "com.day06.Validate" method= "Login" >
<result name= "Success" >/success.jsp</result>
<result name= "Input" >/input.jsp</result>
</action>
</package>

Struts checks the input data

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.