Data verification is an indispensable part of project development. It is required when users log in and password verification. Of course, what you need to do is to obtain the content entered by the user and then verify the content. Generally, read out from the database and then verify it. If it is wrong, the prompt message will be displayed, and if it is correct, it will enter the user's main interface.
Here is a simple example to illustrate the following steps:
1. Index form
Copy the 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="<%=basePath %>"/>
<title>Insert title here</title>
</head>
<body>
<h1>Demo</h1>
<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="submit"/>
</form>
</body>
</html>
When submitting, two variables -- user.name and user.age will be passed to the server, and then the corresponding Action in the struts.xml file configuration will be called.
2. Struts.xml configuration
Copy the 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">
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
It's obvious - when success is returned, success.jsp is called, error.jsp is called
3. Contents of check method in Action
Copy the 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 called the addFieldError method
4.Error.jsp page
Copy the 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Verification failed</h2>
<s:property value="errors.name[0]"/>
<br>
<s:property value="errors.name[1]"/>
<s:debug></s:debug>
</body>
</html>
The third line inside is the label library with struts2 added as specified, and it starts with s.
The fourth to last line and the sixth line are the key points. The corresponding errors.name[0] is the name is error that we put into the name attribute through the addFieldError method in 3, and errors.name[1] is obviously name is too long. The third last line is debug information.
The entire effect is finally displayed as:
The above is the entire content of the simple data verification in Struts2. I hope it can give you a reference and I hope you can support Wulin.com more.