Table of contents
1.
Introduction
2.
Syntax
3.
Example
3.1.
Create Action Class
3.2.
Create views
3.3.
Configuration Files
4.
Frequently Asked Questions
4.1.
What are structs in JAVA?
4.2.
What is a set in java?
4.3.
What is an ORM tool?
4.4.
Write about Struts XML.
4.5.
What does struts2 mean in action?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

The Date Tag

Author Manan Singhal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The date tag makes it simple and quick to format a Date. The key 'struts.date.format' in the properties file allows the user to select a custom format (for example, "dd/MM/yyyy hh:mm"), generate easily understandable notations, or just fall back on a predefined format.

Structs 2

Syntax

<s:date name="dateField" format="dateFormat" />

Example

Create Action Class

package com.coding.ninjas.struts2.action;

import java.util.Calendar;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
 
public class DataTagAction extends ActionSupport
{
	private static final long serialVersionUID = -7744420104547018874 L;
	private Date birthday;
	private Calendar calendar = Calendar.getInstance();

 
	public String execute()
	{
		calendar.set(1983, 7, 6);
		birthday = calendar.getTime();
		return SUCCESS;
	}

 	
	public Date getBirthday()
	{
		return birthday;
	}

 	
	public void setBirthday(Date birthday)
	{
		this.birthday = birthday;
	}
}
You can also try this code with Online Java Compiler
Run Code

Create views

Create a file with the name success.jsp and put the below content into it.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
   <head>
      <STYLE type="text/css">
         b{color: blue;}
      </STYLE>
      <title>Date Data Tag Struts2 | dineshonjava.com</title>
   </head>
   <body>
        
      <h2>An example of the Date tag: </h2>
        <b>Birthday Date Format(EEEE yyyy-MM-dd):</b><br/><br/>
      <s:date name="birthday" format="EEEE yyyy-MM-dd" />
      <hr>
      <b>Birthday in Date Format (EEEE dd/MMM/yyyy hh:mm AM/PM) :</b><br/><br/>
      <s:date name="birthday" format="EEEE dd/MMM/yyyy 'at' hh:mm a" />
      <hr>
      <b>Birthday Date Format (EEEE MM-dd-yyyy hh:mm AM/PM):</b><br/><br/>
      <s:date name="birthday" format="EEEE MM-dd-yyyy 'at' hh:mm a" />
      <hr>
      <b> Birthday in Format (EEEE dd-MM-yyyy hh:mm) :</b><br/><br/>
      <s:date name="birthday" format="EEEE dd-MM-yyyy hh:mm" />
      <hr>
      <b>Birthday in Date Format(Nice):</b><br/><br/>
      <s:date name="birthday" format="yyyy/MM/dd" nice="true"/>
      <hr>
   </body>
</html>
You can also try this code with Online Javascript Compiler
Run Code

Configuration Files

Create a file with the name structs.xml and put the below content into it.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC>
<struts>
   <constant name="struts.enable.DynamicMethodInvocation" value="false" />
   <constant name="struts.devMode" value="false" />
   <constant name="struts.custom.i18n.resources" value="testapp" />
   <package name="default" extends="struts-default" namespace="/">
      <action name="datetag" class="com.coding.ninjas.struts2.action.DataTagAction">
         <result name="success">/success.jsp</result>
      </action>
   </package>
</struts>


Create a file with the name web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
version="3.0">
<display-name>Struts2DataTag</display-name>
<filter>
   <filter-name>struts2</filter-name>
   <filter-class>
      org.apache.struts2.dispatcher.FilterDispatcher
   </filter-class>
</filter>
<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
   <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


To produce a War file:

  • Perform right-click on the project name and select Export > WAR File.
  • Deploy this WAR to the webapps directory of Tomcat.
  • Launch the Tomcat server, then attempt to access it.

Frequently Asked Questions

What are structs in JAVA?

Structs is an open-source Java framework used for developing JAVA EE web applications. Many developers use it as it allows them to build maintainable, flexible, and extensible web applications faster. 

What is a set in java?

A data structure called a set is used in Java to contain distinct components. There can be no duplicates in a set collection. The equals() and hashCode() methods must be implemented for an element to be added to a set.

What is an ORM tool?

An object-relational mapping (ORM) tool makes it easier to create, manipulate, and store objects and makes them more accessible. It offers a means of translating programming languages (such as Python and Java) into objects that can be mapped to a SQL database.

Write about Struts XML.

The struts.xml file contains the configuration information that will modify as actions are generated. For an application using this file, such as struts, DevMode = false, and other default choices provided in property files can be modified.

What does struts2 mean in action?

The action tag allows programmers to execute the action specified on the view page. They can do this by giving the activity a name. They can set the "executeResult" option to "true" to render the result directly in the view.

Conclusion

In this article, we learned the structs 2 date tag concept. We have also discussed the concept of the date tag using an example.

I hope you would have gained a better understanding of these topics now!

Refer to our guided paths on Coding Ninjas Studio to learn about Data Structure and Algorithms, Competitive Programming, JavaScript, etc. Enroll in our courses and refer to our mock test available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Thank You
Live masterclass