Presentation is loading. Please wait.

Presentation is loading. Please wait.

Struts 2 Actions By, Srinivas Reddy.S www.JAVA9S.com.

Similar presentations


Presentation on theme: "Struts 2 Actions By, Srinivas Reddy.S www.JAVA9S.com."— Presentation transcript:

1 Struts 2 Actions By, Srinivas Reddy.S www.JAVA9S.com

2 Agenda Action and its role Packages and namespaces Results Action and ActionSupport Using wildcards www.JAVA9S.com

3 A Simple Birthday Action URL: http://localhost:8080/struts2actions/greetings/birthdayhttp://localhost:8080/struts2actions/greetings/birthday When accessed, a page asking for the persons name should be displayed URL: http://localhost:8080/struts2actions/greetings/happybirthdayhttp://localhost:8080/struts2actions/greetings/happybirthday This displays the success page which greets the person. www.JAVA9S.com

4 Step 1: Create a JSP: birthday.jsp Enter the Birthday Boys name: A Simple Birthday Action www.JAVA9S.com

5 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; } www.JAVA9S.com

6 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 www.JAVA9S.com

7 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 www.JAVA9S.com

8 Role of Action Controller Data carrier Result to be invoked www.JAVA9S.com

9 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"; } } www.JAVA9S.com

10 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; } www.JAVA9S.com

11 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 www.JAVA9S.com

12 No execute() method You can specify a different method other than execute method to play as controller method. { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/5/1513960/slides/slide_12.jpg", "name": "No execute() method You can specify a different method other than execute method to play as controller method.", "description": " /pages/birthdaySuccess.jsp /pages/noBirthday.jsp www.JAVA9S.com.", "width": "800" }

13 Packages and Namespaces /pages/birthday.jsp /pages/birthdaySuccess.jsp /pages/noBirthday.jsp www.JAVA9S.com

14 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 www.JAVA9S.com

15 Result and types JSP Velocity Freemarker Xslt Text Stream www.JAVA9S.com

16 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 www.JAVA9S.com

17 Action Interface www.JAVA9S.com

18 ActionSupport class Implements the interfaces Action, LocaleProvider, TextProvider, Validateable, ValidationAware, Serializable Supports Internationalization. Supports Programmatic validation. www.JAVA9S.com

19 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; } } www.JAVA9S.com

20 Using Wildcards /WEB-INF/jsps/{1}.jsp URL: http://localhost:8080/struts2actions/testwildcard /WEB-INF/jsps/testwildcard.jsp www.JAVA9S.com

21 /WEB-INF/jsps/{1}/{2}.jsp URL: http://localhost:8080/struts2action/test/testwildcard /WEB-INF/jsps/test/testwildcard.jsp Using Wildcards www.JAVA9S.com

22 External configuration files Struts.xml www.JAVA9S.com

23 Thank you Download this PPT and example code from www.JAVA9S.com


Download ppt "Struts 2 Actions By, Srinivas Reddy.S www.JAVA9S.com."

Similar presentations


Ads by Google