From 2fde1b4713c7a8b40679f8567154f78b1989e623 Mon Sep 17 00:00:00 2001 From: Ocheja Patrick Date: Sun, 4 Mar 2018 00:21:11 +0900 Subject: [PATCH 01/11] Implement examples for ASCII Art in Java --- .../java/com/baeldung/asciiart/AsciiArt.java | 62 +++++++++++++++++++ .../com/baeldung/asciiart/AsciiArtTest.java | 20 ++++++ 2 files changed, 82 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java create mode 100644 core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java diff --git a/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java b/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java new file mode 100644 index 000000000000..081717b4fa0e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/asciiart/AsciiArt.java @@ -0,0 +1,62 @@ +package com.baeldung.asciiart; + +import java.awt.Font; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; + +public class AsciiArt { + + public AsciiArt() { + } + + public void drawString(String text, String artChar, Settings settings) { + BufferedImage image = getImageIntegerMode(settings.width, settings.height); + + Graphics2D graphics2D = getGraphics2D(image.getGraphics(), settings); + graphics2D.drawString(text, 6, ((int) (settings.height * 0.67))); + + for (int y = 0; y < settings.height; y++) { + StringBuilder stringBuilder = new StringBuilder(); + + for (int x = 0; x < settings.width; x++) { + stringBuilder.append(image.getRGB(x, y) == -16777216 ? " " : artChar); + } + + if (stringBuilder.toString() + .trim() + .isEmpty()) { + continue; + } + + System.out.println(stringBuilder); + } + + } + + private BufferedImage getImageIntegerMode(int width, int height) { + return new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + } + + private Graphics2D getGraphics2D(Graphics graphics, Settings settings) { + graphics.setFont(settings.font); + + Graphics2D graphics2D = (Graphics2D) graphics; + graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + + return graphics2D; + } + + public class Settings { + public Font font; + public int width; + public int height; + + public Settings(Font font, int width, int height) { + this.font = font; + this.width = width; + this.height = height; + } + } +} diff --git a/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java b/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java new file mode 100644 index 000000000000..103681894e14 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/asciiart/AsciiArtTest.java @@ -0,0 +1,20 @@ +package com.baeldung.asciiart; + +import java.awt.Font; + +import org.junit.Test; + +import com.baeldung.asciiart.AsciiArt.Settings; + +public class AsciiArtTest { + + @Test + public void givenTextWithAsciiCharacterAndSettings_shouldPrintAsciiArt() { + AsciiArt asciiArt = new AsciiArt(); + String text = "BAELDUNG"; + Settings settings = asciiArt.new Settings(new Font("SansSerif", Font.BOLD, 24), text.length() * 30, 30); // 30 pixel width per character + + asciiArt.drawString(text, "*", settings); + } + +} From 3ef78579bcd43a7f194258c20e6195fa4ecbf942 Mon Sep 17 00:00:00 2001 From: ocheja Date: Tue, 27 Mar 2018 18:23:12 +0900 Subject: [PATCH 02/11] Remove deprecated use of ControllerClassNameHandlerMapping --- spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml index 032457da436c..5c8fa611edd3 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml @@ -40,12 +40,6 @@ - - - - - From 2285841c6efd2c12f74194c4d01fa0a4d43d45e4 Mon Sep 17 00:00:00 2001 From: ocheja Date: Wed, 28 Mar 2018 14:35:46 +0900 Subject: [PATCH 03/11] Implement examples for JSTL core tag --- .../jstl/controllers/JSTLController.java | 24 +++++ .../main/webapp/WEB-INF/view/core_tags.jsp | 92 +++++++++++++++++++ .../WEB-INF/view/core_tags_redirect.jsp | 16 ++++ 3 files changed, 132 insertions(+) create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags_redirect.jsp diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java new file mode 100644 index 000000000000..2a34b4a16ddc --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java @@ -0,0 +1,24 @@ +package com.baeldung.jstl.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class JSTLController { + + @RequestMapping(value = "/core_tags", method = RequestMethod.GET) + public ModelAndView coreTags(final Model model) { + ModelAndView mv = new ModelAndView("core_tags"); + return mv; + } + + @RequestMapping(value = "/core_tags_redirect", method = RequestMethod.GET) + public ModelAndView coreTagsRedirect(final Model model) { + ModelAndView mv = new ModelAndView("core_tags_redirect"); + return mv; + } + +} diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags.jsp new file mode 100644 index 000000000000..f5defd101e6b --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags.jsp @@ -0,0 +1,92 @@ +<%@ page import="java.util.Random" %> +<%@ page import="java.util.Date" %> +<%@ page import="java.util.Calendar" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + + <c:out value="${pageTitle}"/> + + + +

+ +

+ +

+
+

+ +

+ + <% int x = Integer.valueOf("a");%> + + + +

The exception is : ${exceptionThrown}
+ There is an exception: ${exceptionThrown.message}

+
+
+
+ +

+ + +

+

+ + + + + + + + + + + +

+
+
+

+ +

+ + + +
+
+ +

+ +

+ + + Item

+ +

+
+ +

+ +

+ + +

+ +

+
+ +

+ +

+ + + + + +
+ + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags_redirect.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags_redirect.jsp new file mode 100644 index 000000000000..e8ab0a60c6ed --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags_redirect.jsp @@ -0,0 +1,16 @@ +<%@ page import="java.util.Random" %> +<%@ page import="java.util.Date" %> +<%@ page import="java.util.Calendar" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + + <c:out value="${pageTitle}"/> + + + + + + \ No newline at end of file From ef1e1992441fe1f23d1f596e1987b4dde720ec52 Mon Sep 17 00:00:00 2001 From: ocheja Date: Sat, 31 Mar 2018 15:00:37 +0900 Subject: [PATCH 04/11] Implement examples for JSTL formatting tags --- .../jstl/bundles/CustomMessage_en.java | 17 ++ .../jstl/bundles/CustomMessage_fr_FR.java | 17 ++ .../jstl/controllers/JSTLController.java | 6 + .../webapp/WEB-INF/view/formatting_tags.jsp | 210 ++++++++++++++++++ 4 files changed, 250 insertions(+) create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/jstl/bundles/CustomMessage_en.java create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/jstl/bundles/CustomMessage_fr_FR.java create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jstl/bundles/CustomMessage_en.java b/spring-mvc-xml/src/main/java/com/baeldung/jstl/bundles/CustomMessage_en.java new file mode 100644 index 000000000000..ac27c0cfa286 --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/bundles/CustomMessage_en.java @@ -0,0 +1,17 @@ +package com.baeldung.jstl.bundles; + +import java.util.ListResourceBundle; + +public class CustomMessage_en extends ListResourceBundle { + @Override + protected Object[][] getContents() { + return contents; + } + + static final Object[][] contents = { + {"verb.go", "go"}, + {"verb.come", "come"}, + {"verb.sit", "sit"}, + {"verb.stand", "stand"} + }; +} diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jstl/bundles/CustomMessage_fr_FR.java b/spring-mvc-xml/src/main/java/com/baeldung/jstl/bundles/CustomMessage_fr_FR.java new file mode 100644 index 000000000000..44f1c6a02f4b --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/bundles/CustomMessage_fr_FR.java @@ -0,0 +1,17 @@ +package com.baeldung.jstl.bundles; + +import java.util.ListResourceBundle; + +public class CustomMessage_fr_FR extends ListResourceBundle { + @Override + protected Object[][] getContents() { + return contents; + } + + static final Object[][] contents = { + {"verb.go", "aller"}, + {"verb.come", "venir"}, + {"verb.sit", "siéger"}, + {"verb.stand", "se lever"} + }; +} diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java index 2a34b4a16ddc..bb49e9e90230 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java @@ -21,4 +21,10 @@ public ModelAndView coreTagsRedirect(final Model model) { return mv; } + + @RequestMapping(value = "/formatting_tags", method = RequestMethod.GET) + public ModelAndView formattingTags(final Model model) { + ModelAndView mv = new ModelAndView("formatting_tags"); + return mv; + } } diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp new file mode 100644 index 000000000000..479374927300 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp @@ -0,0 +1,210 @@ +<%@ page import="java.util.Calendar" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> + + + + + <c:out value="${pageTitle}"/> + + + +
+

+ +

+ + +

Formatted Number - Currency:

+ +

Formatted Number - Maximum Integer Digits:

+ +

Formatted Number - Maximum Fraction Digits:

+ +

Formatted Number - Grouping:

+ +

Formatted Number - Percent with Maximum Integer Digits:

+ +

Formatted Number - Percent with Minimum Fraction Digits:

+ +

Formatted Number Percent with Minimum Integer Digits:

+ +

Formatted Number - with Pattern:

+ +

Formatted Number - Currency with Internationalization : + + +

+
+
+ +

+ +

+ +

Parsed Number :

+ +

Parsed Number - with Integer Only set to True:

+
+
+

+ + +

+ +

Formatted Date - with type set to time:

+ +

Formatted Date - with type set to date:

+ +

Formatted Date - with type set to both:

+ +

Formatted Date - with short style for both date and time:

+ +

Formatted Date - with medium style for both date and time:

+ +

Formatted Date - with long style for both date and time:

+ +

Formatted Date - with pattern:

+
+
+ +

+ +

+ + + +

Parsed Date:

+
+
+ +

+ +

+ +

+ +
+
+
+
+
+

+
+
+ +

+ +

+ +

+ + +
+
+
+
+
+

+
+
+ +

+ +

+ +

+ + +
+
+
+
+

+
+
+ +

+ +

+ + + + + + + + + + + +
+

+ + Time being formatted: + + + +

+
+ + + + + +
+
+ +
+ +

+ +

+ +

Current Date with Default Time Zone:

+

Change Time Zone to GMT+9

+ +

Date in Changed Zone:

+
+
+ +

+ +

+ + + +
+
+
+
+
+ + From f3fd4cbde5fc1dc86debd8ec8c9d5f14c4e89153 Mon Sep 17 00:00:00 2001 From: ocheja Date: Mon, 2 Apr 2018 21:36:01 +0900 Subject: [PATCH 05/11] Implement examples for JSTL SQL tags --- spring-mvc-xml/pom.xml | 14 ++ .../jstl/controllers/JSTLController.java | 67 ++++++ .../baeldung/jstl/dbaccess/SQLConnection.java | 30 +++ .../src/main/webapp/WEB-INF/mvc-servlet.xml | 2 +- .../webapp/WEB-INF/view/formatting_tags.jsp | 3 + .../src/main/webapp/WEB-INF/view/sql_tags.jsp | 198 ++++++++++++++++++ 6 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 spring-mvc-xml/src/main/java/com/baeldung/jstl/dbaccess/SQLConnection.java create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/sql_tags.jsp diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index 049a3fec82ea..81e0422274da 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -37,6 +37,13 @@ + + com.fasterxml.jackson.core + jackson-databind + 2.9.2 + + + javax.servlet javax.servlet-api @@ -57,6 +64,13 @@ ${hibernate-validator.version} + + mysql + mysql-connector-java + 6.0.6 + + + com.fasterxml.jackson.core diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java index bb49e9e90230..369b6fc949a7 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java @@ -1,13 +1,58 @@ package com.baeldung.jstl.controllers; +import com.baeldung.jstl.dbaccess.SQLConnection; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; +import javax.annotation.PostConstruct; +import java.sql.*; +import java.util.Calendar; + @Controller public class JSTLController { + private static final Logger LOGGER = LoggerFactory.getLogger(JSTLController.class.getName()); + + @PostConstruct + public void init() { + PreparedStatement preparedStatement = null; + Connection connection = null; + try { + connection = SQLConnection.getConnection(); + preparedStatement = connection.prepareStatement("create table IF NOT EXISTS USERS " + + "( id int not null primary key AUTO_INCREMENT," + + " email VARCHAR(255) not null, first_name varchar (255), last_name varchar (255), registered DATE)"); + int status = preparedStatement.executeUpdate(); + preparedStatement = connection.prepareStatement("SELECT COUNT(*) AS total FROM USERS;"); + ResultSet result = preparedStatement.executeQuery(); + if(result!=null) { + result.next(); + if (result.getInt("total") == 0) { + generateDummy(connection); + } + } else { + generateDummy(connection); + } + } catch (Exception e) { + LOGGER.error(e.getMessage()); + } finally { + try { + if (preparedStatement != null) { + preparedStatement.close(); + } + if (connection != null) { + connection.close(); + } + } catch (SQLException e) { + LOGGER.error(e.getMessage()); + } + + } + } @RequestMapping(value = "/core_tags", method = RequestMethod.GET) public ModelAndView coreTags(final Model model) { @@ -27,4 +72,26 @@ public ModelAndView formattingTags(final Model model) { ModelAndView mv = new ModelAndView("formatting_tags"); return mv; } + + @RequestMapping(value = "/sql_tags", method = RequestMethod.GET) + public ModelAndView sqlTags(final Model model) { + ModelAndView mv = new ModelAndView("sql_tags"); + return mv; + } + + + private void generateDummy(Connection connection) throws SQLException { + PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USERS " + + "(email, first_name, last_name, registered) VALUES (?, ?, ?, ?);"); + preparedStatement.setString(1, "patrick@baeldung.com"); + preparedStatement.setString(2, "Patrick"); + preparedStatement.setString(3, "Frank"); + preparedStatement.setDate(4, new Date(Calendar.getInstance().getTimeInMillis())); + preparedStatement.addBatch(); + preparedStatement.setString(1, "bfrank@baeldung.com"); + preparedStatement.setString(2, "Benjamin"); + preparedStatement.setString(3, "Franklin"); + preparedStatement.setDate(4, new Date(Calendar.getInstance().getTimeInMillis())); + preparedStatement.executeBatch(); + } } diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jstl/dbaccess/SQLConnection.java b/spring-mvc-xml/src/main/java/com/baeldung/jstl/dbaccess/SQLConnection.java new file mode 100644 index 000000000000..441009ed7736 --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/dbaccess/SQLConnection.java @@ -0,0 +1,30 @@ +package com.baeldung.jstl.dbaccess; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +public class SQLConnection { + private static String userName = "root"; + private static String password = ""; + private static final Logger LOGGER = LoggerFactory.getLogger(SQLConnection.class.getName()); + + public static Connection getConnection() throws Exception { + LOGGER.error("connecting..."); + Class.forName("com.mysql.cj.jdbc.Driver"); + LOGGER.error("class checked..."); + Connection conn = null; + Properties connectionProps = new Properties(); + connectionProps.put("user", userName); + connectionProps.put("password", password); + conn = DriverManager.getConnection( + "jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false", + connectionProps); + LOGGER.info("Connected to database"); + return conn; + } +} diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml index 5c8fa611edd3..f664472652ea 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml @@ -7,7 +7,7 @@ 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"> - + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp index 479374927300..1b5cb93cf2c3 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp @@ -10,6 +10,9 @@ +

+ +

diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/sql_tags.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/sql_tags.jsp new file mode 100644 index 000000000000..df10e41c0412 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/sql_tags.jsp @@ -0,0 +1,198 @@ +<%@ page import="java.util.Random" %> +<%@ page import="java.util.Date" %> +<%@ page import="java.util.Calendar" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %> + + + + + <c:out value="${pageTitle}"/> + + + +

+ +

+ + + +
+ +

+ +

+ + SELECT * from USERS; + + + + + + + + + + + + + + + + + + + +
User IDFirst NameLast NameEmailRegistered
+
+
+

+ +

+ + + INSERT INTO USERS(first_name, last_name, email) VALUES ('Grace', 'Adams', 'gracea@domain.com'); + + + SELECT * from USERS; + + + + + + + + + + + + + + + + + + + +
User IDFirst NameLast NameEmailRegistered
+
+
+ +

+ +

+ + + DELETE FROM USERS WHERE email = ? + + + + SELECT * from USERS; + + + + + + + + + + + + + + + + + + + +
User IDFirst NameLast NameEmailRegistered
+ +
+
+ +

+ +

+ <% + Date registered = new Date("2018/03/31"); + String email = "patrick@baeldung.com"; + %> + + + UPDATE Users SET registered = ? WHERE email = ? + + + + + SELECT * from USERS; + + + + + + + + + + + + + + + + + + + +
User IDFirst NameLast NameEmailRegistered
+
+
+ +

+ +

+ + + UPDATE Users SET first_name = 'Patrick-Ellis' WHERE email='patrick@baeldung.com' + + + + UPDATE Users SET last_name = 'Nelson' WHERE email = 'patrick@baeldung.com' + + + + INSERT INTO Users(first_name, last_name, email) VALUES ('Grace', 'Adams', 'gracea@domain.com'); + + + + SELECT * from USERS; + + + + + + + + + + + + + + + + + + + +
User IDFirst NameLast NameEmailRegistered
+
+ + \ No newline at end of file From 4ebc75e7ba0f373836e56a289e7d1f33f1de52c7 Mon Sep 17 00:00:00 2001 From: ocheja Date: Tue, 3 Apr 2018 02:16:36 +0900 Subject: [PATCH 06/11] Implement examples for JSTL XML tags --- .../jstl/controllers/JSTLController.java | 22 +++++ .../src/main/webapp/WEB-INF/items.xsl | 27 +++++ .../src/main/webapp/WEB-INF/view/xml_tags.jsp | 99 +++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/items.xsl create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java index 369b6fc949a7..d2e13488a775 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java @@ -3,13 +3,20 @@ import com.baeldung.jstl.dbaccess.SQLConnection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.FileSystemResource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.annotation.PostConstruct; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.sql.*; import java.util.Calendar; @@ -17,6 +24,9 @@ public class JSTLController { private static final Logger LOGGER = LoggerFactory.getLogger(JSTLController.class.getName()); + @Autowired + ServletContext servletContext; + @PostConstruct public void init() { PreparedStatement preparedStatement = null; @@ -79,6 +89,18 @@ public ModelAndView sqlTags(final Model model) { return mv; } + @RequestMapping(value = "/xml_tags", method = RequestMethod.GET) + public ModelAndView xmlTags(final Model model) { + ModelAndView mv = new ModelAndView("xml_tags"); + return mv; + } + + @RequestMapping(value = "/items_xml", method = RequestMethod.GET) + @ResponseBody public FileSystemResource getFile(HttpServletRequest request, HttpServletResponse response) { + response.setContentType("application/xml"); + return new FileSystemResource(new File(servletContext.getRealPath("/WEB-INF/items.xsl"))); + } + private void generateDummy(Connection connection) throws SQLException { PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USERS " + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/items.xsl b/spring-mvc-xml/src/main/webapp/WEB-INF/items.xsl new file mode 100644 index 000000000000..42cb719bb54f --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/items.xsl @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
\ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp new file mode 100644 index 000000000000..ce711e794ec9 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp @@ -0,0 +1,99 @@ +<%@ page import="java.util.Random" %> +<%@ page import="java.util.Date" %> +<%@ page import="java.util.Calendar" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %> + + + + + <c:out value="${pageTitle}"/> + + + +

+ +

+ +

+
+

+ +

+

Store Items:

+ + + + Steve Madden + Sneakers + 34 + + + + Pearl Izumi + Sneakers + 80 + + + + Katy Perry + Heels + 72 + + + + + + The name of the first item listed in the store is: + with price $ +
+ + + The second item is : + + + + Document has at least one + element. + +
+ + + + + +
+
+ +

+ +

+
    + +
  • Item Name:
  • +
    +
+
+
+

+ +

+ + + + Item category is Sneakers + + + + Item category is Heels + + + + Unknown category. + + +
+ + + \ No newline at end of file From 824fdd3dad3629622b375bd132adcfe8a05a8e3e Mon Sep 17 00:00:00 2001 From: ocheja Date: Tue, 3 Apr 2018 02:42:18 +0900 Subject: [PATCH 07/11] Implement examples for JSTL function tags --- .../jstl/controllers/JSTLController.java | 6 + .../webapp/WEB-INF/view/function_tags.jsp | 151 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/function_tags.jsp diff --git a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java index d2e13488a775..c69e8a1b2a93 100644 --- a/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java @@ -101,6 +101,12 @@ public ModelAndView xmlTags(final Model model) { return new FileSystemResource(new File(servletContext.getRealPath("/WEB-INF/items.xsl"))); } + @RequestMapping(value = "/function_tags", method = RequestMethod.GET) + public ModelAndView functionTags(final Model model) { + ModelAndView mv = new ModelAndView("function_tags"); + return mv; + } + private void generateDummy(Connection connection) throws SQLException { PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USERS " + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/function_tags.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/function_tags.jsp new file mode 100644 index 000000000000..d45bfd1d516e --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/function_tags.jsp @@ -0,0 +1,151 @@ +<%@ page import="java.util.Random" %> +<%@ page import="java.util.Date" %> +<%@ page import="java.util.Calendar" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix = "fn" + uri = "http://java.sun.com/jsp/jstl/functions" %> + + + + + <c:out value="${pageTitle}"/> + + + +

+ +

+ + + +
+

+ +

+ +

Found 'first' in string

+ +

+
+

+ +

+ +

Found 'first' string

+ + + +

Found 'FIRST' string

+ +

+
+

+ +

+ +

String ends with 'string'

+ +

+
+

+ +

+

With escapeXml() Function:

+

string (1) : ${fn:escapeXml(string1)}

+

string (2) : ${fn:escapeXml(string2)}

+ +

Without escapeXml() Function:

+

string (1) : ${string1}

+

string (2) : ${string2}

+
+
+

+ +

+

Index (1) : ${fn:indexOf(string1, "first")}

+

Index (2) : ${fn:indexOf(string2, "second")}

+
+
+

+ +

+ + +

Final String : ${string4}

+
+
+

+ +

+

Length of String (1) : ${fn:length(string1)}

+

Length of String (2) : ${fn:length(string2)}

+
+
+

+ +

+ +

Final String : ${string3}

+
+
+

+ +

+ +

String starts with 'This'

+
+
+
+

+ +

+ + +

Final sub string : ${string3}

+
+
+

+ +

+ + +

Final sub string : ${string3}

+
+
+

+ +

+ + +

Final sub string : ${string3}

+
+
+

+ +

+ + +

Final string : ${string3}

+
+
+

+ +

+ + +

Final string : ${string3}

+
+
+

+ +

+ +

String (1) Length : ${fn:length(string1)}

+ + +

String (2) Length : ${fn:length(string2)}

+

Final string : "${string2}"

+
+ + \ No newline at end of file From e100527cb3d755a38c4f649912eb3aec1bf92388 Mon Sep 17 00:00:00 2001 From: ocheja Date: Tue, 3 Apr 2018 02:43:15 +0900 Subject: [PATCH 08/11] Setup controllers package scanning --- spring-mvc-xml/src/main/resources/webMvcConfig.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index 37aebe1d1df2..ebb0a14113ea 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -23,7 +23,7 @@ - + From 3a20feb1672cc7902fb3848c6fb7939b3fe14e5c Mon Sep 17 00:00:00 2001 From: ocheja Date: Tue, 3 Apr 2018 02:47:50 +0900 Subject: [PATCH 09/11] Add link to article on jstl --- spring-mvc-xml/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-mvc-xml/README.md b/spring-mvc-xml/README.md index 7a5e8c75e9a6..b84614dd71d6 100644 --- a/spring-mvc-xml/README.md +++ b/spring-mvc-xml/README.md @@ -15,3 +15,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Geolocation by IP in Java](http://www.baeldung.com/geolocation-by-ip-with-maxmind) - [Guide to JavaServer Pages (JSP)](http://www.baeldung.com/jsp) - [Exploring SpringMVC’s Form Tag Library](http://www.baeldung.com/spring-mvc-form-tags) +- [Guide to JSTL](http://www.baeldung.com/guide-to-jstl) From 7c81cf2d554d0cad9bf0600c40f41baba0000e73 Mon Sep 17 00:00:00 2001 From: ocheja Date: Tue, 3 Apr 2018 12:19:41 +0900 Subject: [PATCH 10/11] naming change --- spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp index ce711e794ec9..9d93c3c190ad 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp @@ -69,7 +69,7 @@

-
    +
    • Item Name:
    • From f7e5b886d0bee0d49396cca9085dcfb7ff0d4c93 Mon Sep 17 00:00:00 2001 From: ocheja Date: Tue, 3 Apr 2018 20:34:06 +0900 Subject: [PATCH 11/11] Add jstl maven repository --- spring-mvc-xml/pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index 81e0422274da..47ecdc438dcc 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -122,6 +122,14 @@ + + + 1 + jstl + https://mvnrepository.com/artifact/javax.servlet/jstl + + + 5.0.2.RELEASE