Feel a weekend, people are too lazy not to, today will be a simple content it-----------------------crossing
There are four main ways to verify STRUTS2:
First, write the verification code directly in the function method (do not throw eggs, this method is really a kind of)
Second, rewrite the Validate method (Note that this method verifies all the methods in the Class)
Iii. using the ValidateXxx method (XXX corresponds to the method name of the method to be validated)
Four, with STRUTS2 verification framework (that is, the use of configuration files to achieve, this method of personal feel should belong to the content of the abandonment, why? Because some of the small partners in front of three methods are used very happy, a learning this way to give up--)
No nonsense, on the code (here on the show to everyone, does not mean I want to live "on" code):
First, write the verification code directly in the function method
This is relatively simple, the steps are also few, I believe that everyone will see.
1.1) Modify the action in need of data validation method, we still take the Add method to operate it, with ripe, handy ...
12345678910111213141516171819202122 |
// 添加
public
String add() {
//编号必须是六位的数字
if
(Integer.toString(singer.getSingerID()).length()!=
6
){
this
.addFieldError(
"err_id"
,
"编号必须是6位的数字"
);
}
//姓名不能为空
if
(
""
.equals(singer.getSingerName().trim())){
this
.addFieldError(
"err_name"
,
"姓名不能为空"
);
}
System.out.println(
"调用了添加的方法!"
);
System.out.println(
"编号:"
+singer.getSingerID());
System.out.println(
"姓名:"
+singer.getSingerName());
System.out.println(
"地区:"
+singer.getArea());
System.out.println(
"留言:"
+msg);
if
(
this
.hasErrors()){
return
INPUT;
}
return
"add"
;
}
|
1.2) Modify the Struts.xml configuration file, plus the case when the return result is input
12345 |
<!-- 通配符方式调用 -->
<
action
name
=
"smng_*" class
=
"com.pxy.action.Hello"
method
=
"{1}"
>
<
result
name
=
"{1}"
>/WEB-INF/jsp/singer_{1}.jsp</
result
>
<
result
name
=
"input"
>/WEB-INF/jsp/singer_{1}.jsp</
result
>
</
action
>
|
1.3) Modify the JSP page, plus the error display information (display area to decide it yourself)
12 |
< s:fielderror fieldName = "err_id" ></ s:fielderror > < s:fielderror fieldName = "err_name" ></ s:fielderror > |
1.4) Call the corresponding method
In the Address bar type: localhost:8888/strutsdemo/singeradd.action, the final result is as follows:
Today's time is limited, first write so much, after the content, we tell!!!
Java from getting started to giving up: a common way to verify STRUTS2