struts 2 we have 3 types of validations
- Programmatic validations [ manually ]
- Declarative validations [ xml validations ]
- Using Annotations
First am going explain with
programmatic validations…
- If we want to apply manual validations in struts 2, then we need to extend our Action class from ActionSupport
- We need to override validate() method in our Action class
- So in our action class we have execute() and validate() 2 methods, among these 2 validate() method will be executed first, because depends on the validations result execute() method will be executed
- In struts 2 if we get any input validation error then either we can type the error message or we can get the message from a resource bundle
- If we get any error in validate(), this automatically by default will returns string “input“. We have error in validate() method so execute() will not executes, in struts.xml we will sends login.jsp back will see this in example
- In login.jsp we no need to write any tag to display the errors from this.addActionError(—-) from action class, automatically errors will displays on the top of the input fields
- Come to execute(), if there is no errors in validate() controller will comes and executes execute() method, and if we get any errors in execute() we need to add errors to addActionError(–), hear we need to add <s:actionerrors/> tag in the input page to display the errors
- If we want to get the error message from the resource bundle we need to call gettext() method
Complete
Example
files required…
- index.jsp
- success.jsp
- error.jsp
- LogingEx.java [ in java4s package ]
- web.xml [ in web-inf ]
- struts.xml [ in web-inf/classes folder ]
- struts.properties [ in web-inf/classes folder ]
- Mybundel.properties [ in web-inf/classes folder ]
index.jsp
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<%@ taglib prefix="s"
uri="/struts-tags" %>
<html>
<body>
<s:actionerror/>
<s:form action="verify">
<s:textfield
name="uname" key="enter.user" /><br>
<s:password name="password"
key="enter.pass" /><br>
<s:submit value="Click"
align="center" />
</s:form>
</body>
</html>
|
success.jsp
|
1
2
|
<%@ taglib prefix="s"
uri="/struts-tags" %>
Hello <s:property value="uname"
/>
|
error.jsp
|
1
2
3
|
<%@ taglib prefix="s"
uri="/struts-tags" %>
<s:actionerror />
error page is this
|
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
41
42
43
44
45
|
package java4s;
import com.opensymphony.xwork2.ActionSupport;
public class LogingEx extends ActionSupport{
private static final
long serialVersionUID = 1L;
private String uname,password;
public String getUname()
{
return
uname;
}
public void setUname(String
uname) {
this.uname
= uname;
}
public String getPassword()
{
return
password;
}
public void setPassword(String
password) {
this.password
= password;
}
public String execute()
{
if(uname.equals("java4s")
&& password.equals("pass"))
{
return SUCCESS;
}else
this.addActionError(getText("u.p.wrong"));
return ERROR;
}
public void validate()
{
if(uname.equals("")
|| uname.length()==0)
this.addFieldError("uname",getText("user.wrong"));
if(password.equals("")
|| password.length()==0)
this.addFieldError("password",getText("pass.wrong"));
}
}
|
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>
|
struts.properties
|
1
|
struts.custom.i18n.resources=Mybundel
|
Mybundel.properties
|
1
2
3
4
5
|
enter.user = User name
enter.pass = Password
user.wrong = You must enter the user
pass.wrong = password should not be
enter
u.p.wrong = user or Password is wrong
|
No comments:
Post a Comment