1 / 23

Struts 2 Actions

Struts 2 Actions. By, Srinivas Reddy.S. www.JAVA9S.com. Agenda. Action and its role Packages and namespaces Results Action and ActionSupport Using wildcards. www.JAVA9S.com. A Simple Birthday Action. URL: http://localhost:8080/struts2actions/greetings/birthday

santos
Download Presentation

Struts 2 Actions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Struts 2 Actions By, SrinivasReddy.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/birthday When accessed, a page asking for the persons name should be displayed URL: http://localhost:8080/struts2actions/greetings/happybirthday This displays the success page which greets the person. www.JAVA9S.com

  4. A Simple Birthday Action Step 1: Create a JSP: birthday.jsp <%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Enter the Birthday Boys name:</h1> <s:form action ="happybirthday"> <s:textfield name="birthdayBoy" label="Birthday Boy Name:"/> <s:submit/> </s:form> </body> </html> 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 <%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Wish you a Very Happy Birthday to You :-) <s:property value="birthdayBoy"/></h1> </body> </html> noBirthday.jsp <%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>LOOKS LIKE NO BODY IS CELEBRATING BIRTHDAY TODAY</h1> </body> </html> www.JAVA9S.com

  7. A Simple Birthday Action Step 4: Configure the Struts.xml <package name="default" namespace="/greetings" extends="struts-default"> <action name ="birthday" > <result>/pages/birthday.jsp</result> </action> <action name ="happybirthday" class ="greetings.HappyBirthday"> <result name ="SUCCESS">/pages/birthdaySuccess.jsp</result> <result name ="failure">/pages/noBirthday.jsp</result> </action> </package> 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"; } } <action name ="happybirthday" class ="greetings.HappyBirthday"> <result name ="SUCCESS">/pages/birthdaySuccess.jsp</result> <result name ="failure">/pages/noBirthday.jsp</result> </action> www.JAVA9S.com

  12. 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”> <result name ="SUCCESS">/pages/birthdaySuccess.jsp</result> <result name ="failure">/pages/noBirthday.jsp</result> </action> www.JAVA9S.com

  13. Packages and Namespaces <package name=“greetings" namespace="/greetings" extends="struts-default"> <action name ="birthday" > <result>/pages/birthday.jsp</result> </action> <action name ="happybirthday" class ="greetings.HappyBirthday"> <result name ="SUCCESS">/pages/birthdaySuccess.jsp</result> <result name ="failure">/pages/noBirthday.jsp</result> </action> </package> <package name=“reminders" namespace="/reminder" extends="struts-default"> <action name=“myreminder” …/> </package> www.JAVA9S.com

  14. Packages and Namespaces • 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 www.JAVA9S.com

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

  16. Result and types <action name ="happybirthday" class ="greetings.HappyBirthday"> <result name ="SUCCESS">/pages/birthdaySuccess.jsp</result> <result name ="failure">/pages/noBirthday.jsp</result> </action> 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 <action name="*"> <result>/WEB-INF/jsps/{1}.jsp</result> </action> URL: http://localhost:8080/struts2actions/testwildcard /WEB-INF/jsps/testwildcard.jsp www.JAVA9S.com

  21. Using Wildcards <action name="*/*"> <result>/WEB-INF/jsps/{1}/{2}.jsp</result> </action> URL: http://localhost:8080/struts2action/test/testwildcard /WEB-INF/jsps/test/testwildcard.jsp www.JAVA9S.com

  22. External configuration files Struts.xml </package> <include file="com/java9s/struts2/actions/shopping.xml"/> </struts> www.JAVA9S.com

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

More Related