Struts2 Simple Data validation

Source: Internet
Author: User

When the form data is submitted to the background usually to verify the data, to log in as an example, the background to get the user name password will determine whether the correct, correct words will jump to the site user login Successful page, if not correct will prompt the user input is incorrect.

First configure the login action on the Struts.xml:

< Packagename= "User"namespace= "/user"extends= "Struts-default">         <Actionname= "Login"class= "Cn.orlion.user.UserAction"Method= "Login">             <resultname= "Error">/login.jsp</result>             <resultname= "Success">/backend.jsp</result>         </Action>     </ Package>

Then create a useraction (package: Cn.orlion.user):

 PackageCn.orlion.user;ImportCom.opensymphony.xwork2.ActionSupport; Public classUseractionextendsactionsupport{PrivateString username; PrivateString password;  PublicString Login () {if(!username.equals ("User")) {                         This. Addfielderror ("username", "user name is wrong!")); returnERROR; }                if(!password.equals ("pwd")) {                         This. Addfielderror ("Password", "Password wrong!")); returnERROR; }                returnSUCCESS; }     PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }    }

Backend.jsp Code:

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Website backstage</title></Head><Body>Login Successful</Body></HTML>

Login.jsp Code:

Explanation: <% @taglib uri= "/struts-tags" prefix= "s"%> this sentence is to add the STRUTS2 tag library and set the prefix to S.

<s:fielderror fieldname= "username" theme= "simple"/> (fieldName specify wrong domain name theme specified mode) This is the output useraction specified error (This.addfielderror ("username", "User name Error!") It outputs the following results:

<ul class= "ErrorMessage" >

<li>
<span>

User name is wrong!

</span>
</li>

</ul>

<s:property value= "errors.username[0"/> This sentence will only output: The user name is wrong!

<s:debug></s:debug> adds a [debug] link to the JSP page:

When clicked, it will appear:

The value stack contents is a stack that holds the value, and the property name is the name, and the property value is the value. <s:property value= "errors.username[0]"/> will fetch errors in username[0 in this stack. (Errors is a map type, and a username can contain multiple error messages)


")

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@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><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Login</title></Head><Body>    <formAction=""Method= "POST">        <inputtype= "text"name= "username" /><BR/>        <S:fielderrorFieldName= "username"Theme= "simple" />        <S:propertyvalue= "Errors.username[0]" />        <inputtype= "text"name= "Password" /><BR/>        <S:fielderrorFieldName= "Password"Theme= "simple" />        <S:propertyvalue= "Errors.password[0]" />        <inputtype= "Submit"value= "Login"    />    </form>    <S:debug></S:debug></Body></HTML>

Struts2 Simple Data validation

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.