Skip to content
Merged
1 change: 1 addition & 0 deletions spring-mvc-xml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 22 additions & 0 deletions spring-mvc-xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@

<!-- web -->

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>


<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand All @@ -57,6 +64,13 @@
<version>${hibernate-validator.version}</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>


<!-- Json conversion -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down Expand Up @@ -108,6 +122,14 @@

</build>

<repositories>
<repository>
<id>1</id>
<name>jstl</name>
<url>https://mvnrepository.com/artifact/javax.servlet/jstl</url>
</repository>
</repositories>

<properties>
<!-- Spring -->
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}
};
}
Original file line number Diff line number Diff line change
@@ -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"}
};
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion spring-mvc-xml/src/main/resources/webMvcConfig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</mvc:message-converters>
</mvc:annotation-driven>

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

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/>
Expand Down
27 changes: 27 additions & 0 deletions spring-mvc-xml/src/main/webapp/WEB-INF/items.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">

<xsl:output method = "html" indent = "yes"/>
<xsl:param name = "bgColor"/>

<xsl:template match = "/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match = "items">
<table border = "1" width = "40%" bgColor = "{$bgColor}">
<xsl:for-each select = "item">
<tr>
<td><i><xsl:value-of select = "name"/></i></td>
<td><xsl:value-of select = "category"/></td>
<td><xsl:value-of select = "price"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>
8 changes: 1 addition & 7 deletions spring-mvc-xml/src/main/webapp/WEB-INF/mvc-servlet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">


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

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

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

<bean class="com.baeldung.spring.controller.HelloGuestController"/>
<!-- End: Mapping by ControllerClassNameHandlerMapping -->
Expand Down
92 changes: 92 additions & 0 deletions spring-mvc-xml/src/main/webapp/WEB-INF/view/core_tags.jsp
Original file line number Diff line number Diff line change
@@ -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" %>
<html>
<head>
<c:set value="JSTL Core Tags Example" var="pageTitle"/>
<title>
<c:out value="${pageTitle}"/>
</title>
</head>
<body>
<h1>
<c:out value="${pageTitle}"/>
</h1>
<c:remove var="pageTitle"/>
<p><c:out value="${pageTitle}"/></p>
<div>
<h3>
<c:out value="<c:catch> Example"/>
</h3>
<c:catch var ="exceptionThrown">
<% int x = Integer.valueOf("a");%>
</c:catch>

<c:if test = "${exceptionThrown != null}">
<p>The exception is : ${exceptionThrown} <br />
There is an exception: ${exceptionThrown.message}</p>
</c:if>
</div>
<div>

<h3>
<c:out value="<c:choose> Example"/>
<c:set value="<%= Calendar.getInstance().get(Calendar.SECOND)%>" var="seconds"/>
</h3>
<p>
<c:choose>
<c:when test="${seconds le 30 }">
<c:out value="${seconds} is less than 30"/>
</c:when>
<c:when test="${seconds eq 30 }">
<c:out value="${seconds} is equal to 30"/>
</c:when>
<c:otherwise>
<c:out value="${seconds} is greater than 30"/>
</c:otherwise>
</c:choose>
</p>
</div>
<div>
<h3>
<c:out value="<c:import> Example"/>
</h3>

<c:import var = "data" url = "http://www.example.com"/>
<c:out value = "${data}"/>
</div>
<div>

<h3>
<c:out value="<c:forEach> Example"/>
</h3>

<c:forEach var = "i" items="1,4,5,6,7,8,9">
Item <c:out value = "No. ${i}"/><p>
</c:forEach>
</div>
<div>

<h3>
<c:out value="<c:forToken> Example"/>
</h3>

<c:forTokens items = "Patrick:Wilson:Ibrahima:Chris" delims = ":" var = "name">
<c:out value = "Name: ${name}"/><p>
</c:forTokens>
</div>
<div>

<h3>
<c:out value="<c:url> and <c:param> Example"/>
</h3>
<c:url value = "/core_tags" var = "myURL">
<c:param name = "parameter_1" value = "1234"/>
<c:param name = "parameter_2" value = "abcd"/>
</c:url>
<c:out value = "URL: ${myURL}"/>
</div>
</body>
</html>
Loading