Skip to content

Commit 460a317

Browse files
ochejapivovarit
authored andcommitted
BAEL-1602 (eugenp#3926)
* Implement examples for ASCII Art in Java * Remove deprecated use of ControllerClassNameHandlerMapping * Implement examples for JSTL core tag * Implement examples for JSTL formatting tags * Implement examples for JSTL SQL tags * Implement examples for JSTL XML tags * Implement examples for JSTL function tags * Setup controllers package scanning * Add link to article on jstl * naming change * Add jstl maven repository
1 parent 5a44224 commit 460a317

15 files changed

Lines changed: 1010 additions & 8 deletions

File tree

‎spring-mvc-xml/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
1515
- [Geolocation by IP in Java](http://www.baeldung.com/geolocation-by-ip-with-maxmind)
1616
- [Guide to JavaServer Pages (JSP)](http://www.baeldung.com/jsp)
1717
- [Exploring SpringMVC’s Form Tag Library](http://www.baeldung.com/spring-mvc-form-tags)
18+
- [Guide to JSTL](http://www.baeldung.com/guide-to-jstl)

‎spring-mvc-xml/pom.xml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737

3838
<!-- web -->
3939

40+
<dependency>
41+
<groupId>com.fasterxml.jackson.core</groupId>
42+
<artifactId>jackson-databind</artifactId>
43+
<version>2.9.2</version>
44+
</dependency>
45+
46+
4047
<dependency>
4148
<groupId>javax.servlet</groupId>
4249
<artifactId>javax.servlet-api</artifactId>
@@ -57,6 +64,13 @@
5764
<version>${hibernate-validator.version}</version>
5865
</dependency>
5966

67+
<dependency>
68+
<groupId>mysql</groupId>
69+
<artifactId>mysql-connector-java</artifactId>
70+
<version>6.0.6</version>
71+
</dependency>
72+
73+
6074
<!-- Json conversion -->
6175
<dependency>
6276
<groupId>com.fasterxml.jackson.core</groupId>
@@ -108,6 +122,14 @@
108122

109123
</build>
110124

125+
<repositories>
126+
<repository>
127+
<id>1</id>
128+
<name>jstl</name>
129+
<url>https://mvnrepository.com/artifact/javax.servlet/jstl</url>
130+
</repository>
131+
</repositories>
132+
111133
<properties>
112134
<!-- Spring -->
113135
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.jstl.bundles;
2+
3+
import java.util.ListResourceBundle;
4+
5+
public class CustomMessage_en extends ListResourceBundle {
6+
@Override
7+
protected Object[][] getContents() {
8+
return contents;
9+
}
10+
11+
static final Object[][] contents = {
12+
{"verb.go", "go"},
13+
{"verb.come", "come"},
14+
{"verb.sit", "sit"},
15+
{"verb.stand", "stand"}
16+
};
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.jstl.bundles;
2+
3+
import java.util.ListResourceBundle;
4+
5+
public class CustomMessage_fr_FR extends ListResourceBundle {
6+
@Override
7+
protected Object[][] getContents() {
8+
return contents;
9+
}
10+
11+
static final Object[][] contents = {
12+
{"verb.go", "aller"},
13+
{"verb.come", "venir"},
14+
{"verb.sit", "siéger"},
15+
{"verb.stand", "se lever"}
16+
};
17+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.baeldung.jstl.controllers;
2+
3+
import com.baeldung.jstl.dbaccess.SQLConnection;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.core.io.FileSystemResource;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.Model;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.ResponseBody;
13+
import org.springframework.web.servlet.ModelAndView;
14+
15+
import javax.annotation.PostConstruct;
16+
import javax.servlet.ServletContext;
17+
import javax.servlet.http.HttpServletRequest;
18+
import javax.servlet.http.HttpServletResponse;
19+
import java.io.File;
20+
import java.sql.*;
21+
import java.util.Calendar;
22+
23+
@Controller
24+
public class JSTLController {
25+
private static final Logger LOGGER = LoggerFactory.getLogger(JSTLController.class.getName());
26+
27+
@Autowired
28+
ServletContext servletContext;
29+
30+
@PostConstruct
31+
public void init() {
32+
PreparedStatement preparedStatement = null;
33+
Connection connection = null;
34+
try {
35+
connection = SQLConnection.getConnection();
36+
preparedStatement = connection.prepareStatement("create table IF NOT EXISTS USERS " +
37+
"( id int not null primary key AUTO_INCREMENT," +
38+
" email VARCHAR(255) not null, first_name varchar (255), last_name varchar (255), registered DATE)");
39+
int status = preparedStatement.executeUpdate();
40+
preparedStatement = connection.prepareStatement("SELECT COUNT(*) AS total FROM USERS;");
41+
ResultSet result = preparedStatement.executeQuery();
42+
if(result!=null) {
43+
result.next();
44+
if (result.getInt("total") == 0) {
45+
generateDummy(connection);
46+
}
47+
} else {
48+
generateDummy(connection);
49+
}
50+
} catch (Exception e) {
51+
LOGGER.error(e.getMessage());
52+
} finally {
53+
try {
54+
if (preparedStatement != null) {
55+
preparedStatement.close();
56+
}
57+
if (connection != null) {
58+
connection.close();
59+
}
60+
} catch (SQLException e) {
61+
LOGGER.error(e.getMessage());
62+
}
63+
64+
}
65+
}
66+
67+
@RequestMapping(value = "/core_tags", method = RequestMethod.GET)
68+
public ModelAndView coreTags(final Model model) {
69+
ModelAndView mv = new ModelAndView("core_tags");
70+
return mv;
71+
}
72+
73+
@RequestMapping(value = "/core_tags_redirect", method = RequestMethod.GET)
74+
public ModelAndView coreTagsRedirect(final Model model) {
75+
ModelAndView mv = new ModelAndView("core_tags_redirect");
76+
return mv;
77+
}
78+
79+
80+
@RequestMapping(value = "/formatting_tags", method = RequestMethod.GET)
81+
public ModelAndView formattingTags(final Model model) {
82+
ModelAndView mv = new ModelAndView("formatting_tags");
83+
return mv;
84+
}
85+
86+
@RequestMapping(value = "/sql_tags", method = RequestMethod.GET)
87+
public ModelAndView sqlTags(final Model model) {
88+
ModelAndView mv = new ModelAndView("sql_tags");
89+
return mv;
90+
}
91+
92+
@RequestMapping(value = "/xml_tags", method = RequestMethod.GET)
93+
public ModelAndView xmlTags(final Model model) {
94+
ModelAndView mv = new ModelAndView("xml_tags");
95+
return mv;
96+
}
97+
98+
@RequestMapping(value = "/items_xml", method = RequestMethod.GET)
99+
@ResponseBody public FileSystemResource getFile(HttpServletRequest request, HttpServletResponse response) {
100+
response.setContentType("application/xml");
101+
return new FileSystemResource(new File(servletContext.getRealPath("/WEB-INF/items.xsl")));
102+
}
103+
104+
@RequestMapping(value = "/function_tags", method = RequestMethod.GET)
105+
public ModelAndView functionTags(final Model model) {
106+
ModelAndView mv = new ModelAndView("function_tags");
107+
return mv;
108+
}
109+
110+
111+
private void generateDummy(Connection connection) throws SQLException {
112+
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USERS " +
113+
"(email, first_name, last_name, registered) VALUES (?, ?, ?, ?);");
114+
preparedStatement.setString(1, "patrick@baeldung.com");
115+
preparedStatement.setString(2, "Patrick");
116+
preparedStatement.setString(3, "Frank");
117+
preparedStatement.setDate(4, new Date(Calendar.getInstance().getTimeInMillis()));
118+
preparedStatement.addBatch();
119+
preparedStatement.setString(1, "bfrank@baeldung.com");
120+
preparedStatement.setString(2, "Benjamin");
121+
preparedStatement.setString(3, "Franklin");
122+
preparedStatement.setDate(4, new Date(Calendar.getInstance().getTimeInMillis()));
123+
preparedStatement.executeBatch();
124+
}
125+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.jstl.dbaccess;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.sql.Connection;
7+
import java.sql.DriverManager;
8+
import java.sql.SQLException;
9+
import java.util.Properties;
10+
11+
public class SQLConnection {
12+
private static String userName = "root";
13+
private static String password = "";
14+
private static final Logger LOGGER = LoggerFactory.getLogger(SQLConnection.class.getName());
15+
16+
public static Connection getConnection() throws Exception {
17+
LOGGER.error("connecting...");
18+
Class.forName("com.mysql.cj.jdbc.Driver");
19+
LOGGER.error("class checked...");
20+
Connection conn = null;
21+
Properties connectionProps = new Properties();
22+
connectionProps.put("user", userName);
23+
connectionProps.put("password", password);
24+
conn = DriverManager.getConnection(
25+
"jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false",
26+
connectionProps);
27+
LOGGER.info("Connected to database");
28+
return conn;
29+
}
30+
}

‎spring-mvc-xml/src/main/resources/webMvcConfig.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</mvc:message-converters>
2424
</mvc:annotation-driven>
2525

26-
<context:component-scan base-package="com.baeldung.spring.controller"/>
26+
<context:component-scan base-package="com.baeldung.spring.controller, com.baeldung.jstl.controllers"/>
2727

2828
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
2929
<property name="prefix" value="/WEB-INF/view/"/>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version = "1.0"?>
2+
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
3+
4+
<xsl:output method = "html" indent = "yes"/>
5+
<xsl:param name = "bgColor"/>
6+
7+
<xsl:template match = "/">
8+
<html>
9+
<body>
10+
<xsl:apply-templates/>
11+
</body>
12+
</html>
13+
</xsl:template>
14+
15+
<xsl:template match = "items">
16+
<table border = "1" width = "40%" bgColor = "{$bgColor}">
17+
<xsl:for-each select = "item">
18+
<tr>
19+
<td><i><xsl:value-of select = "name"/></i></td>
20+
<td><xsl:value-of select = "category"/></td>
21+
<td><xsl:value-of select = "price"/></td>
22+
</tr>
23+
</xsl:for-each>
24+
</table>
25+
</xsl:template>
26+
27+
</xsl:stylesheet>

‎spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
88

99

10-
<context:component-scan base-package="com.baeldung.spring.controller"/>
10+
<context:component-scan base-package="com.baeldung.spring.controller, com.baeldung.jstl.controllers"/>
1111

1212
<!-- Start: Mapping by bean name (BeanNameUrlHandlerMapping) -->
1313
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
@@ -40,12 +40,6 @@
4040
<bean id="helloController" class="com.baeldung.spring.controller.HelloController"/>
4141
<!-- End: Mapping by SimpleUrlHandlerMapping -->
4242

43-
<!-- Start: Mapping by ControllerClassNameHandlerMapping -->
44-
<bean
45-
class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
46-
<property name="caseSensitive" value="true"/>
47-
<property name="order" value="0"/>
48-
</bean>
4943

5044
<bean class="com.baeldung.spring.controller.HelloGuestController"/>
5145
<!-- End: Mapping by ControllerClassNameHandlerMapping -->
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<%@ page import="java.util.Random" %>
2+
<%@ page import="java.util.Date" %>
3+
<%@ page import="java.util.Calendar" %>
4+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
5+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
6+
<html>
7+
<head>
8+
<c:set value="JSTL Core Tags Example" var="pageTitle"/>
9+
<title>
10+
<c:out value="${pageTitle}"/>
11+
</title>
12+
</head>
13+
<body>
14+
<h1>
15+
<c:out value="${pageTitle}"/>
16+
</h1>
17+
<c:remove var="pageTitle"/>
18+
<p><c:out value="${pageTitle}"/></p>
19+
<div>
20+
<h3>
21+
<c:out value="<c:catch> Example"/>
22+
</h3>
23+
<c:catch var ="exceptionThrown">
24+
<% int x = Integer.valueOf("a");%>
25+
</c:catch>
26+
27+
<c:if test = "${exceptionThrown != null}">
28+
<p>The exception is : ${exceptionThrown} <br />
29+
There is an exception: ${exceptionThrown.message}</p>
30+
</c:if>
31+
</div>
32+
<div>
33+
34+
<h3>
35+
<c:out value="<c:choose> Example"/>
36+
<c:set value="<%= Calendar.getInstance().get(Calendar.SECOND)%>" var="seconds"/>
37+
</h3>
38+
<p>
39+
<c:choose>
40+
<c:when test="${seconds le 30 }">
41+
<c:out value="${seconds} is less than 30"/>
42+
</c:when>
43+
<c:when test="${seconds eq 30 }">
44+
<c:out value="${seconds} is equal to 30"/>
45+
</c:when>
46+
<c:otherwise>
47+
<c:out value="${seconds} is greater than 30"/>
48+
</c:otherwise>
49+
</c:choose>
50+
</p>
51+
</div>
52+
<div>
53+
<h3>
54+
<c:out value="<c:import> Example"/>
55+
</h3>
56+
57+
<c:import var = "data" url = "http://www.example.com"/>
58+
<c:out value = "${data}"/>
59+
</div>
60+
<div>
61+
62+
<h3>
63+
<c:out value="<c:forEach> Example"/>
64+
</h3>
65+
66+
<c:forEach var = "i" items="1,4,5,6,7,8,9">
67+
Item <c:out value = "No. ${i}"/><p>
68+
</c:forEach>
69+
</div>
70+
<div>
71+
72+
<h3>
73+
<c:out value="<c:forToken> Example"/>
74+
</h3>
75+
76+
<c:forTokens items = "Patrick:Wilson:Ibrahima:Chris" delims = ":" var = "name">
77+
<c:out value = "Name: ${name}"/><p>
78+
</c:forTokens>
79+
</div>
80+
<div>
81+
82+
<h3>
83+
<c:out value="<c:url> and <c:param> Example"/>
84+
</h3>
85+
<c:url value = "/core_tags" var = "myURL">
86+
<c:param name = "parameter_1" value = "1234"/>
87+
<c:param name = "parameter_2" value = "abcd"/>
88+
</c:url>
89+
<c:out value = "URL: ${myURL}"/>
90+
</div>
91+
</body>
92+
</html>

0 commit comments

Comments
 (0)