|
| 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 | +} |
0 commit comments