Newsletter |
Programmatic Validations With Resource Bundle In Struts2
Struts » on Oct 23, 2011 { 9 Comments } By Sivateja
In 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(–), here 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 ]
Directory Structure
index.jsp
<%@ 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
<%@ taglib prefix="s" uri="/struts-tags" %> Hello <s:property value="uname" />
error.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <s:actionerror /> error page is this
LogingEx.java
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
<?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
<?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
struts.custom.i18n.resources=Mybundel
Mybundel.properties
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
After Execution
Output
Output if enter wrong user/pass
You Might Also Like
::. About the Author .:: | ||
Comments
9 Responses to “Programmatic Validations With Resource Bundle In Struts2”
Dear SivaTeja
Here you explained about Programmatic and Declarative Validations in a very fantastic way. But I didn’t fount example of Annotations validation. Could you please provide that one also.
Thanks,
Ibrahim
How can i implement threads in struts 1.x ?
Hi SivaTeja,
Can you provide validation with separate Bean values. When i am trying it is not doing any validation.
Regards
Hemanth
Hi java4s,
this is good tutorial and to any beginner its helpful but in the example that you explained above please correct the line 5 in index.jsp remove and replace it with since by using even though the validations are failed the page is not displaying any error messages but when replaced with its showing me the errors
Regards
Sreeram.
+1 for If we get any error in validate(), this automatically by default will returns string “input“.
Hello,
What should I have to do when I want to display the error message infront of fields. In above example error message displays on the top of the input fields
There site is very help full…
Hey man… you are doing good work for people like me…Thanks a lot…
Please provide examples for Declarative validations [ xml validations ]
Using Annotations too.