or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.mdjetty-integration.mdjstl-core.mdjstl-formatting.md

jstl-formatting.mddocs/

0

# JSTL Formatting

1

2

The JSTL Formatting tag library provides internationalization and localization capabilities, including number formatting, date formatting, and message localization for building multilingual web applications.

3

4

## Tag Library Declaration

5

6

```jsp

7

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

8

```

9

10

## Capabilities

11

12

### Number Parsing

13

14

Parses string representations of numbers into numeric values with locale-aware formatting and error handling.

15

16

```jsp { .api }

17

<fmt:parseNumber var="variableName" value="${stringValue}" />

18

<fmt:parseNumber var="variableName" value="${stringValue}" type="number|currency|percent" />

19

<fmt:parseNumber var="variableName" value="${stringValue}" pattern="customPattern" />

20

<fmt:parseNumber var="variableName" value="${stringValue}" parseLocale="${locale}" />

21

```

22

23

**Usage Example:**

24

25

```jsp

26

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

27

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

28

29

<!-- Parse a currency string -->

30

<fmt:parseNumber var="price" value="$1,234.56" type="currency" />

31

<c:out value="Parsed price: ${price}" />

32

33

<!-- Parse with custom pattern -->

34

<fmt:parseNumber var="percentage" value="85.5%" pattern="#.#%" />

35

<c:out value="Parsed percentage: ${percentage}" />

36

37

<!-- Parse with error handling -->

38

<c:catch var="parseError">

39

<fmt:parseNumber var="number" value="${userInput}" />

40

</c:catch>

41

42

<c:if test="${parseError != null}">

43

<div class="error">Failed to parse number: ${parseError.message}</div>

44

</c:if>

45

```

46

47

### Number Formatting

48

49

Formats numeric values as strings with locale-appropriate formatting, currency symbols, and percentage representation.

50

51

```jsp { .api }

52

<fmt:formatNumber value="${numericValue}" />

53

<fmt:formatNumber value="${numericValue}" type="number|currency|percent" />

54

<fmt:formatNumber value="${numericValue}" pattern="customPattern" />

55

<fmt:formatNumber value="${numericValue}" maxFractionDigits="digits" />

56

<fmt:formatNumber value="${numericValue}" minFractionDigits="digits" />

57

<fmt:formatNumber value="${numericValue}" var="variableName" />

58

```

59

60

**Usage Example:**

61

62

```jsp

63

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

64

65

<!-- Format as currency -->

66

<fmt:formatNumber value="${product.price}" type="currency" />

67

68

<!-- Format as percentage -->

69

<fmt:formatNumber value="${successRate}" type="percent" maxFractionDigits="1" />

70

71

<!-- Custom pattern -->

72

<fmt:formatNumber value="${distance}" pattern="#,##0.00 km" />

73

```

74

75

### Date Parsing

76

77

Parses string representations of dates into Date objects with locale and pattern support.

78

79

```jsp { .api }

80

<fmt:parseDate var="variableName" value="${dateString}" />

81

<fmt:parseDate var="variableName" value="${dateString}" type="date|time|both" />

82

<fmt:parseDate var="variableName" value="${dateString}" pattern="customPattern" />

83

<fmt:parseDate var="variableName" value="${dateString}" parseLocale="${locale}" />

84

```

85

86

### Date Formatting

87

88

Formats Date objects as strings with locale-appropriate formatting and customizable patterns.

89

90

```jsp { .api }

91

<fmt:formatDate value="${dateValue}" />

92

<fmt:formatDate value="${dateValue}" type="date|time|both" />

93

<fmt:formatDate value="${dateValue}" pattern="customPattern" />

94

<fmt:formatDate value="${dateValue}" dateStyle="default|short|medium|long|full" />

95

<fmt:formatDate value="${dateValue}" timeStyle="default|short|medium|long|full" />

96

<fmt:formatDate value="${dateValue}" var="variableName" />

97

```

98

99

**Usage Example:**

100

101

```jsp

102

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

103

104

<!-- Format current date -->

105

<fmt:formatDate value="${now}" type="date" dateStyle="long" />

106

107

<!-- Format with custom pattern -->

108

<fmt:formatDate value="${event.startTime}" pattern="MMM dd, yyyy 'at' HH:mm" />

109

110

<!-- Format time only -->

111

<fmt:formatDate value="${appointment.time}" type="time" timeStyle="short" />

112

```

113

114

### Message Localization

115

116

Retrieves localized messages from resource bundles based on the current locale.

117

118

```jsp { .api }

119

<fmt:message key="messageKey" />

120

<fmt:message key="messageKey" var="variableName" />

121

<fmt:message key="messageKey" bundle="${bundleVar}" />

122

<fmt:message key="messageKey">

123

<fmt:param value="${parameter1}" />

124

<fmt:param value="${parameter2}" />

125

</fmt:message>

126

```

127

128

### Locale Management

129

130

Sets and manages the current locale for formatting operations.

131

132

```jsp { .api }

133

<fmt:setLocale value="${localeString}" />

134

<fmt:setLocale value="${localeString}" scope="page|request|session|application" />

135

```

136

137

### Resource Bundle Management

138

139

Loads and manages resource bundles for message localization.

140

141

```jsp { .api }

142

<fmt:bundle basename="bundleName">

143

<!-- Scoped bundle usage -->

144

</fmt:bundle>

145

146

<fmt:setBundle basename="bundleName" var="bundleVar" />

147

```

148

149

**Usage Example:**

150

151

```jsp

152

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

153

154

<!-- Set locale -->

155

<fmt:setLocale value="${user.preferredLocale}" />

156

157

<!-- Load resource bundle -->

158

<fmt:setBundle basename="messages" var="msgs" />

159

160

<!-- Display localized messages -->

161

<fmt:message key="welcome.message" bundle="${msgs}">

162

<fmt:param value="${user.name}" />

163

</fmt:message>

164

165

<!-- Conditional locale formatting -->

166

<c:choose>

167

<c:when test="${user.locale == 'en_US'}">

168

<fmt:formatDate value="${order.date}" pattern="MM/dd/yyyy" />

169

</c:when>

170

<c:otherwise>

171

<fmt:formatDate value="${order.date}" pattern="dd/MM/yyyy" />

172

</c:otherwise>

173

</c:choose>

174

```

175

176

## Error Handling

177

178

JSTL formatting tags can throw the following exceptions:

179

180

- **ParseException**: When parsing fails due to invalid format

181

- **IllegalArgumentException**: Invalid pattern or parameter values

182

- **MissingResourceException**: When resource bundles or keys are not found

183

- **JspException**: General JSP processing errors

184

185

Common error scenarios:

186

- Invalid date/number formats in parsing operations

187

- Missing or malformed resource bundle files

188

- Incorrect locale specifications

189

- Invalid pattern strings for custom formatting