dawdler-boot-web-undertow-jsp
Module Introduction
JSP module support for the embedded undertow container.
1. Usage
Add the following to pom.xml:
<dependencies>
<dependency>
<groupId>club.dawdler</groupId>
<artifactId>dawdler-boot-web-undertow-jsp</artifactId>
</dependency>
</dependencies>
2. Configuration
undertow.yml - undertow is the currently supported container. (For tomcat support see dawdler-boot-web-tomcat-jsp)
static-location needs to be configured in undertow.yml. See Common configuration for details. JSP files are placed in this directory.
3. JSTL support
This module now supports the JSTL tag library. The following tag libraries can be used in JSP files:
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="fmt" uri="jakarta.tags.fmt" %>
<%@ taglib prefix="fn" uri="jakarta.tags.functions" %>
<%@ taglib prefix="sql" uri="jakarta.tags.sql" %>
<%@ taglib prefix="x" uri="jakarta.tags.xml" %>
Example JSP file:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="fn" uri="jakarta.tags.functions" %>
<html>
<head>
<title>JSTL Test Page</title>
</head>
<body>
<h1>JSTL Test Page</h1>
<h2>Core Tags Test</h2>
<c:set var="testVar" value="Hello JSTL!" />
<p>Value of testVar: <c:out value="${testVar}" /></p>
<h2>Functions Test</h2>
<c:set var="testString" value="Hello World" />
<p>Length of testString: ${fn:length(testString)}</p>
<p>Uppercase testString: ${fn:toUpperCase(testString)}</p>
</body>
</html>
4. Custom JSP tag libraries
Create the tld file under resources/META-INF/.
Example:
custom.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-jsptaglibrary_3_0.xsd" version="3.0">
<description>Custom tags library</description>
<display-name>reverse</display-name>
<tlib-version>1.0</tlib-version>
<short-name>cm</short-name>
<uri>custom</uri>
<tag>
<name>reverse</name>
<tag-class>com.example.demo.tag.ReverseTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
ReverseTag.java
package com.example.demo.tag;
import java.io.IOException;
import jakarta.servlet.jsp.JspException;
import jakarta.servlet.jsp.JspWriter;
import jakarta.servlet.jsp.tagext.SimpleTagSupport;
public class ReverseTag extends SimpleTagSupport {
private String value;
public void setValue(String value) {
this.value = value;
}
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
if (value != null) {
String reversedValue = new StringBuilder(value).reverse().toString();
out.print(reversedValue);
}
}
}
demo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="fn" uri="jakarta.tags.functions" %>
<%@ taglib prefix="cm" uri="custom"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple JSP Example with JSTL</title>
</head>
<body>
<h1>Hello from JSP!</h1>
<p>Current time: <%= new java.util.Date() %></p>
<p>Server info: <%= application.getServerInfo() %></p>
<cm:reverse value="Hello World!" />
<% pageContext.setAttribute("name", "Dawdler"); %>
${name}
<!-- Create a List and iterate over it with JSTL -->
<%
java.util.List<String> fruits = new java.util.ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Grape");
fruits.add("Strawberry");
request.setAttribute("fruits", fruits);
%>
<h2>Fruits List (using JSTL forEach)</h2>
<ul>
<c:forEach var="fruit" items="${fruits}">
<li>${fruit}</li>
</c:forEach>
</ul>
<!-- Output the numbers 1-5 in a loop with JSTL -->
<h2>Numbers 1-5 (using JSTL forTokens)</h2>
<%
pageContext.setAttribute("numbers", "1,2,3,4,5");
%>
<ul>
<c:forEach var="number" items="${fn:split(numbers, ',')}">
<li>Number: ${number}</li>
</c:forEach>
</ul>
<!-- Loop with index -->
<h2>Fruits with Index</h2>
<table border="1">
<tr>
<th>Index</th>
<th>Fruit</th>
</tr>
<c:forEach var="fruit" items="${fruits}" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${fruit}</td>
</tr>
</c:forEach>
</table>
</body>
</html>