From 460a317e6ac767716d9f6cf518e259548e4567b8 Mon Sep 17 00:00:00 2001 From: ocheja Date: Thu, 5 Apr 2018 23:38:10 +0900 Subject: [PATCH] BAEL-1602 (#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 --- spring-mvc-xml/README.md | 1 + spring-mvc-xml/pom.xml | 22 ++ .../jstl/bundles/CustomMessage_en.java | 17 ++ .../jstl/bundles/CustomMessage_fr_FR.java | 17 ++ .../jstl/controllers/JSTLController.java | 125 ++++++++++ .../baeldung/jstl/dbaccess/SQLConnection.java | 30 +++ .../src/main/resources/webMvcConfig.xml | 2 +- .../src/main/webapp/WEB-INF/items.xsl | 27 +++ .../src/main/webapp/WEB-INF/mvc-servlet.xml | 8 +- .../main/webapp/WEB-INF/view/core_tags.jsp | 92 ++++++++ .../WEB-INF/view/core_tags_redirect.jsp | 16 ++ .../webapp/WEB-INF/view/formatting_tags.jsp | 213 ++++++++++++++++++ .../webapp/WEB-INF/view/function_tags.jsp | 151 +++++++++++++ .../src/main/webapp/WEB-INF/view/sql_tags.jsp | 198 ++++++++++++++++ .../src/main/webapp/WEB-INF/view/xml_tags.jsp | 99 ++++++++ 15 files changed, 1010 insertions(+), 8 deletions(-) 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/java/com/baeldung/jstl/controllers/JSTLController.java 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/items.xsl 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 create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/function_tags.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/sql_tags.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/xml_tags.jsp diff --git a/spring-mvc-xml/README.md b/spring-mvc-xml/README.md index 7a5e8c75e9..b84614dd71 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) diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index 049a3fec82..47ecdc438d 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 @@ -108,6 +122,14 @@ + + + 1 + jstl + https://mvnrepository.com/artifact/javax.servlet/jstl + + + 5.0.2.RELEASE 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 0000000000..ac27c0cfa2 --- /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 0000000000..44f1c6a02f --- /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 new file mode 100644 index 0000000000..c69e8a1b2a --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/jstl/controllers/JSTLController.java @@ -0,0 +1,125 @@ +package com.baeldung.jstl.controllers; + +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; + +@Controller +public class JSTLController { + private static final Logger LOGGER = LoggerFactory.getLogger(JSTLController.class.getName()); + + @Autowired + ServletContext servletContext; + + @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) { + 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; + } + + + @RequestMapping(value = "/formatting_tags", method = RequestMethod.GET) + 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; + } + + @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"))); + } + + @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 " + + "(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 0000000000..441009ed77 --- /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/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index 37aebe1d1d..ebb0a14113 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -23,7 +23,7 @@ - + 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 0000000000..42cb719bb5 --- /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/mvc-servlet.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml index 032457da43..f664472652 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"> - + @@ -40,12 +40,6 @@ - - - - - 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 0000000000..f5defd101e --- /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 0000000000..e8ab0a60c6 --- /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 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 0000000000..1b5cb93cf2 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/formatting_tags.jsp @@ -0,0 +1,213 @@ +<%@ 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:

+
+
+ +

+ +

+ + + +
+
+
+
+
+ + 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 0000000000..d45bfd1d51 --- /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 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 0000000000..df10e41c04 --- /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 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 0000000000..9d93c3c190 --- /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