Struts 2 Actions By, Srinivas Reddy.S
Agenda Action and its role Packages and namespaces Results Action and ActionSupport Using wildcards
A Simple Birthday Action URL: When accessed, a page asking for the persons name should be displayed URL: This displays the success page which greets the person.
Step 1: Create a JSP: birthday.jsp Enter the Birthday Boys name: A Simple Birthday Action
A Simple Birthday Action Step 2: Create an Action class to handle the form HappyBirthday.java package greetings; public class HappyBirthday { private String birthdayBoy; public String execute(){ if(this.getBirthdayBoy().equals()||this.getBirthdayBoy()==null){ return failure"; }else{ return SUCCESS"; } public String getBirthdayBoy() { return birthdayBoy; } public void setBirthdayBoy(String birthdayBoy) { this.birthdayBoy = birthdayBoy; }
A Simple Birthday Action Step 3: Create the Success and failure JSPs birthdaySuccess.jsp Wish you a Very Happy Birthday to You :-) noBirthday.jsp LOOKS LIKE NO BODY IS CELEBRATING BIRTHDAY TODAY
A Simple Birthday Action Step 4: Configure the Struts.xml /pages/birthday.jsp /pages/birthdaySuccess.jsp /pages/noBirthday.jsp Note: Struts.xml should be in WEB-INF/classes folder
Role of Action Controller Data carrier Result to be invoked
Action as Controller public String execute(){ if(this.getBirthdayBoy().equals()||this.getBirthdayBoy()==null){ return failure"; }else{ BirthdayUtil.calculateAge(birthDate); Other business layer or service layer return SUCCESS"; } }
Action as Data Carrier public class HappyBirthday { A simple POJO private String birthdayBoy; public String getBirthdayBoy() { return birthdayBoy; } public void setBirthdayBoy(String birthdayBoy) { this.birthdayBoy = birthdayBoy; }
Action as Result render public String execute(){ if(this.getBirthdayBoy().equals()||this.getBirthdayBoy()==null){ return failure"; }else{ return SUCCESS"; } } /pages/birthdaySuccess.jsp /pages/noBirthday.jsp
No execute() method You can specify a different method other than execute method to play as controller method. <action name ="happybirthday" class ="greetings.HappyBirthday method =wishForBirthday > /pages/birthdaySuccess.jsp /pages/noBirthday.jsp
Packages and Namespaces /pages/birthday.jsp /pages/birthdaySuccess.jsp /pages/noBirthday.jsp
Note: – Package name is mandatory. – namespace is optional but the default is /. – extends – to extend a parent package. abstract- If true, this package will only be used to define inheritable components, not actions Packages and Namespaces
Result and types JSP Velocity Freemarker Xslt Text Stream
Result and types /pages/birthdaySuccess.jsp /pages/noBirthday.jsp Attributes: name – should match with the return string from the execute method. Default is SUCCESS type – chain, dispatcher, freemarker, httpheader, redirect, redirectAction, stream, velocity, xslt, plaintext, tiles
Action Interface
ActionSupport class Implements the interfaces Action, LocaleProvider, TextProvider, Validateable, ValidationAware, Serializable Supports Internationalization. Supports Programmatic validation.
ModelDriven Interface Used to tell that the form details to be filled to a different POJO instead of Action class itself. Eg., public class Login implements ModelDriven{ private User user = new User(); public Object getModel(){ return user; } }
Using Wildcards /WEB-INF/jsps/{1}.jsp URL: /WEB-INF/jsps/testwildcard.jsp
/WEB-INF/jsps/{1}/{2}.jsp URL: /WEB-INF/jsps/test/testwildcard.jsp Using Wildcards
External configuration files Struts.xml
Thank you Download this PPT and example code from