In this article, we'll go through the URL tag in struts2. But before we get to the URL tag, let's first learn about struts2.
Struts 2 is a corporation Java web application development framework based on MVC. Struts' initial framework has been totally redesigned. It has a comprehensive feature set and an open source API implementation. The URL Tag is one of the struts2 data tags. Struts 2 data tags are usually used to change the data shown on a page. So, with that as a basis, let us go on to our main topic.
The URL Tag
The URL tag is in charge of producing URL strings. The benefit of this is that you can provide parameters to the tag. Let's see an example of how to use this URL tag. This element is used to generate a URL and display it in text format. It never works on its own, but it can offer a URL to other tags such as <s:a> to make a hyperlink or <img> to generate an image.
Implementation
We are writing a program to understand the URL Tag Implementation. First, we will construct a Ninja Coder Class, then a View, and last, we will configure the file. So let's get started with this amazing example.
Create Ninja Coder Classes
package com.CodingNinjas.struts2;
public class HelloNinjaCoder {
private String name;
public String execute() throws Exception {
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Create Views
Let us have NinjaCoder.jsp with the following content:
<%@ page contentType = "text/html; charset = UTF-8"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<html>
<head>
<title>Hello Ninja Coder </title>
</head>
<body>
<s:url id = "login" action = "login" var = "myurl">
<s:param name = "user">Ninja</s:param>
</s:url>
<a href = '<s:property value = "#myurl"/>'>
<s:property value = "#myurl"/></a>
</body>
</html>`
We are creating a URL link to the "login.action" here. We've given this URL the moniker "myurl." This is done so that we may reuse the URL link throughout the jsp file. The URL is then supplemented with a parameter named "USER." As you can see from the result above, the parameter value is attached to the query string.
The URL tag is most useful when you want to build a dynamic hyperlink based on the value of a bean's attribute.
Configuration Files
Your struts.xml file should look like this:
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name = "struts.devMode" value = "true" />
<package name = "helloaction" extends = "struts-default">
<action name = "hello"
class = "com.CodingNinjas.NinjaCoderAction"
method = "execute">
<result name = "success">/NinjaCoder.jsp</result>
</action>
</package>
</struts>
Right-click on the project name and select Export > WAR File.
After that, place this WAR in Tomcat's web apps directory.
Finally, launch Tomcat and navigate to the URL http://localhost:8080/CodingNinjasStruts2/hello.action.
Output
This will result in the screen shown below.
Frequently Asked Questions
What exactly is Struts 2?
Struts 2 is an open-source web application framework that may be used to create Java EE web applications. It uses and extends the Java Servlet API to encourage developers to employ a model-view-controller (MVC) architecture.
What are the uses of the Struts 2 framework?
Struts 2 is a framework for creating MVC (Model View Controller)-based web applications. Struts 2 is a hybrid of the opensymphony webwork framework and struts 1.
webwork + struts1 = struts2
Write some Common Data tags.
The Action Tag, The Include Tag, The Bean Tag, The Date Tag, The Param Tag, The Property Tag, The Text Tag, and The URL Tag are common data tags.
What is the URL Tag?
The URL element is used to generate a URL and display it in text format. It never works on its own, but it can offer a URL to other tags such as <s:a> to make a hyperlink or <img> to generate an image.
How can you produce a WAR file?
To produce a War file, right-click on the project name and select Export > WAR File. After that, place this WAR in Tomcat's web apps directory. Finally, launch Tomcat and navigate to the URL http://localhost:8080/CodingNinjasStruts2/system.action.
Conclusion
In this article, we learned about The URL Tag, which includes Creating and Viewing the URL tag.
After reading about the URL tag, are you not feeling excited to read/explore more articles on the topic of Ruby? Don't worry; Coding Ninjas has you covered. To learn, see the Custom tag in JSF, The Merge Tag, and BorderLayout in Java.