Skip to main content

Posts

Showing posts with the label Struts2 Tags

Struts Generic Tags

The struts generic tags are used to control the execution flow when pages are rendered. Another use of struts generic tags are data extraction. Further Generic Tags are classified into Control Tags and Data Tags. Control Tags: The Control Tags are used for flow control, such as if, else and iterate. Here are the list of Control Tags: * if tag - Details * elseIf tag - Details * else tag - Details * append tag - Details * generator tag - Details * iterator tag - Details * merge tag - Details * subset tag - Details Data Tags: The Data Tags are used for data manipulation or creation, such as bean, push, and i18n. Here are the list of Data Tags: * action - Details * bean - Details * date - Details * include - Details * param - Details * set - Details * text - Details * property - Details Struts UI tags: Struts UI Tags are mainly designed to use the data from your action/value stack or from Data Tags. These tags are used to display the data on the...

Struts2 - Submit Tag (Form Tags)

In this section, we are going to describe the submit tag . The submit tag is a UI tag that is used to render a submit button. The submit tag is used with the form tag to provide asynchronous form submissions. The submit can have three different types of rendering: • input: renders as html <input type="submit"...> • image: renders as html <input type="image"...> • button: renders as html <button type="submit"...> Add the following code snippet into the struts.xml file. struts.xml <action name="submitTag">   <result>/pages/uiTags/submitTag.jsp</result> </action> Create a jsp using the tag <s:submit> that renders a submit button. This tag contains various parameters: The value parameter presets the value of input element. In our 1st case we have set it to " Submit " The type parameter sets the type of submit to use. Valid values are input, button and image. In o...

Struts2 - Radio Tag (Form Tags)

In this section, we are going to describe the radio tag. The radio tag is a UI tag that renders a radio button input field. Add the following code snippet into the struts.xml file. struts.xml <action name="radioTag" class="net.struts2.checkboxlistTag">   <result>/pages/uiTags/radioTag.jsp</result> </action> Create an action class with two lists as shown below: checkboxlistTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class checkboxlistTag extends ActionSupport{      private List fruits;   private List animals;     public String execute()throws Exception{   fruits = new ArrayList();   fruits.add("Apple");   fruits.add("Mango");   fruits.add("Orange");   fruits.add("Pine Apple");  ...

Struts2 - Optgroup Tag (Form Tags)

In this section, we are going to describe the optgroup tag. The optgroup tag is a UI tag that creates an optgroup component which needs to reside within a select tag <s:select>. Add the following code snippet into the struts.xml file. struts.xml <action name="optgroupTag">    <result>/pages/uiTags/optgroupTag.jsp</result> </action> Create a jsp using the tag <s:optgroup> within the <s:select> tag. It creates an optgroup component. This tag contains few parameters: The label parameter sets the label attribute In our case we have set it to " Hardware " and " Software ". optgroupTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>   <title>Optgroup Tag Example!</title>    </head>   <body>  <h1><span style="background-color: #FFFFcc">Optgroup Tag Ex...

Struts2 - Password Tag (Form Tags)

In this section, we are going to describe the password tag. The password tag is a UI tag that renders an HTML input tag of type password. Add the following code snippet into the struts.xml file. struts.xml <action name="passwordTag"> <result>/pages/uiTags/passwordTag.jsp</result> </action> Create a jsp using the tag <s:password> . It renders an HTML input tag of type password. passwordTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>   <title>Password Tag Example!</title>    </head>   <body>   <h1><span style="background-color: #FFFFcc">Password Tag Example! </span></h1>  <s:form>  <s:password label="Enter Password" name="password" size="10" maxlength="8" />   </s:form>   ...

Struts2 - Label Tag (Form Tags)

In this section, we are going to describe the label tag. The label tag is a UI tag that is used to render an HTML LABEL that allows us to output label:name type of combinations that has the same format treatment as the rest of UI controls. Add the following code snippet into the struts.xml file. struts.xml <action name="labelTag">    <result>/pages/uiTags/labelTag.jsp</result> </action> Create a jsp using the tag <s:label> . It renders an HTML LABEL that allows us to output <s: label name=" " value=" " /> combination that has the same format treatment as the rest of UI controls. labelTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>   <title>Label Tag Example!</title>   </head>   <body>   <h1><span style="background-color: #FFFFcc">Label Tag Example...

Struts2 - Doubleselect Tag (Form Tags)

In this section, we are going to describe the doubleselect tag. The doubleselect tag is a UI tag that renders two HTML select elements with second one changing displayed values depending on selected entry of first one. Add the following code snippet into the struts.xml file. struts.xml <action name="doubleselectTag">   <result>/pages/uiTags/doubleselectTag.jsp</result> </action> Create a jsp using the tag <s:doubleselect> that renders two HTML select elements with second one changing displayed values depending on selected entry of first one. This tag contains various parameters: The headerKey parameter sets the header key of the second list. Must not be empty. In our case we have set it to"1" The headerValue parameter sets the header value of the second list. In our case we have set it to "--- Please Select ---" The doubleName parameter sets the name for complete component. In our case we have set it as : doub...

Struts2 - File Tag (Form Tags)

In this section, we are going to describe the file tag. The file tag is a UI tag that renders an HTML file input element achieved through browsing. Add the following code snippet into the struts.xml file. struts.xml <action name="fileTag">   <result>/pages/uiTags/fileTag.jsp</result> </action> Create a jsp using the tag . It renders an HTML file input element. The parameter name is used to set a name for element which we have used as name="uploadFile" . and the parameter accept is the HTML accept attribute that indicates the accepted file mime types which we have used as accept ="text/*". fileTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>   <title>File Tag Example!</title>   </head>   <body>   <h1><span style="background-color: #FFFFcc">File Tag Example! <...

Struts2 - Form Tag

In this section, we are going to describe the form tag. The form tag is a UI tag that renders HTML an input form. The remote form allows the form to be submitted without the page being refreshed. The results from the form can be inserted into any HTML element on the page. Add the following code snippet into the struts.xml file. struts.xml <action name="formTag"> <result>/pages/uiTags/formTag.jsp</result> </action> Create a jsp using the tag <s:form>.It renders HTML as an input form formTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>   <title>Form Tag Example!</title>   <link href="<s:url value="/css/main.css"/>"    rel="stylesheet" type="text/css"/>     </head>   <body>   <h1><span style="background-color: #FFFFcc"...

Datetimepicker Tag Example

In this section, we are going to describe the datetimepicker tag. The datetimepicker tag is a UI tag that is used to render a date/time picker in a dropdown container. A stand-alone DateTimePicker widget that makes it easy to select a date/time or increment by week, month, and/or year. It is possible to customize the user-visible formatting with either the 'formatLength' (long, short, medium or full) or 'displayFormat' attributes. By default current locale will be used. Add the following code snippet into the struts.xml file. struts.xml <action name="datetimepickerTag" class="net.struts2.includeTag">    <result>/pages/uiTags/datetimepickerTag.jsp</result> </action> Create an action class as shown: includeTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class includeTag extends ActionSupport {   private Date m...

Struts2 - Combobox Tag (Form Tags)

The struts2 combobox is basically an HTML INPUT of type text and HTML SELECT grouped together to give you a combo box functionality. You can place text in the INPUT control by using the SELECT control or type it in directly in the text field. Add the following code snippet into the struts.xml file. struts.xml <action name="comboboxTag" class="net.struts2.comboboxTag">    <result>/pages/uiTags/comboboxTag.jsp</result> </action> Create "comboboxTag " action class and a list in the class and populate it with various items as shown in the class. comboboxTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class comboboxTag extends ActionSupport{      private List fruits;   public String execute()throws Exception{   fruits = new ArrayList();   fruits.add("Appl...

Struts2 - Checkboxlist Tag (Form Tags)

In this section, we are going to describe the checkboxlist tag. The checkboxlist tag is a UI tag that creates a series of checkboxes from a list. Setup is like or , but creates checkbox tags. Add the following code snippet into the struts.xml file. struts.xml <action name="checkboxlistTag" class="net.struts2.checkboxlistTag">    <result>/pages/uiTags/checkboxlistTag.jsp</result> </action> Create two lists in the action class and populate them with various items as shown in the " checkboxlistTag " class. checkboxlistTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class checkboxlistTag extends ActionSupport{      private List fruits;   private List animals;     public String execute()throws Exception{   fruits = new ArrayList();...

Struts2 - Checkbox Tag (Data Tags)

In this section, we are going to describe the checkbox tag. The checkbox tag is a UI tag that is used to render an HTML input element of type checkbox, populated by the specified property from the ValueStack. Add the following code snippet into the struts.xml file. struts.xml <action name="checkboxTag">    <result>/pages/uiTags/checkboxTag.jsp</result> </action> Create a jsp using the tag It renders an HTML input element of type chechbox. checkboxTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>    <title>Checkbox  (Form Tag) Tag Example!</title>   </head>   <body>   <h1><span style="background-color: #FFFFcc"> Checkbox Tag Example!</span></h1>   <b>Sex</b><br>   <s:checkbox label="Ma...

Struts2 - Auto Completer Tag

In this section, we are going to describe the autocompleter tag. The autocompleter tag always displays a dropdown list with the options that have at least a partial match with entered text in the textbox. If the user clicks on the dropdown button then all options shown in the dropdown list. The autocompleter tag generates two input fields. First is "text", whose name is specified with the "name" attribute and another one is "hidden" whose name is "$(name). Key", where ${name} is the value in the "name" attribute. The autocompleter tag loads its options asynchronously when the page loads suggested options based on the text entered by you in textbox. If the autoComplete attribute is set to 'true' (By defalut 'false') then it makes suggestions in the textbox. [Note: When we use the "simple" theme, the autocompleter can be used like the ComboBox. When used on the "ajax" theme, the list can be retriev...

Struts2 - Property Tag (Data Tags)

In this section, we are going to describe the property tag. The property tag is a generic tag that is used to get the property of a value, which will default to the top of the stack if none is specified. Add the following code snippet into the struts.xml file. struts.xml <action name="propertyTag" class="net.struts2.propertyTag">   <result>/pages/genericTags/propertyTag.jsp</result> </action> Create an action class as shown: propertyTag.java package net.struts2; import com.opensymphony.xwork2.ActionSupport; public class propertyTag extends ActionSupport {   public String execute() throws Exception{   return SUCCESS;   } } Create a bean class " companyName " as shown: companyName.java package net.struts2; public class companyName {      private String name;   public voi...

Struts2 - Text Tag (Data Tags)

In this section, we are going to describe the text tag. The text tag is a generic tag that is used to render a I18n text message. Follow one of the three steps: 1. Keep the message to be displayed in a resource bundle with the same name as the action that it is associated with ie. create a properties file in the same package as your Java class with the same name as your class, but with .properties extension. 2. If the property file does-not work or the message is not found in the resource bundle, then the body of the tag will be used as default message. 3. If there is no body, then the name of the message will be used. Add the following code snippet into the struts.xml file. struts.xml <action name="textTag" class="net.struts2.textTag">    <result>/pages/genericTags/textTag.jsp</result> </action> Create an action class as shown below: textTag.java package net.struts2; import com.opensymphony.xwork2.ActionSuppor...

Struts2 - Set Tag (Data Tags)

In this section, we are going to describe the Set tag. The set tag is a generic tag that is used to assign a value to a variable in a specified scope. It is useful when you wish to assign a variable to a complex expression and then simply reference that variable each time rather than the complex expression. Add the following code snippet into the struts.xml file. struts.xml <action name="setTag">    <result>/pages/genericTags/setTag.jsp</result> </action> Now create a jsp page using tag as shown in the setTag.jsp page. The set tag is used to assign a value to a variable in a specified scope. The parameters name and value in the tag acts as the name-value pair. Here we set the parameters as name="technologyName" value="Java". setTag.jsp <%@ taglib prefix="s" uri="/struts-tags" %> <html>   <head>   <title>Set Tag (Data Tag) Example!</title>...

Struts2 - Param Tag (Data Tags)

In this section, we are going to describe the param tag. The param tag is a generic tag that is used to parameterize other tags. For example the include tag and bean tag. The parameters can be added with or without a name as a key. The param tag has the following two parameters. 1. name (String) - the name of the parameter 2. value (Object) - the value of the parameter Note: When you declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag. Struts behaves a bit different according to these two situations. Case 1. AmitHere the value would be evaluated to the stack as a java.lang.String object. Case 2. Here the value would be evaluated to the stack as a java.lang.Object object. Add the following code snippet into the struts.xml file. struts.xml <action name="paramTag">   <result>/pages/genericTags/paramTag.jsp</result> </action> Now create a jsp page to see the w...

Struts2 - Include Tag (Data Tag)

In this section, we are going to describe the include tag. The include tag is a generic tag that is used to include a servlet's output (result of servlet or a JSP page) to the current page. Add the following code snippet into the struts.xml file. struts.xml <action name="includeTag" class="net.roseindia.includeTag">    <result name="success">/pages/genericTags/includeTag.jsp</result> </action> Create an action class as shown below: includeTag.java package net.roseindia; import com.opensymphony.xwork2.ActionSupport; import java.util.*; public class includeTag extends ActionSupport {   private Date myBirthday;   public String execute() throws Exception{   setMyBirthday(new Date("Jan 12, 1984 11:21:30 AM"));   return SUCCESS;   }  ...

Struts2 - Date Tag (Data Tag)

In this section, we are going to describe the Date tag. The date tag allows to format a Date in a quick and easy way. User can specify a custom format (eg. "dd/MM/yyyy hh:mm"), can generate easy readable notations (like "in 2 hours, 14 minutes"), or can just fall back on a predefined format with key 'struts.date.format' in the properties file. If that key is not defined, it will finally fall back to the default DateFormat.MEDIUM formatting. Note: If the requested Date object isn't found on the stack, a blank will be returned. Configurable attributes are :- 1. name 2. nice 3. format Add the following code snippet into the "struts.xml" file. struts.xml <action name="dateTag" class="net.roseindia.dateTag">    <result name="success">/pages/genericTags/dateTag.jsp</result> </action> Create an action class as shown below: dateTag.java package net.roseindia; import com.opensympho...