Post on 10-Jan-2016
description
Struts 2 Actions
By,Srinivas Reddy.Swww.JAVA9S.com
Agenda
• Action and its role• Packages and namespaces• Results• Action and ActionSupport• Using wildcards
www.JAVA9S.com
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
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
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
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
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
Role of Action
• Controller• Data carrier• Result to be invoked
www.JAVA9S.com
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
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
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
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
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
• 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
Result and types• JSP• Velocity• Freemarker• Xslt• Text• Stream
www.JAVA9S.com
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
Action Interface
www.JAVA9S.com
ActionSupport class• Implements the interfaces Action, LocaleProvider,
TextProvider, Validateable, ValidationAware, Serializable
• Supports Internationalization.• Supports Programmatic validation.
www.JAVA9S.com
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
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
<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
External configuration files
Struts.xml</package> <include file="com/java9s/struts2/actions/shopping.xml"/></struts>
www.JAVA9S.com
Thank you
Download this PPT and example code from
www.JAVA9S.com