Displaying Dynamic Content
Struts University Series
Displaying Dynamic Content How do we integrate Struts tags into our server pages? Which tags work with data items? Which tags work with control issues? What other tags are available?
Displaying Dynamic Content Tag Syntax Data Tags Control tags Other Tags
Tag Syntax
i18n org.apache.struts2.views.jsp.I18nTag JSP <description> Get a resource bundle and place it on the value stack name <required>true true
Tag Syntax <description> Name of resource bundle to use (eg foo/bar/customBundle) id <required>false true <description> id for referencing element. For UI and form tags it will be used as HTML id attribute
Tag Syntax
/tags /WEB-INF/lib/struts2-core.jar
Tag Syntax
/tags /WEB-INF/lib/struts2-core.jar <%@ taglib prefix="s" uri="/tags" %>
Hello <s:property name="fullName"/>
Tag Syntax Some tags may push and pop values from the stack at runtime
Some values not be available outside the scope of the tag
Initially,attributes are applied as Strings Strings evaluated for %{ ... }
Anything in between the braces is evaluated against the value stack.
Displaying Dynamic Content Tag Syntax Data Tags Control tags Other Tags
Data Tags Property Set Push Bean Action
Data Tags Property Output value of an expression Set Push Bean Action
Data Tags Property Output value of an expression Set Create temporary variable Push Bean Action
Data Tags Property Output value of an expression Set Create temporary variable Push Place object on top of value stack Bean Action
Data Tags Property Output value of an expression Set Create temporary variable Push Place object on top of value stack Bean Push JavaBean onto the value stack Action
Data Tags Property Output value of an expression Set Create temporary variable Push Place object on top of value stack Bean Push JavaBean onto the value stack Action Render URI or execute action
Examples <s:push value="myBean"> <s:property value="myBeanProperty" /> <s:property value="myBeanProperty" default="a default value" /> Example 1 prints the result of myBean's getMyBeanProperty() method. Example 2 prints the result of myBean's getMyBeanProperty() method and if it is null, print 'a default value' instead.
Examples <s:set name="personName" value="person.name"/> Hello, <s:property value="#personName"/>. How are you? <%@ taglib prefix="s" uri="/tags" %> <s:bean name="util.Counter" id="counter"> <s:param name="last" value="100"/> <s:iterator value="#counter">
<s:property name="current" />
Examples <%@ taglib prefix="s" uri="/tags" %> <s:set name="user" value="#session['user']" /> <s:bean name="util.DateFormatter" id="counter"> <s:param name="format" value="MM/dd/yyyy"/> <s:param name="date" value="#user.birthdate" /> The user's birthdate is <ww:property value="formattedDate" />
public class DateFormatter { public String getFormattedDate() { return format.format(date); } }
Examples <s:form> <s:action name="languages" executeResult="true"/> <s:submit/>
Examples public class Languages extends ActionSupport { List langList = new ArrayList(); public List getLangList() { return langlist; } public String execute() { langList.add(new Select.Language("EnglishKey", "English Language")); langList.add(new Select.Language("FrenchKey", "French Language")); langList.add(new Select.Language("SpanishKey", "Spanish Language")); return SUCCESS; } }
Examples <%@ taglib prefix="s" uri="/tags" %> <s:select tooltip="Choose Your Language" label=”Language" list="langList" name="language" listKey="key" listValue="description" emptyOption="true" headerKey="None" headerValue="None"/>
Examples public String alternate() throws Exception { ServletActionContext.getRequest().setAttribute( "stringByAction", "This is a String put in by the action's alternate()"); return SUCCESS; } }
Action output: <s:action name="actionTagAction" method="alternate" executeResult="false" /> <s:property value="#attr.stringByAction" />
Displaying Dynamic Content Tag Syntax Data Tags Control tags Other Tags
Control Tags iterator
Loop over collections of objects
if, else, elseif
Invoke conditional logic
Examples <s:iterator value="user.subscriptions">
<s:param name="host" value="host"/>"> <s:text name="registration.deleteSubscription"/> <s:param name="host" value="host"/>"> <s:text name="registration.editSubscription"/> |
Examples <s:if test="task == 'Create'"> <s:textfield label="%{getText('username')}" name="username"/> <s:else> <s:label label="%{getText('username')}" name="username"/> <s:hidden name="username"/>
Displaying Dynamic Content Tag Syntax Data Tags Control tags Other Tags
Other Tags include url i18n and text param
Other Tags include
Struts-aware include tag
url i18n and text param
Other Tags include
Struts-aware include tag
url
Render relative or absolute URLs
i18n and text param
Other Tags include
Struts-aware include tag
url
Render relative or absolute URLs
i18n and text
Render messages from resource bundle
param
Other Tags include
Struts-aware include tag
url
Render relative or absolute URLs
i18n and text
Render messages from resource bundle
param
Parameterize other tags
Examples <-- One: --> <s:include value="myJsp.jsp" /> <-- Two: --> <s:include value="myJsp.jsp"> <s:param name="param1" value="value2" /> <s:param name="param2" value="value2" /> <-- Three: --> <s:include value="myJsp.jsp"> <s:param name="param1">value1 <s:param name="param2">value2<s:param>
Example one - do an include myJsp.jsp page Example two - do an include to myJsp.jsp page with parameters param1=value1 and param2=value2 Example three - do an include to myJsp.jsp page with parameters param1=value1 and param2=value2
Displaying Dynamic Content How do we integrate Struts tags into our server pages? Which tags work with data items? Which tags work with control issues? What other tags are available?
Displaying Dynamic Content How do we integrate Struts tags into our server pages?
For JSP, reference the “/tags” URI.
Which tags work with data items? Which tags work with control issues? What other tags are available?
Displaying Dynamic Content How do we integrate Struts tags into our server pages?
For JSP, reference the “/tags” URI.
Which tags work with data items?
property, set, push, bean, action
Which tags work with control issues? What other tags are available?
Displaying Dynamic Content How do we integrate Struts tags into our server pages?
For JSP, reference the “/tags” URI.
Which tags work with data items?
property, set, push, bean, action
Which tags work with control issues?
iterator, if, else, elseif
What other tags are available?
Displaying Dynamic Content How do we integrate Struts tags into our server pages?
For JSP, reference the “/tags” URI.
Which tags work with data items?
property, set, push, bean, action
Which tags work with control issues?
iterator, if, else, elseif
What other tags are available?
include, url, i18n, param
Struts University Series