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

Struts 2 Actions

  • Upload
    santos

  • View
    36

  • Download
    1

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Struts 2 Actions

Struts 2 Actions

By,Srinivas Reddy.Swww.JAVA9S.com

Page 2: Struts 2 Actions

Agenda

• Action and its role• Packages and namespaces• Results• Action and ActionSupport• Using wildcards

www.JAVA9S.com

Page 3: Struts 2 Actions

A Simple Birthday ActionURL: http://localhost:8080/struts2actions/greetings/birthdayWhen accessed, a page asking for the persons name should be displayed

URL: http://localhost:8080/struts2actions/greetings/happybirthdayThis displays the success page which greets the person.

www.JAVA9S.com

Page 4: Struts 2 Actions

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>

A Simple Birthday Action

www.JAVA9S.com

Page 5: Struts 2 Actions

A Simple Birthday ActionStep 2: Create an Action class to handle the formHappyBirthday.javapackage 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

Page 6: Struts 2 Actions

A Simple Birthday ActionStep 3: Create the Success and failure JSPsbirthdaySuccess.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

Page 7: Struts 2 Actions

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

Page 8: Struts 2 Actions

Role of Action

• Controller• Data carrier• Result to be invoked

www.JAVA9S.com

Page 9: Struts 2 Actions

Action as Controllerpublic String execute(){if(this.getBirthdayBoy().equals(“”)||this.getBirthdayBoy()==null){return “failure";}else{BirthdayUtil.calculateAge(birthDate); Other business layer or service layerreturn “SUCCESS";}}

www.JAVA9S.com

Page 10: Struts 2 Actions

Action as Data Carrier

public class HappyBirthday { A simple POJOprivate String birthdayBoy;

public String getBirthdayBoy() {return birthdayBoy;}

public void setBirthdayBoy(String birthdayBoy) {this.birthdayBoy = birthdayBoy;}

www.JAVA9S.com

Page 11: Struts 2 Actions

Action as Result renderpublic 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

Page 12: Struts 2 Actions

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

Page 13: Struts 2 Actions

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

Page 14: Struts 2 Actions

• 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

Page 15: Struts 2 Actions

Result and types• JSP• Velocity• Freemarker• Xslt• Text• Stream

www.JAVA9S.com

Page 16: Struts 2 Actions

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

Page 17: Struts 2 Actions

Action Interface

www.JAVA9S.com

Page 18: Struts 2 Actions

ActionSupport class• Implements the interfaces Action, LocaleProvider,

TextProvider, Validateable, ValidationAware, Serializable

• Supports Internationalization.• Supports Programmatic validation.

www.JAVA9S.com

Page 19: Struts 2 Actions

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

Page 20: Struts 2 Actions

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

Page 21: Struts 2 Actions

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

Using Wildcards

www.JAVA9S.com

Page 22: Struts 2 Actions

External configuration files

Struts.xml</package> <include file="com/java9s/struts2/actions/shopping.xml"/></struts>

www.JAVA9S.com

Page 23: Struts 2 Actions

Thank you

Download this PPT and example code from

www.JAVA9S.com