struts 2 we have 3 types of validations
- Programmatic validations [ manually ]
- Declarative
validations [ xml validations ]
- Using Annotations
i already explained how we can do
programmatic validations, now will see how to do declarative
validations, friends observe am going to use multiple
validations rules for the field mail
Example
files required…
- index.jsp
- success.jsp
- error.jsp
- LogingEx.java [ in java4s
package ]
- LogingEx-validation.xml [ in java4s
package, must be with that .class file]
- web.xml [ in web-inf
]
- struts.xml [ in web-inf/classes folder ]
index.jsp
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib
uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Login Page</title>
<s:head />
</head>
<body>
<s:actionerror />
<s:form
action="verify">
<s:textfield
name="uname" label="Enter Username"
required="true"/><br>
<s:textfield
name="age" label="Enter Age"
required="true"/><br>
<s:textfield
name="mail" label="Enter Email id"
required="true"/><br>
<s:submit
value="Click" align="center" />
</s:form>
</body>
</html>
|
success.jsp
|
1
2
3
4
5
|
<%@ taglib prefix="s"
uri="/struts-tags" %>
Hello <s:property
value="uname" /><br>
Your Age is:<s:property
value="age" /><br>
Your mail:<s:property
value="mail" />
|
error.jsp
LogingEx.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package java4s;
import
com.opensymphony.xwork2.ActionSupport;
public class LogingEx extends
ActionSupport{
private static
final long serialVersionUID = 1L;
private String
uname,mail;
private int
age;
public String
getUname() {
return uname;
}
public void
setUname(String uname) {
this.uname = uname;
}
public String
getMail() {
return mail;
}
public void
setMail(String mail) {
this.mail = mail;
}
public int
getAge() {
return age;
}
public void
setAge(int age) {
this.age = age;
}
public String
execute()
{
return SUCCESS;
}
}
|
LogingEx-validation.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field
name="uname">
<field-validator
type="requiredstring">
<message>User Name is required.</message>
</field-validator>
</field>
<field name="age">
<field-validator
type="int">
<param
name="min">15</param>
<param
name="max">25</param>
<message>Age
must be ${min} - ${max}</message>
</field-validator>
</field>
<field
name="mail">
<field-validator
type="requiredstring">
<message>email
required</message>
</field-validator>
<field-validator
type="email">
<message>enter
valid email id</message>
</field-validator>
</field>
</validators>
|
web.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
|
struts.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?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>
<include
file="struts-default.xml"/>
<package
name="a" extends="struts-default">
<action
name="verify" class="java4s.LogingEx">
<result
name="success">/success.jsp</result>
<result
name="error">/error.jsp</result>
<result
name="input">/index.jsp</result>
</action>
</package>
</struts>
|