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 myBirthday;
  public String execute() throws Exception{
  setMyBirthday(new Date("Jan 12, 1984 11:21:30 AM"));
  return SUCCESS;
  }
  public void setMyBirthday(Date date){
  this.myBirthday = date;
  }
  public Date getMyBirthday(){
  return myBirthday;
  }
}

Create a jsp using the tag <s:datetimepicker> This tag renders a date/time picker in a dropdown container.

The tag <s:datetimepicker name="myBirthday" label="My Birth Day (dd-MM- yyyy)" displayFormat="dd-MM-yyyy" /> picks the data from the action class
"includeTag" using the parameter name="myBirthday" using the display format as displayFormat="dd-MM-yyyy".

datetimepickerTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head> 
  <title>Datetimepicker (Form Tag) Tag Example!</title>
  <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
  type="text/css"/>

  <s:head theme="ajax" />
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Datetimepicker 
    Tag Example!</span></h1>
  <s:datetimepicker name="myBirthday" label="My Birth Day 
   (dd-MM-yyyy)" displayFormat="dd-MM-yyyy" />    
  </body>
</html>


Output of the datetimepickerTag.jsp:

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("Apple");
  fruits.add("Mango");
  fruits.add("Orange");
  fruits.add("Pine Apple");
  return SUCCESS;

  }

  public List getFruits(){
  return fruits;
  }
}


Create a jsp using the combobox tag


The first struts2 combobox tag prints a combobox with name Color name and an HTML INPUT of type text and HTML SELECT grouped together created using the list.

The second
struts2 combobox tag prints a combobox with name Fruits name and an HTML INPUT of type text and HTML SELECT grouped together created using the "fruits" list of the action class.

comboboxTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head> 
  <title>Combobox  (Form Tag) Tag Example!</title>
  <link href="<s:url value="/css/main.css"/>" rel="stylesheet"
  type="text/css"/>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Combobox 
   Tag Example!</span></h1>
  <s:form>
  <s:combobox label="Colors Name" name="colorNames" 
   headerValue="--- Please Select ---"
  headerKey="1" list="{'Black','Green','White','Yellow',
  'Red','Pink'}" /><br>
  
  <!-- Use array list --><br>
  <s:combobox label="Fruits Name" name="fruitsNames"
    headerValue="--- Please Select ---"
  headerKey="1" list="fruits" />
  </s:form>
  </body>
</html>

Output of the struts2 comboboxTag.jsp:


struts2 combobox

Create Struts2 project Using NetBeans

If you decide to start learning and using struts2, probably one of the first things you think about is which IDE to use, and what you need in order to create and run a Struts2 project...

If you search the web , you'll find many tutorials on how to create a struts2 project, and start developing.
This is what I did when I was starting, and soon find out that nothing I found on the web worked for me in proper manner...

So, I decided to create a small tutorial on how to create an empty struts2 project , in order to help others, and save their time.
Not to teach you Struts2, but to help you create a working Struts2 project!

Ok, I will use NetBeans 6.5, and a Tomcat 6 that comes with it...

First of all, go to http://struts.apache.org/2.x/index.html and download the latest version of Struts2.
I downloaded struts-2.1.6-all.zip
You won't be needing all the libraries in it. Just 6 of them. Because of this, you can download essential dependencies only.

When you download one of these files, extract it to some location, for example: c:\Struts2
It is now time to start a new project. Start NetBeans.

Click File -> New project -> Java web -> Web application



Let's name our project "Struts2Example". Click "next".
Choose which server you wish to use ( I selected Tomcat 6 ), set JavaEE to version 5, and leave ContextPath to be /Struts2Example

You can click "finish" now.
If you click "next" DO NOT, I repeat DO NOT choose available ( older version ) of struts under "frameworks".

Your project tree will look this:



Now, in order to use Struts2 libraries, let's add those essential dependencies to project:

Right click on "Libraries" folder and then click "Add JAR/folder". A dialog will open.
Go to folder where you extracted Struts2 libraries and select ( add to project ) following files:

- struts2-core-2.1.6.jar
- freemarker-2.3.13.jar
- ognl-2.6.11.jar
- commons-fileupload-1.2.1.jar
- commons-logging-1.0.4.jar
- xwork-2.1.2.jar


Most of the tutorials on the web are written for older version of struts2, at the time when commons-fileupload-1.2.1.jar was not mandatory, so even when following all the steps in tutorial , you just couldn't get your project to work ( with newer version of Struts2 ) !

Now, your project will loke like this:



Now, in the root of WEB-INF folder create a new folder , and name it "lib".
Copy these six JARs into this new folder.



These files will be uploaded to App Server , and a deployed application will use it.
We could put this files into main lib folder of application server, but it is most probably better this way...

We need to create one more folder in the root of the WEB-INF , and name it "classes".
This is where you will create struts.xml, and if needed - a struts.properties file.




I'll put all my JSP's into new folder, "jsp" , which I will create in the root of "Web Pages" folder.

All Java code that we write, we will place into some package in the "Source Packages" folder.
I'll create one now, called "Struts2Example" and a "Hello" Class in it.
If you use validaton, put all Validation XMLs inside class's package too...





This is how, at minimum, your Struts2 project structure should look like.


I'll create one small Struts2 application now. Nothing complicated.
User will enter his name, and press "submit" and other page will open , saying Hello to this user.

So, let's first create a Java class extending ActionSupport class , named hello, inside Struts2Example package.
Let's make it to look something like this:


package Struts2Example;

    import com.opensymphony.xwork2.ActionSupport;

    /**
    *
    * @author Darko
    */
    public class Hello extends ActionSupport
    {
    private String name, message;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getMessage() {
    return "Hello " + getName();
    }

    public void setMessage(String message) {
    this.message = message;
    }

    }

We now need two JSPs, for example "nameinput.jsp" and a "response.jsp"
Each JSP needs to contain this line, in order to use struts2 custom tags in it:

<%@ taglib prefix="s" uri="/struts-tags" %>

nameinput.jsp:



response.jsp:


Let's create an XML validation file for Hello class, and make a name property mandatory.
Hello-validation.xml:


<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
<field name="name">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>Please enter name</message>
</field-validator>
</field>
</validators>

There's only 2 things left for us to do:

Edit struts.xml, and web.xml:

struts.xml:


web.xml:


and if you deploy this project to server, and open
http://localhost:8084/Struts2Example/entername.action , you'll get something like this:




If we do not enter name:





and if we do:




So, I hope I helped you to understand how to create an empty struts2 project in NetBeans.
At the end, our project looked like this:




There's more to it. For example, if you wish to override some theme's behaviour ( for example "simple" ) you create folders
template/theme_name ( for example template/simple ) and it you copy all the original files from this theme ( from struts2-core-X.X.X.jar ) and edit them in this folder...



But, some other time about that and some other usefull things...

Enjoy Struts2!

Global Action Forwards and Action Mappings

What is an action forward?
A action forward can be used to forward to a jsp or action mapping. There are two different action forwards. The global action forward and the local action forward. You can access a global action forward on each jsp or action class. A local action forward can only be accessed by the assigned action class.

What is a action mapping?
The action mapping is the heart of struts. It managed all actions between the application and the
user. You can define which action will be executed by creating a action mapping.

The diagram show you, how the application server manage the request of the index.jsp or a
non existing action mapping.



In the first step we create a new action mapping. Open the struts-config.xml, you will find it in the folder WebRoot/WEB-INF. Right click in the outline window on action-mapping.



Choose Use Case default and Action Type Forward. The Forward Path is the welcome page /jsp/index.jsp



In the second step you create a global action forward. Go back to the outline window of MyEclipse and choose Global Forward



Choose the Forward Scope Global Forward. For name use the same you have set in your default page. The Global Forward refers to your action mapping.




Create a object class „book“ Create a new class Book in the package de.laliluna.tutorial.library.


Create a getter and setter for each variable. Right click in your class, Source > Generate Getters and Setters


Create a form bean, action form and jsp
Open the struts-config.xml. Right click on Form Bean in the outline window.


Create an action mapping and action class
Open the struts-config.xml and create a new action mapping.



Test the application
Start the jboss and deploy the project as package archiv.




Call the project in your favorite web browser. http://localhost:8080/LibraryWeb/

Create Struts2 Project Using MyEclipse

Create a struts project

Create a new struts project with File > New > Project or use the shortcut Strg + n.
Select the Wizard in J2EE Web Project


Create a nice name for your project


After creating the project, your Package Explorer looks like the picture below.


For now your project is a normal J2EE project, so we need to add the struts capabilityies. Right click on the project and add the capabilityies for struts with Add Struts Capabilityies.


Change the properties Base package for new classes and Default application resource



Create a default welcome page
Ok, now we want to create a default page. Right click (yes again) on the Folder WebRoot in the Project and choose New > JSP.



Set the name to index.jsp and choose on template to use > Standard JSP using Struts 1.1 MyEcplise will use the template to create the JSP File.




You will find the file index.jsp in the folder WebRoot of the project. On the top of the file you will
find the struts tag libraries. These includes will be use to access the tags of struts. In your case we only need the logic tag library.



Global Action Forwards and Action Mappings>>>



Create struts2 HelloWorld Application>>>

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();
  fruits.add("Apple");
  fruits.add("Mango");
  fruits.add("Orange");
  fruits.add("Pine Apple");

  animals = new ArrayList();
  animals.add("Dog");
  animals.add("Elephant");
  animals.add("Ox");
  animals.add("Fox");
  return SUCCESS;

  }

  public List getFruits(){
  return fruits;
  }

  public List getAnimals(){
  return animals;
  }
}



Create a jsp using the tags


it prints a checboxlist with name Fruits and Creates a series of checkboxes from fruits
list of the action class "checkboxlistTag".
it prints a checboxlist with name Animals and Creates a series of checkboxes from animals list of the action class "checkboxlistTag".


checkboxlistTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>


   
  Checkboxlist  (Form Tag) Tag Example!
  
  
  

Checkboxlist Tag Example!

Fruits Animals



Output of the checkboxlistTag.jsp:

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="Male" name="male" value="true" /><br>
  <s:checkbox label="Female" name="male" />
  </body>
</html>

Output of the checkboxTag.jsp :

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 retrieved from an action.]

Add the following code snippet into the struts.xml file.

struts.xml
<action name="autocompleter" class="net.struts2.autocompleter">
  <result>/pages/autocompleter.jsp</result>
</action>

Create a list in the action class and populate them with various states name of U.S. as shown in the "autocompleter" class.

package net.struts2;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;

public class autocompleter extends ActionSupport{
  private List state;
  public String execute() throws Exception{
  state = new ArrayList();
  state.add("Alabama");
  state.add("Alaska");
  state.add("Arizona");
  state.add("Arkansas");
  state.add("California");
  state.add("Colorado");
  state.add("Connecticut");
  state.add("Delaware");
  state.add("District of Columbia");
  state.add("Florida");
  state.add("Georgia");
  state.add("Hawaii");
  state.add("Idaho");
  state.add("Illinois");
  state.add("Indiana");
  state.add("Iowa");  
  state.add("Kansas");  
  state.add("Kentucky");
  state.add("Louisiana");  
  state.add("Maine");  
  state.add("Maryland");
  state.add("Massachusetts");  
  state.add("Michigan");
  state.add("Minnesota");
  state.add("Mississippi");
  state.add("Missouri");
  state.add("Montana");  
  state.add("Nebraska");  
  state.add("Nevada");  
  state.add("New Hampshire");  
  state.add("New Jersey");
  state.add("New Mexico");
  state.add("New York");  
  state.add("North Carolina");
  state.add("North Dakota");  
  state.add("Ohio");
  state.add("Oklahoma");
  state.add("Oregon");  
  state.add("Pennsylvania");
  state.add("Rhode Island");  
  state.add("South Carolina");  
  state.add("South Dakota");
  state.add("Tennessee");  
  state.add("Texas");  
  state.add("Utah");
  state.add("Vermont");  
  state.add("Virginia");  
  state.add("Washington");  
  state.add("West Virginia");
  state.add("Wisconsin");  
  state.add("Wyoming");
  return SUCCESS;
  }
  public List getState(){
  return state;
  }
}



it creates a autocompleter list with the name of U.S. states.


autocompleter.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
  <title>Struts 2 Autocompleter Example!</title>
  <s:head theme="ajax" />
  </head>
  <body>
  <h1>Struts 2 Autocompleter Example!</h1>
  <s:label name="stateName" value="Select State Name:" />
  <s:autocompleter theme="simple" list="state" name="StateName"/>
  </body>
</html>

Output of the autocompleter.jsp :




When you enter the 'c' letter in the textbox. You get the following names:


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 void setName(String name){
  this.name =name ;
  }

  public String getName(){
  return name;
  }
}

Create a jsp using the tags.

it prints the result of myBean's getMyBeanProperty() method.

it prints the result of companyName's getName() method and if it is null, print 'a default value' instead.


propertyTag.jsp


<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
  <title>Property Tag (Data Tag) Example</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Property Tag 
    (Data Tags) Example!</span></h1>
  <!-- Example to pick the value through bean class -->
  <s:bean name="net.struts2.companyName" id="uid">
  <s:param name="name">Struts2</s:param> 
  <s:property value="%{name}" /><br>
  </s:bean>
  <!-- Default value -->
  <s:property value="name" default="Default Value" />
  </body>
</html>

Output of the propertyTag.jsp:

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.ActionSupport;

public class textTag extends ActionSupport {

  public String execute() throws Exception{
  return SUCCESS;
  }
}

Create a property file in the same package where your Java program file (textTag.java) is saved with the name as package.properties.

package.properties

webname1 = http://www.struts2.net
webname2 = http://www.java.com
webname3 = http://www.newstrack.com

Now create a jsp page to see the working of the text tags.
The first three tags , and uses the package.properties file to display the text message.
The next tag Vinod, Amit, Sushil, ....... uses the body of the tag as a default message.
The last tag does not have access to the package.properties files nor does it have a body so it uses name of the message to display.


textTag.jsp


<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
  <title>Text Tag (Data Tag) Example!</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Text Tag 
    (Data Tags) Example!</span></h1>
  
  <s:text name="webname1"></s:text><br>
  <s:text name="webname2"></s:text><br>
  <s:text name="webname3"></s:text><br>
  <s:text name="empname">Vinod, Amit, Sushil, .......</s:text><br>
  <s:text name="empname"></s:text>

  </body>
</html>

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>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Set Tag 
   (Data Tags) Example!</span></h1>
  <s:set name="technologyName" value="%{'Java'}"/>
  Technology Name: <s:property value="#technologyName"/>
  </body>
</html>

Output of the setTag.jsp:

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 working of the param tags.

paramTag.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
  <head>
  <title>Param Tag (Data Tag) Example</title>
  </head>
  <body>
  <h1><span style="background-color: #FFFFcc">Param Tag 
   (Data Tags) Example!</span></h1>
  <ui:component>
  <ui:param name="empname">Vinod</ui:param><br>
  <ui:param name="empname">Amit</ui:param><br>
  <ui:param name="empname">Sushil</ui:param> 
  </ui:component>
  </body>
</html>

Output of paramTag.jsp: