STRUTS2 Learning Notes (6)-Simple data validation _java

Source: Internet
Author: User

Data validation is an integral part of project development, user login, password verification needs, of course, the first thing to do is to get the user input content, and then to verify the content, generally read from the database and then check, if the error will display the message, the correct access to the user's main interface.

Here's a simple little example to illustrate the next step:

1, the index of the form

Copy Code code as follows:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
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" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<base href= "<%=basepath%>"/>
<title>insert title here</title>
<body>
<form action= "User/user!check" method= "POST" >
Name: <input type= "text" name= "User.Name" ></input>
<br/>
Age: <input type= "text" name= "User.age" ></input>
<br/>
<input type= "Submit" value= "submitted"/>
</form>
</body>

The commit will have two variables--user.name and user.age uploaded to the server, and then call the corresponding action in struts.xml file configuration

2, Struts.xml Configuration

Copy Code code as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apache Software foundation//dtd Struts Configuration 2.0//en"
"Http://struts.apache.org/dtds/struts-2.0.dtd" >

<struts>
<constant name= "Struts.devmode" value= "true"/>
<package name= "front" namespace= "/user" extends= "Struts-default" >

<action name= "user" class= "com.myservice.web.UserAction" >
<result>/success.jsp</result>
<result name= "Error" >/error.jsp</result>
</action>
</package>
</struts>

Obviously-calling Success.jsp,error when success is returned calls Error.jsp

3, action in the Check method content

Copy Code code as follows:
Public String Check () {
System.out.println ("Name=" +user.getname ());
System.out.println ("age=" +user.getage ());
if (User.getname (). Equals ("admin") &&user.getage () ==20) {
return SUCCESS;
}else{
This.addfielderror ("name", "Name is Error");
This.addfielderror ("name", "name is too long");
return ERROR;
}
}

Here we call the Addfielderror method

4, error.jsp page

Copy Code code as follows:
<%@ 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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<s:property value= "errors.name[0]"/>
<br>
<s:property value= "errors.name[1]"/>
<s:debug></s:debug>
</body>

The third line inside is a description of the tag library that added the Struts2, and begins with S.

and the fourth and sixth lines are the focus, errors.name[0] corresponds to the name is error,errors.name[1 that we put into the Addfielderror method in 3, which is clearly the name is too Long The third line is debug information.

The entire effect is finally displayed as:

The above is a simple data Struts2 in all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.