From 0fdbd513bcfdc936228c4485caf0b7e5ff15d294 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 12 Feb 2016 00:09:58 +0100 Subject: [PATCH 01/10] Add form tag example --- .../spring/controller/PersonController.java | 86 +++++++++++++ .../java/org/baeldung/spring/form/Person.java | 120 ++++++++++++++++++ .../main/webapp/WEB-INF/view/personForm.jsp | 88 +++++++++++++ .../main/webapp/WEB-INF/view/personResume.jsp | 61 +++++++++ 4 files changed, 355 insertions(+) create mode 100644 spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java create mode 100644 spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java new file mode 100644 index 0000000000..688e52e51b --- /dev/null +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java @@ -0,0 +1,86 @@ +package org.baeldung.spring.controller; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.validation.Valid; + +import org.baeldung.spring.form.Person; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; + +@Controller +public class PersonController { + + @RequestMapping(value = "/person", method = RequestMethod.GET) + public ModelAndView initForm(final Model model) { + + final List favouriteLanguage = new ArrayList(); + favouriteLanguage.add("Java"); + favouriteLanguage.add("C++"); + favouriteLanguage.add("Perl"); + model.addAttribute("favouriteLanguage", favouriteLanguage); + + final List job = new ArrayList(); + job.add("Full time"); + job.add("Part time"); + model.addAttribute("job", job); + + final Map country = new LinkedHashMap(); + country.put("US", "United Stated"); + country.put("IT", "Italy"); + country.put("UK", "United Kingdom"); + country.put("FR", "Grance"); + model.addAttribute("country", country); + + final List fruit = new ArrayList(); + fruit.add("Banana"); + fruit.add("Mango"); + fruit.add("Apple"); + model.addAttribute("fruit", fruit); + + final List books = new ArrayList(); + books.add("The Great Gatsby"); + books.add("Nineteen Eighty-Four"); + books.add("The Lord of the Rings"); + model.addAttribute("books", books); + + return new ModelAndView("personForm", "person", new Person()); + } + + @RequestMapping(value = "/addPerson", method = RequestMethod.POST) + public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, + final ModelMap model) { + + if (result.hasErrors()) { + return "error"; + } + + model.addAttribute("person", person); + + return "personResume"; + } + // + // protected Map> referenceData(final + // HttpServletRequest request) throws Exception { + // + // final Map> referenceData = new HashMap<>(); + // + // final List favouriteLanguageList = new ArrayList(); + // favouriteLanguageList.add("Java"); + // favouriteLanguageList.add("C++"); + // favouriteLanguageList.add("Perl"); + // referenceData.put("favouriteLanguageList", favouriteLanguageList); + // + // return referenceData; + // + // } +} diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java new file mode 100644 index 0000000000..1b77ab5771 --- /dev/null +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java @@ -0,0 +1,120 @@ +package org.baeldung.spring.form; + +import java.util.List; + +public class Person { + + private long id; + private String name; + private String password; + private String sex; + private String country; + private String book; + private String job; + boolean receiveNewsletter; + private String[] hobbies; + private List favouriteLanguage; + private List fruit; + private String notes; + + public Person() { + super(); + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(final String password) { + this.password = password; + } + + public String getSex() { + return sex; + } + + public void setSex(final String sex) { + this.sex = sex; + } + + public String getCountry() { + return country; + } + + public void setCountry(final String country) { + this.country = country; + } + + public String getJob() { + return job; + } + + public void setJob(final String job) { + this.job = job; + } + + public boolean isReceiveNewsletter() { + return receiveNewsletter; + } + + public void setReceiveNewsletter(final boolean receiveNewsletter) { + this.receiveNewsletter = receiveNewsletter; + } + + public String[] getHobbies() { + return hobbies; + } + + public void setHobbies(final String[] hobbies) { + this.hobbies = hobbies; + } + + public List getFavouriteLanguage() { + return favouriteLanguage; + } + + public void setFavouriteLanguage(final List favouriteLanguage) { + this.favouriteLanguage = favouriteLanguage; + } + + public String getNotes() { + return notes; + } + + public void setNotes(final String notes) { + this.notes = notes; + } + + public List getFruit() { + return fruit; + } + + public void setFruit(final List fruit) { + this.fruit = fruit; + } + + public String getBook() { + return book; + } + + public void setBook(final String book) { + this.book = book; + } + +} diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp new file mode 100644 index 0000000000..f6cf0bf460 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -0,0 +1,88 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + +Form Example - Register a Person + + +

Welcome, Enter The Person Details

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name
Password
Sex + Male:
+ Female: +
Job + +
Country + +
Book + + + + +
Fruit + +
Receive newsletter
Hobbies + Bird watching: + Astronomy: + Snowboarding: +
Favourite languages + +
Notes
+
+ + + + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp new file mode 100644 index 0000000000..81f7fd3411 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp @@ -0,0 +1,61 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + +Spring MVC Form Handling + + + +

Submitted Person Information

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Id :${person.id}
Name :${person.name}
Password :${person.password}
Sex :${person.sex}
Job :${person.job}
Country :${person.country}
Fruit :[]
Book :${person.book}
Receive Newsletter :${person.receiveNewsletter}
Hobbies :[]
Favourite Languages :[]
Notes :${person.notes}
+ + \ No newline at end of file From c1f4c574a86d33f216564990870fb6b14ebdcc82 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 12 Feb 2016 14:38:01 +0100 Subject: [PATCH 02/10] Add form tag example --- .../spring/controller/PersonController.java | 53 +++-- .../java/org/baeldung/spring/form/Person.java | 16 ++ .../src/main/resources/messages.properties | 2 + .../src/main/resources/webMvcConfig.xml | 13 ++ .../main/webapp/WEB-INF/view/personForm.jsp | 187 ++++++++++-------- .../main/webapp/WEB-INF/view/personResume.jsp | 4 + .../src/main/webapp/WEB-INF/web.xml | 3 + 7 files changed, 164 insertions(+), 114 deletions(-) create mode 100644 spring-mvc-xml/src/main/resources/messages.properties diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java index 688e52e51b..d5b68c7516 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java @@ -21,7 +21,28 @@ import org.springframework.web.servlet.ModelAndView; public class PersonController { @RequestMapping(value = "/person", method = RequestMethod.GET) - public ModelAndView initForm(final Model model) { + public ModelAndView showForm(final Model model) { + + initData(model); + return new ModelAndView("personForm", "person", new Person()); + } + + @RequestMapping(value = "/addPerson", method = RequestMethod.POST) + public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, + final ModelMap modelMap, final Model model) { + + if (result.hasErrors()) { + + initData(model); + return "personForm"; + } + + modelMap.addAttribute("person", person); + + return "personResume"; + } + + private void initData(final Model model) { final List favouriteLanguage = new ArrayList(); favouriteLanguage.add("Java"); @@ -52,35 +73,5 @@ public class PersonController { books.add("Nineteen Eighty-Four"); books.add("The Lord of the Rings"); model.addAttribute("books", books); - - return new ModelAndView("personForm", "person", new Person()); } - - @RequestMapping(value = "/addPerson", method = RequestMethod.POST) - public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, - final ModelMap model) { - - if (result.hasErrors()) { - return "error"; - } - - model.addAttribute("person", person); - - return "personResume"; - } - // - // protected Map> referenceData(final - // HttpServletRequest request) throws Exception { - // - // final Map> referenceData = new HashMap<>(); - // - // final List favouriteLanguageList = new ArrayList(); - // favouriteLanguageList.add("Java"); - // favouriteLanguageList.add("C++"); - // favouriteLanguageList.add("Perl"); - // referenceData.put("favouriteLanguageList", favouriteLanguageList); - // - // return referenceData; - // - // } } diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java index 1b77ab5771..3db8cc4377 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java @@ -2,10 +2,17 @@ package org.baeldung.spring.form; import java.util.List; +import org.hibernate.validator.constraints.NotEmpty; +import org.springframework.web.multipart.MultipartFile; + public class Person { private long id; + + @NotEmpty private String name; + + @NotEmpty private String password; private String sex; private String country; @@ -16,6 +23,7 @@ public class Person { private List favouriteLanguage; private List fruit; private String notes; + private MultipartFile file; public Person() { super(); @@ -117,4 +125,12 @@ public class Person { this.book = book; } + public MultipartFile getFile() { + return file; + } + + public void setFile(final MultipartFile file) { + this.file = file; + } + } diff --git a/spring-mvc-xml/src/main/resources/messages.properties b/spring-mvc-xml/src/main/resources/messages.properties new file mode 100644 index 0000000000..2a187731ae --- /dev/null +++ b/spring-mvc-xml/src/main/resources/messages.properties @@ -0,0 +1,2 @@ +NotEmpty.person.name = Name is required! +NotEmpty.person.password = Password is required! \ No newline at end of file diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index a7aa252c08..f3297fdbf6 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -11,7 +11,9 @@ > + + @@ -19,4 +21,15 @@ + + + + + + + + + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index f6cf0bf460..9ef02efbae 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -1,88 +1,109 @@ <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> - - -Form Example - Register a Person - - -

Welcome, Enter The Person Details

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Name
Password
Sex - Male:
- Female: -
Job - -
Country - -
Book - - - - -
Fruit - -
Receive newsletter
Hobbies - Bird watching: - Astronomy: - Snowboarding: -
Favourite languages - -
Notes
-
- - + + + Form Example - Register a Person + + + + + + +

Welcome, Enter The Person Details

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name
Password
Sex + Male:
+ Female: +
Job + +
Country + +
Book + + + + +
Fruit + +
Receive newsletter
Hobbies + Bird watching: + Astronomy: + Snowboarding: +
Favourite languages + +
Notes
Select a file to upload
+ +
+ + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp index 81f7fd3411..857738c545 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp @@ -56,6 +56,10 @@ Notes : ${person.notes} + + File : + ${person.file.originalFilename} + \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 5275efdf24..4e38b2fae6 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -28,6 +28,9 @@ mvc org.springframework.web.servlet.DispatcherServlet 1 + + C:/Users/ivan/Desktop/tmp + mvc From 90853626d29edd3516e9d83bcd9726de36494054 Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 19 Feb 2016 17:25:54 +0100 Subject: [PATCH 03/10] Add form tag example --- .../spring/controller/PersonController.java | 39 +++++---- .../java/org/baeldung/spring/form/Person.java | 22 ++++- .../spring/validator/PersonValidator.java | 22 +++++ .../main/webapp/WEB-INF/view/personForm.jsp | 15 +++- .../main/webapp/WEB-INF/view/personResume.jsp | 65 --------------- .../main/webapp/WEB-INF/view/personView.jsp | 80 +++++++++++++++++++ 6 files changed, 156 insertions(+), 87 deletions(-) create mode 100644 spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java delete mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp create mode 100644 spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java index d5b68c7516..0d89a29b9d 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java @@ -8,6 +8,8 @@ import java.util.Map; import javax.validation.Valid; import org.baeldung.spring.form.Person; +import org.baeldung.spring.validator.PersonValidator; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; @@ -20,6 +22,9 @@ import org.springframework.web.servlet.ModelAndView; @Controller public class PersonController { + @Autowired + PersonValidator validator; + @RequestMapping(value = "/person", method = RequestMethod.GET) public ModelAndView showForm(final Model model) { @@ -31,6 +36,8 @@ public class PersonController { public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, final ModelMap modelMap, final Model model) { + validator.validate(person, result); + if (result.hasErrors()) { initData(model); @@ -39,28 +46,28 @@ public class PersonController { modelMap.addAttribute("person", person); - return "personResume"; + return "personView"; } private void initData(final Model model) { - final List favouriteLanguage = new ArrayList(); - favouriteLanguage.add("Java"); - favouriteLanguage.add("C++"); - favouriteLanguage.add("Perl"); - model.addAttribute("favouriteLanguage", favouriteLanguage); + final List favouriteLanguageItem = new ArrayList(); + favouriteLanguageItem.add("Java"); + favouriteLanguageItem.add("C++"); + favouriteLanguageItem.add("Perl"); + model.addAttribute("favouriteLanguageItem", favouriteLanguageItem); - final List job = new ArrayList(); - job.add("Full time"); - job.add("Part time"); - model.addAttribute("job", job); + final List jobItem = new ArrayList(); + jobItem.add("Full time"); + jobItem.add("Part time"); + model.addAttribute("jobItem", jobItem); - final Map country = new LinkedHashMap(); - country.put("US", "United Stated"); - country.put("IT", "Italy"); - country.put("UK", "United Kingdom"); - country.put("FR", "Grance"); - model.addAttribute("country", country); + final Map countryItems = new LinkedHashMap(); + countryItems.put("US", "United Stated"); + countryItems.put("IT", "Italy"); + countryItems.put("UK", "United Kingdom"); + countryItems.put("FR", "Grance"); + model.addAttribute("countryItems", countryItems); final List fruit = new ArrayList(); fruit.add("Banana"); diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java index 3db8cc4377..bf313b4d7d 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java @@ -9,8 +9,9 @@ public class Person { private long id; - @NotEmpty private String name; + private String email; + private String dateOfBirth; @NotEmpty private String password; @@ -18,7 +19,7 @@ public class Person { private String country; private String book; private String job; - boolean receiveNewsletter; + private boolean receiveNewsletter; private String[] hobbies; private List favouriteLanguage; private List fruit; @@ -45,6 +46,22 @@ public class Person { this.name = name; } + public String getEmail() { + return email; + } + + public void setEmail(final String email) { + this.email = email; + } + + public String getDateOfBirth() { + return dateOfBirth; + } + + public void setDateOfBirth(final String dateOfBirth) { + this.dateOfBirth = dateOfBirth; + } + public String getPassword() { return password; } @@ -132,5 +149,4 @@ public class Person { public void setFile(final MultipartFile file) { this.file = file; } - } diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java b/spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java new file mode 100644 index 0000000000..068a9fee7f --- /dev/null +++ b/spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java @@ -0,0 +1,22 @@ +package org.baeldung.spring.validator; + +import org.baeldung.spring.form.Person; +import org.springframework.stereotype.Component; +import org.springframework.validation.Errors; +import org.springframework.validation.ValidationUtils; +import org.springframework.validation.Validator; + +@Component +public class PersonValidator implements Validator { + + @Override + public boolean supports(final Class calzz) { + return Person.class.isAssignableFrom(calzz); + } + + @Override + public void validate(final Object obj, final Errors errors) { + + ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required.name"); + } +} \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 9ef02efbae..9b0a78899c 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -28,6 +28,15 @@ + + E-mail + + + + + Date of birth + + Password @@ -43,13 +52,13 @@ Job - + Country - + @@ -82,7 +91,7 @@ Favourite languages - + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp deleted file mode 100644 index 857738c545..0000000000 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personResume.jsp +++ /dev/null @@ -1,65 +0,0 @@ -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> - - - -Spring MVC Form Handling - - - -

Submitted Person Information

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Id :${person.id}
Name :${person.name}
Password :${person.password}
Sex :${person.sex}
Job :${person.job}
Country :${person.country}
Fruit :[]
Book :${person.book}
Receive Newsletter :${person.receiveNewsletter}
Hobbies :[]
Favourite Languages :[]
Notes :${person.notes}
File :${person.file.originalFilename}
- - \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp new file mode 100644 index 0000000000..8893314d20 --- /dev/null +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personView.jsp @@ -0,0 +1,80 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + Spring MVC Form Handling + + + +

Submitted Person Information

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Id :${person.id}
Name :${person.name}
Date of birth :${person.dateOfBirth}
Password :${person.password}
Sex :${person.sex}
Job :${person.job}
Country :${person.country}
Fruit :[]
Book :${person.book}
Receive Newsletter :${person.receiveNewsletter}
Hobbies :[]
Favourite Languages :[]
Notes :${person.notes}
+ +

Submitted File

+ + + + + + + + + + + + +
OriginalFileName :${person.file.originalFilename}
Type :${person.file.contentType}
+ + \ No newline at end of file From 09d9cf26b41156fe807f22d2acce231d6b087650 Mon Sep 17 00:00:00 2001 From: Ivan Date: Wed, 2 Mar 2016 14:57:33 +0100 Subject: [PATCH 04/10] Add form tag example --- spring-mvc-xml/src/main/resources/messages.properties | 2 +- .../src/main/webapp/WEB-INF/view/personForm.jsp | 10 ++++++++-- spring-mvc-xml/src/main/webapp/WEB-INF/web.xml | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/spring-mvc-xml/src/main/resources/messages.properties b/spring-mvc-xml/src/main/resources/messages.properties index 2a187731ae..2a3cccf76c 100644 --- a/spring-mvc-xml/src/main/resources/messages.properties +++ b/spring-mvc-xml/src/main/resources/messages.properties @@ -1,2 +1,2 @@ -NotEmpty.person.name = Name is required! +required.name = Name is required! NotEmpty.person.password = Password is required! \ No newline at end of file diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 9b0a78899c..79bda7c17d 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -12,6 +12,12 @@ color: #ff0000; } + .errorbox { + + background-color: #ffEEEE; + border: 2px solid #ff0000; + } + @@ -20,13 +26,13 @@ - + - + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index 4e38b2fae6..a39c4fdd3a 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -29,7 +29,7 @@ org.springframework.web.servlet.DispatcherServlet1 - C:/Users/ivan/Desktop/tmp + /tmp From bae9b89af3b565eb2ba6efb3d4934379b764a2d2 Mon Sep 17 00:00:00 2001 From: David Morley Date: Thu, 3 Mar 2016 05:30:23 -0600 Subject: [PATCH 05/10] Clean up README for Jackson/Gson article --- gson-jackson-performance/README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/gson-jackson-performance/README.md b/gson-jackson-performance/README.md index 9bbe679dfc..32002eb38e 100644 --- a/gson-jackson-performance/README.md +++ b/gson-jackson-performance/README.md @@ -1,5 +1,3 @@ -## Performance of GSON and Jackson +## Performance of Gson and Jackson -## STandalone java programs to measure the performance of both JSON API's - -## based on file size , iterations. \ No newline at end of file +## Standalone java programs to measure the performance of both JSON APIs based on file size and object graph complexity. From 6c5fa86613a5293fb92af74e74f96cd19408a6e2 Mon Sep 17 00:00:00 2001 From: David Morley Date: Thu, 3 Mar 2016 05:30:43 -0600 Subject: [PATCH 06/10] Clean up README for Jackson/Gson article --- gson-jackson-performance/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gson-jackson-performance/README.md b/gson-jackson-performance/README.md index 32002eb38e..5b08f6cdec 100644 --- a/gson-jackson-performance/README.md +++ b/gson-jackson-performance/README.md @@ -1,3 +1,3 @@ ## Performance of Gson and Jackson -## Standalone java programs to measure the performance of both JSON APIs based on file size and object graph complexity. +Standalone java programs to measure the performance of both JSON APIs based on file size and object graph complexity. From cf4cce751be208e2920c0e89b07da7dfdcf24fc2 Mon Sep 17 00:00:00 2001 From: David Morley Date: Thu, 3 Mar 2016 06:20:44 -0600 Subject: [PATCH 07/10] Clean up Spring MVC Form Tag Library example --- spring-mvc-xml/pom.xml | 2 +- .../java/{org => com}/baeldung/spring/ClientWebConfig.java | 2 +- .../{org => com}/baeldung/spring/ClientWebConfigJava.java | 2 +- .../baeldung/spring/controller/EmployeeController.java | 4 ++-- .../baeldung/spring/controller/PersonController.java | 6 +++--- .../java/{org => com}/baeldung/spring/form/Employee.java | 2 +- .../main/java/{org => com}/baeldung/spring/form/Person.java | 2 +- .../baeldung/spring/validator/PersonValidator.java | 4 ++-- spring-mvc-xml/src/main/resources/webMvcConfig.xml | 2 +- spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp | 2 +- spring-mvc-xml/src/main/webapp/WEB-INF/web.xml | 2 +- 11 files changed, 15 insertions(+), 15 deletions(-) rename spring-mvc-xml/src/main/java/{org => com}/baeldung/spring/ClientWebConfig.java (93%) rename spring-mvc-xml/src/main/java/{org => com}/baeldung/spring/ClientWebConfigJava.java (97%) rename spring-mvc-xml/src/main/java/{org => com}/baeldung/spring/controller/EmployeeController.java (93%) rename spring-mvc-xml/src/main/java/{org => com}/baeldung/spring/controller/PersonController.java (94%) rename spring-mvc-xml/src/main/java/{org => com}/baeldung/spring/form/Employee.java (95%) rename spring-mvc-xml/src/main/java/{org => com}/baeldung/spring/form/Person.java (98%) rename spring-mvc-xml/src/main/java/{org => com}/baeldung/spring/validator/PersonValidator.java (84%) diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index 896d744e99..75efdca423 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -1,6 +1,6 @@ 4.0.0 - org.baeldung + com.baeldung 0.1-SNAPSHOT spring-mvc-xml diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfig.java similarity index 93% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfig.java index c4d819caa5..b5238b04d5 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfig.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfig.java @@ -1,4 +1,4 @@ -package org.baeldung.spring; +package com.baeldung.spring; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java similarity index 97% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java index d2b57da818..06b2c0e461 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/ClientWebConfigJava.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/ClientWebConfigJava.java @@ -1,4 +1,4 @@ -package org.baeldung.spring; +package com.baeldung.spring; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.ViewResolver; diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java similarity index 93% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java index 1dbe230adc..baffe7b503 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/EmployeeController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/EmployeeController.java @@ -1,8 +1,8 @@ -package org.baeldung.spring.controller; +package com.baeldung.spring.controller; import javax.validation.Valid; -import org.baeldung.spring.form.Employee; +import com.baeldung.spring.form.Employee; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java similarity index 94% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java index 0d89a29b9d..39dabf86ed 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/controller/PersonController.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/PersonController.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.controller; +package com.baeldung.spring.controller; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -7,8 +7,8 @@ import java.util.Map; import javax.validation.Valid; -import org.baeldung.spring.form.Person; -import org.baeldung.spring.validator.PersonValidator; +import com.baeldung.spring.form.Person; +import com.baeldung.spring.validator.PersonValidator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java similarity index 95% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java index 70132b9665..f41496212f 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Employee.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Employee.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.form; +package com.baeldung.spring.form; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java similarity index 98% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java index bf313b4d7d..88e4f9ff4c 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/form/Person.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/form/Person.java @@ -1,4 +1,4 @@ -package org.baeldung.spring.form; +package com.baeldung.spring.form; import java.util.List; diff --git a/spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java similarity index 84% rename from spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java rename to spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java index 068a9fee7f..3a271f6545 100644 --- a/spring-mvc-xml/src/main/java/org/baeldung/spring/validator/PersonValidator.java +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/validator/PersonValidator.java @@ -1,6 +1,6 @@ -package org.baeldung.spring.validator; +package com.baeldung.spring.validator; -import org.baeldung.spring.form.Person; +import com.baeldung.spring.form.Person; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index f3297fdbf6..e9e2f1c536 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -12,7 +12,7 @@ - + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp index 79bda7c17d..a26fb6c3ea 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/view/personForm.jsp @@ -24,7 +24,7 @@

Welcome, Enter The Person Details

- + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml index a39c4fdd3a..aeaa9d394e 100644 --- a/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml +++ b/spring-mvc-xml/src/main/webapp/WEB-INF/web.xml @@ -16,7 +16,7 @@ contextConfigLocation - org.baeldung.spring + com.baeldung.spring From 516729bf57f7981bb5fb3af9736fdba7e2a1beb6 Mon Sep 17 00:00:00 2001 From: Thai Nguyen Date: Fri, 4 Mar 2016 10:32:39 +0700 Subject: [PATCH 08/10] jsonpath with new use cases --- json-path/pom.xml | 51 +++++++++++ json-path/src/main/resources/intro_api.json | 57 ++++++++++++ .../src/main/resources/intro_service.json | 61 +++++++++++++ .../jsonpath/introduction/OperationTest.java | 71 +++++++++++++++ .../jsonpath/introduction/ServiceTest.java | 91 +++++++++++++++++++ 5 files changed, 331 insertions(+) create mode 100644 json-path/pom.xml create mode 100644 json-path/src/main/resources/intro_api.json create mode 100644 json-path/src/main/resources/intro_service.json create mode 100644 json-path/src/test/java/org/baeldung/jsonpath/introduction/OperationTest.java create mode 100644 json-path/src/test/java/org/baeldung/jsonpath/introduction/ServiceTest.java diff --git a/json-path/pom.xml b/json-path/pom.xml new file mode 100644 index 0000000000..9a86431ffe --- /dev/null +++ b/json-path/pom.xml @@ -0,0 +1,51 @@ + + 4.0.0 + org.baeldung + json-path + 0.0.1-SNAPSHOT + json-path + + + + + com.jayway.jsonpath + json-path + ${json-path.version} + + + + + junit + junit + ${junit.version} + test + + + + + org.slf4j + slf4j-api + ${slf4j.version} + runtime + + + ch.qos.logback + logback-classic + ${logback.version} + runtime + + + + + + 2.1.0 + + + 4.12 + + + 1.7.14 + 1.1.3 + + \ No newline at end of file diff --git a/json-path/src/main/resources/intro_api.json b/json-path/src/main/resources/intro_api.json new file mode 100644 index 0000000000..8a252f2971 --- /dev/null +++ b/json-path/src/main/resources/intro_api.json @@ -0,0 +1,57 @@ +{ + "tool": + { + "jsonpath": + { + "creator": + { + "name": "Jayway Inc.", + "location": + [ + "Malmo", + "Stockholm", + "Copenhagen", + "San Francisco", + "Karlskrona", + "Halmstad", + "Helsingborg" + ] + }, + + "current release": "2.1" + } + }, + + "book": + [ + { + "title": "Beginning JSON", + "author": "Ben Smith", + "price": 49.99 + }, + + { + "title": "JSON at Work", + "author": "Tom Marrs", + "price": 29.99 + }, + + { + "title": "Learn JSON in a DAY", + "author": "Acodemy", + "price": 8.99 + }, + + { + "title": "JSON: Questions and Answers", + "author": "George Duckett", + "price": 6.00 + } + ], + + "price range": + { + "cheap": 10.00, + "medium": 20.00 + } +} \ No newline at end of file diff --git a/json-path/src/main/resources/intro_service.json b/json-path/src/main/resources/intro_service.json new file mode 100644 index 0000000000..448492463a --- /dev/null +++ b/json-path/src/main/resources/intro_service.json @@ -0,0 +1,61 @@ +[ + { + "id": 1, + "title": "Casino Royale", + "director": "Martin Campbell", + "starring": + [ + "Daniel Craig", + "Eva Green" + ], + + "desc": "Twenty-first James Bond movie", + "release date": 1163466000000, + "box office": 594275385 + }, + + { + "id": 2, + "title": "Quantum of Solace", + "director": "Marc Forster", + "starring": + [ + "Daniel Craig", + "Olga Kurylenko" + ], + + "desc": "Twenty-second James Bond movie", + "release date": 1225242000000, + "box office": 591692078 + }, + + { + "id": 3, + "title": "Skyfall", + "director": "Sam Mendes", + "starring": + [ + "Daniel Craig", + "Naomie Harris" + ], + + "desc": "Twenty-third James Bond movie", + "release date": 1350954000000, + "box office": 1110526981 + }, + + { + "id": 4, + "title": "Spectre", + "director": "Sam Mendes", + "starring": + [ + "Daniel Craig", + "Lea Seydoux" + ], + + "desc": "Twenty-fourth James Bond movie", + "release date": 1445821200000, + "box office": 879376275 + } +] \ No newline at end of file diff --git a/json-path/src/test/java/org/baeldung/jsonpath/introduction/OperationTest.java b/json-path/src/test/java/org/baeldung/jsonpath/introduction/OperationTest.java new file mode 100644 index 0000000000..9347a7f754 --- /dev/null +++ b/json-path/src/test/java/org/baeldung/jsonpath/introduction/OperationTest.java @@ -0,0 +1,71 @@ +package org.baeldung.jsonpath.introduction; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.not; + +import org.junit.Test; + +import java.io.InputStream; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import com.jayway.jsonpath.Criteria; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.Filter; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Predicate; + +public class OperationTest { + InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_api.json"); + String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); + + @Test + public void givenJsonPathWithoutPredicates_whenReading_thenCorrect() { + String jsonpathCreatorNamePath = "$['tool']['jsonpath']['creator']['name']"; + String jsonpathCreatorLocationPath = "$['tool']['jsonpath']['creator']['location'][*]"; + + DocumentContext jsonContext = JsonPath.parse(jsonDataSourceString); + String jsonpathCreatorName = jsonContext.read(jsonpathCreatorNamePath); + List jsonpathCreatorLocation = jsonContext.read(jsonpathCreatorLocationPath); + + assertEquals("Jayway Inc.", jsonpathCreatorName); + assertThat(jsonpathCreatorLocation.toString(), containsString("Malmo")); + assertThat(jsonpathCreatorLocation.toString(), containsString("San Francisco")); + assertThat(jsonpathCreatorLocation.toString(), containsString("Helsingborg")); + } + + @Test + public void givenJsonPathWithFilterPredicate_whenReading_thenCorrect() { + Filter expensiveFilter = Filter.filter(Criteria.where("price").gt(20.00)); + List> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?]", expensiveFilter); + predicateUsageAssertionHelper(expensive); + } + + @Test + public void givenJsonPathWithCustomizedPredicate_whenReading_thenCorrect() { + Predicate expensivePredicate = new Predicate() { + public boolean apply(PredicateContext context) { + String value = context.item(Map.class).get("price").toString(); + return Float.valueOf(value) > 20.00; + } + }; + List> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?]", expensivePredicate); + predicateUsageAssertionHelper(expensive); + } + + @Test + public void givenJsonPathWithInlinePredicate_whenReading_thenCorrect() { + List> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?(@['price'] > $['price range']['medium'])]"); + predicateUsageAssertionHelper(expensive); + } + + private void predicateUsageAssertionHelper(List predicate) { + assertThat(predicate.toString(), containsString("Beginning JSON")); + assertThat(predicate.toString(), containsString("JSON at Work")); + assertThat(predicate.toString(), not(containsString("Learn JSON in a DAY"))); + assertThat(predicate.toString(), not(containsString("JSON: Questions and Answers"))); + } +} diff --git a/json-path/src/test/java/org/baeldung/jsonpath/introduction/ServiceTest.java b/json-path/src/test/java/org/baeldung/jsonpath/introduction/ServiceTest.java new file mode 100644 index 0000000000..66cc73af52 --- /dev/null +++ b/json-path/src/test/java/org/baeldung/jsonpath/introduction/ServiceTest.java @@ -0,0 +1,91 @@ +package org.baeldung.jsonpath.introduction; + +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.hamcrest.CoreMatchers.containsString; + +import org.junit.Test; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Option; + +public class ServiceTest { + InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_service.json"); + String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next(); + + @Test + public void givenId_whenRequestingRecordData_thenSucceed() { + Object dataObject = JsonPath.parse(jsonString).read("$[?(@.id == 2)]"); + String dataString = dataObject.toString(); + + assertThat(dataString, containsString("2")); + assertThat(dataString, containsString("Quantum of Solace")); + assertThat(dataString, containsString("Twenty-second James Bond movie")); + } + + @Test + public void givenStarring_whenRequestingMovieTitle_thenSucceed() { + List> dataList = JsonPath.parse(jsonString).read("$[?('Eva Green' in @['starring'])]"); + String title = (String) dataList.get(0).get("title"); + + assertEquals("Casino Royale", title); + } + + @Test + public void givenCompleteStructure_whenCalculatingTotalRevenue_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + int length = context.read("$.length()"); + long revenue = 0; + for (int i = 0; i < length; i++) { + revenue += context.read("$[" + i + "]['box office']", Long.class); + } + + assertEquals(594275385L + 591692078L + 1110526981L + 879376275L, revenue); + } + + @Test + public void givenStructure_whenRequestingHighestRevenueMovieTitle_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + List revenueList = context.read("$[*]['box office']"); + Integer[] revenueArray = revenueList.toArray(new Integer[0]); + Arrays.sort(revenueArray); + + int highestRevenue = revenueArray[revenueArray.length - 1]; + Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build(); + List pathList = JsonPath.using(pathConfiguration).parse(jsonString).read("$[?(@['box office'] == " + highestRevenue + ")]"); + + Map dataRecord = context.read(pathList.get(0)); + String title = dataRecord.get("title"); + + assertEquals("Skyfall", title); + } + + @Test + public void givenDirector_whenRequestingLatestMovieTitle_thenSucceed() { + DocumentContext context = JsonPath.parse(jsonString); + List> dataList = context.read("$[?(@.director == 'Sam Mendes')]"); + + List dateList = new ArrayList<>(); + for (Map item : dataList) { + Object date = item.get("release date"); + dateList.add(date); + } + Long[] dateArray = dateList.toArray(new Long[0]); + Arrays.sort(dateArray); + + long latestTime = dateArray[dateArray.length - 1]; + List> finalDataList = context.read("$[?(@['director'] == 'Sam Mendes' && @['release date'] == " + latestTime + ")]"); + String title = (String) finalDataList.get(0).get("title"); + + assertEquals("Spectre", title); + } +} \ No newline at end of file From 9dcec31b0012724e87763deca48a4b6373100db9 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sat, 5 Mar 2016 12:53:30 +0200 Subject: [PATCH 09/10] minor metrics fix --- .../web/metric/ActuatorMetricService.java | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java index fec03d1ddb..89e3a22d45 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java @@ -17,13 +17,13 @@ public class ActuatorMetricService implements IActuatorMetricService { @Autowired private MetricReaderPublicMetrics publicMetrics; - private final List> statusMetric; + private final List> statusMetricsByMinute; private final List statusList; private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public ActuatorMetricService() { super(); - statusMetric = new ArrayList>(); + statusMetricsByMinute = new ArrayList>(); statusList = new ArrayList(); } @@ -31,7 +31,7 @@ public class ActuatorMetricService implements IActuatorMetricService { public Object[][] getGraphData() { final Date current = new Date(); final int colCount = statusList.size() + 1; - final int rowCount = statusMetric.size() + 1; + final int rowCount = statusMetricsByMinute.size() + 1; final Object[][] result = new Object[rowCount][colCount]; result[0][0] = "Time"; int j = 1; @@ -41,19 +41,23 @@ public class ActuatorMetricService implements IActuatorMetricService { j++; } - List temp; - List last = new ArrayList(); for (int i = 1; i < rowCount; i++) { - temp = statusMetric.get(i - 1); result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); - for (j = 1; j <= temp.size(); j++) { - result[i][j] = temp.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0); + } + + List minuteOfStatuses; + List last = new ArrayList(); + + for (int i = 1; i < rowCount; i++) { + minuteOfStatuses = statusMetricsByMinute.get(i - 1); + for (j = 1; j <= minuteOfStatuses.size(); j++) { + result[i][j] = minuteOfStatuses.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0); } while (j < colCount) { result[i][j] = 0; j++; } - last = temp; + last = minuteOfStatuses; } return result; } @@ -62,16 +66,16 @@ public class ActuatorMetricService implements IActuatorMetricService { @Scheduled(fixedDelay = 60000) private void exportMetrics() { - final ArrayList statusCount = initializeCounterList(statusList.size()); + final ArrayList lastMinuteStatuses = initializeStatuses(statusList.size()); for (final Metric counterMetric : publicMetrics.metrics()) { - updateStatusCount(counterMetric, statusCount); + updateMetrics(counterMetric, lastMinuteStatuses); } - statusMetric.add(statusCount); + statusMetricsByMinute.add(lastMinuteStatuses); } - private ArrayList initializeCounterList(final int size) { + private ArrayList initializeStatuses(final int size) { final ArrayList counterList = new ArrayList(); for (int i = 0; i < size; i++) { counterList.add(0); @@ -79,22 +83,26 @@ public class ActuatorMetricService implements IActuatorMetricService { return counterList; } - private void updateStatusCount(final Metric counterMetric, final ArrayList statusCount) { + private void updateMetrics(final Metric counterMetric, final ArrayList statusCount) { String status = ""; int index = -1; int oldCount = 0; if (counterMetric.getName().contains("counter.status.")) { status = counterMetric.getName().substring(15, 18); // example 404, 200 - if (!statusList.contains(status)) { - statusList.add(status); - statusCount.add(0); - } + appendStatusIfNotExist(status, statusCount); index = statusList.indexOf(status); oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index); statusCount.set(index, counterMetric.getValue().intValue() + oldCount); } } + private void appendStatusIfNotExist(final String status, final ArrayList statusCount) { + if (!statusList.contains(status)) { + statusList.add(status); + statusCount.add(0); + } + } + // } \ No newline at end of file From 716d2072c024454e0f89e162a9458631deb53651 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sat, 5 Mar 2016 13:23:15 +0200 Subject: [PATCH 10/10] minor metric fix --- .../metric/CustomActuatorMetricService.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java index 7df7e199b4..852a9fae99 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java @@ -21,14 +21,14 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService @Autowired private CounterService counter; - private final List> statusMetric; + private final List> statusMetricsByMinute; private final List statusList; private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public CustomActuatorMetricService() { super(); repo = new InMemoryMetricRepository(); - statusMetric = new ArrayList>(); + statusMetricsByMinute = new ArrayList>(); statusList = new ArrayList(); } @@ -46,7 +46,7 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService public Object[][] getGraphData() { final Date current = new Date(); final int colCount = statusList.size() + 1; - final int rowCount = statusMetric.size() + 1; + final int rowCount = statusMetricsByMinute.size() + 1; final Object[][] result = new Object[rowCount][colCount]; result[0][0] = "Time"; @@ -56,12 +56,15 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService j++; } - List temp; for (int i = 1; i < rowCount; i++) { - temp = statusMetric.get(i - 1); result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); - for (j = 1; j <= temp.size(); j++) { - result[i][j] = temp.get(j - 1); + } + + List minuteOfStatuses; + for (int i = 1; i < rowCount; i++) { + minuteOfStatuses = statusMetricsByMinute.get(i - 1); + for (j = 1; j <= minuteOfStatuses.size(); j++) { + result[i][j] = minuteOfStatuses.get(j - 1); } while (j < colCount) { result[i][j] = 0; @@ -87,6 +90,6 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService } } - statusMetric.add(statusCount); + statusMetricsByMinute.add(statusCount); } } \ No newline at end of file
Name
E-mail