From 648b70cd82e3bd65ac497158240954490235c8c1 Mon Sep 17 00:00:00 2001 From: deepanshuPC Date: Fri, 20 Feb 2015 19:20:15 +0530 Subject: [PATCH 1/4] demonstrate use of regex for validating bean in Validation tab --- .../samples/mvc/validation/JavaBean.java | 12 ++++++++++++ .../samples/mvc/validation/ValidationController.java | 2 -- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/samples/mvc/validation/JavaBean.java b/src/main/java/org/springframework/samples/mvc/validation/JavaBean.java index 91bdcab81..a31b56c29 100644 --- a/src/main/java/org/springframework/samples/mvc/validation/JavaBean.java +++ b/src/main/java/org/springframework/samples/mvc/validation/JavaBean.java @@ -5,6 +5,7 @@ import javax.validation.constraints.Future; import javax.validation.constraints.Max; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat.ISO; @@ -19,6 +20,17 @@ public class JavaBean { @Future @DateTimeFormat(iso=ISO.DATE) private Date date; + + @Pattern(regexp="/([A-Za-z-0-9])+/g") + private String alphaNumericHyphen; + + public String getAlphaNumericHyphen() { + return alphaNumericHyphen; + } + + public void setAlphaNumericHyphen(String alphaNumericHyphen) { + this.alphaNumericHyphen = alphaNumericHyphen; + } public Integer getNumber() { return number; diff --git a/src/main/java/org/springframework/samples/mvc/validation/ValidationController.java b/src/main/java/org/springframework/samples/mvc/validation/ValidationController.java index 6384e3065..34f6b6d4d 100644 --- a/src/main/java/org/springframework/samples/mvc/validation/ValidationController.java +++ b/src/main/java/org/springframework/samples/mvc/validation/ValidationController.java @@ -11,7 +11,6 @@ public class ValidationController { // enforcement of constraints on the JavaBean arg require a JSR-303 provider on the classpath - @RequestMapping("/validate") public @ResponseBody String validate(@Valid JavaBean bean, BindingResult result) { if (result.hasErrors()) { @@ -20,5 +19,4 @@ public class ValidationController { return "No errors"; } } - } From 25d47efc418e57def45d23463be9fcc81929409c Mon Sep 17 00:00:00 2001 From: deepanshuPC Date: Fri, 20 Feb 2015 20:09:39 +0530 Subject: [PATCH 2/4] jsp change for regex validation --- src/main/webapp/WEB-INF/views/home.jsp | 1268 ++++++++++++++---------- 1 file changed, 743 insertions(+), 525 deletions(-) diff --git a/src/main/webapp/WEB-INF/views/home.jsp b/src/main/webapp/WEB-INF/views/home.jsp index 8e0fe94f2..51c4b78f0 100644 --- a/src/main/webapp/WEB-INF/views/home.jsp +++ b/src/main/webapp/WEB-INF/views/home.jsp @@ -1,650 +1,868 @@ -<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> -<%@ page session="false" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +<%@ page session="false"%> - spring-mvc-showcase - " rel="stylesheet" type="text/css" /> - " rel="stylesheet" type="text/css"/> - " rel="stylesheet" type="text/css"/> - " rel="stylesheet" type="text/css"/> - - - - + + -

">spring-mvc-showcase

-

Recommended: Using a Web Developer tool such a Firebug to inspect the client/server interaction

-
- -
-

Simple

-

- See the org.springframework.samples.mvc.simple package for the @Controller code -

+

+ spring-mvc-showcase +

+

Recommended: Using a Web Developer tool such a Firebug to inspect the client/server interaction

+ - -
-

Request Data

-

- See the org.springframework.samples.mvc.data package for the @Controller code -

- -
-

Standard Resolvable Web Arguments

+ +
+

Simple

+

+ See the + org.springframework.samples.mvc.simple + package for the @Controller code +

+
+ -
-

Custom Resolvable Web Arguments

+
+

Request Data

+

+ See the + org.springframework.samples.mvc.data + package for the @Controller code +

+
+

Standard Resolvable Web Arguments

+ +
+
+

Custom Resolvable Web Arguments

+ +
-
-
-

Response Writing

-

- See the org.springframework.samples.mvc.response package for the @Controller code -

- -
-
-

Http Message Converters

-

- See the org.springframework.samples.mvc.messageconverters package for the @Controller code -

-
-

StringHttpMessageConverter

+
+

Response Writing

+

+ See the + org.springframework.samples.mvc.response + package for the @Controller code +

-

FormHttpMessageConverter

+
+
+

Http Message Converters

+

+ See the + org.springframework.samples.mvc.messageconverters + package for the @Controller code +

+
+

StringHttpMessageConverter

+ +

FormHttpMessageConverter

+ +

Jaxb2RootElementHttpMessageConverter

+ +

MappingJacksonHttpMessageConverter

+ +

AtomFeedHttpMessageConverter

+ +

RssChannelHttpMessageConverter

+ +
+
+
+

View Rendering

+

+ See the + org.springframework.samples.mvc.views + package for the @Controller code +

+ -

Jaxb2RootElementHttpMessageConverter

+ -

MappingJacksonHttpMessageConverter

+
+
+

Type Conversion

+

+ See the + org.springframework.samples.mvc.convert + package for the @Controller code +

-

AtomFeedHttpMessageConverter

+
+

JavaBean Property Binding

+ +
+
+
+

Validation

+

+ See the + org.springframework.samples.mvc.validation + package for the @Controller code +

-

RssChannelHttpMessageConverter

+
+
+

Exception Handling

+

+ See the + org.springframework.samples.mvc.exceptions + package for the @Controller code +

+
-
-
-

View Rendering

-

- See the org.springframework.samples.mvc.views package for the @Controller code -

- - - - -
-
-

Type Conversion

-

- See the org.springframework.samples.mvc.convert package for the @Controller code -

- -
-

JavaBean Property Binding

+
+

Redirecting

+

+ See the + org.springframework.samples.mvc.redirect + package for the @Controller code +

+
+
-
-

Validation

-

- See the org.springframework.samples.mvc.validation package for the @Controller code -

- -
-
-

Exception Handling

-

- See the org.springframework.samples.mvc.exceptions package for the @Controller code -

- -
-
-

Redirecting

-

- See the org.springframework.samples.mvc.redirect package for the @Controller code -

- -
-
-

Async Requests

-

- Note: Links may take 2-3 seconds to complete. -

-

- See the org.springframework.samples.mvc.async package for the @Controller code. -

- -
-
- - - - - - - - + + + + + + + + \ No newline at end of file From 4d16ded50d717f3bb2f9ed1838495e4ff13db10e Mon Sep 17 00:00:00 2001 From: deepanshuPC Date: Fri, 20 Feb 2015 20:14:45 +0530 Subject: [PATCH 3/4] Jsp change for regex based validation --- src/main/webapp/WEB-INF/views/home.jsp | 1268 ++++++++++-------------- 1 file changed, 525 insertions(+), 743 deletions(-) diff --git a/src/main/webapp/WEB-INF/views/home.jsp b/src/main/webapp/WEB-INF/views/home.jsp index 51c4b78f0..b054423fb 100644 --- a/src/main/webapp/WEB-INF/views/home.jsp +++ b/src/main/webapp/WEB-INF/views/home.jsp @@ -1,868 +1,650 @@ -<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> -<%@ page session="false"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ page session="false" %> -spring-mvc-showcase - - - - - - - + + -

- spring-mvc-showcase -

-

Recommended: Using a Web Developer tool such a Firebug to inspect the client/server interaction

-
+

">spring-mvc-showcase

+

Recommended: Using a Web Developer tool such a Firebug to inspect the client/server interaction

+
+ +
+

Simple

+

+ See the org.springframework.samples.mvc.simple package for the @Controller code +

+
+ +
+

Request Data

+

+ See the org.springframework.samples.mvc.data package for the @Controller code +

+ -
-

Simple

-

- See the - org.springframework.samples.mvc.simple - package for the @Controller code -

- -
-
-

Request Mapping

-

- See the - org.springframework.samples.mvc.mapping - package for the @Controller code -

+
  • + ">Path variable +
  • +
  • + ">Matrix variable +
  • +
  • + ">Matrix variables (multiple) +
  • +
  • + ">Header +
  • +
  • +
    " method="post"> + +
    +
  • +
  • +
    " method="post"> + +
    +
  • + + -
    -

    Request Data

    -

    - See the - org.springframework.samples.mvc.data - package for the @Controller code -

    +
    +

    Custom Resolvable Web Arguments

    -
    -

    Standard Resolvable Web Arguments

    - -
    -
    -

    Custom Resolvable Web Arguments

    - -
    -
    -

    Response Writing

    -

    - See the - org.springframework.samples.mvc.response - package for the @Controller code -

    +
    +
    +

    Response Writing

    +

    + See the org.springframework.samples.mvc.response package for the @Controller code +

    + +
    +
    +

    Http Message Converters

    +

    + See the org.springframework.samples.mvc.messageconverters package for the @Controller code +

    + -
    -

    Http Message Converters

    -

    - See the - org.springframework.samples.mvc.messageconverters - package for the @Controller code -

    -
    -

    StringHttpMessageConverter

    - -

    FormHttpMessageConverter

    - -

    Jaxb2RootElementHttpMessageConverter

    - -

    MappingJacksonHttpMessageConverter

    - -

    AtomFeedHttpMessageConverter

    - -

    RssChannelHttpMessageConverter

    - -
    -
    -
    -

    View Rendering

    -

    - See the - org.springframework.samples.mvc.views - package for the @Controller code -

    +

    FormHttpMessageConverter

    - +

    Jaxb2RootElementHttpMessageConverter

    - -
    -
    -

    Type Conversion

    -

    - See the - org.springframework.samples.mvc.convert - package for the @Controller code -

    +

    MappingJacksonHttpMessageConverter

    -
    -

    JavaBean Property Binding

    - -
    -
    -
    -

    Validation

    -

    - See the - org.springframework.samples.mvc.validation - package for the @Controller code -

    +

    AtomFeedHttpMessageConverter

    -
    -
    -

    Exception Handling

    -

    - See the - org.springframework.samples.mvc.exceptions - package for the @Controller code -

    - -
    -
    -

    Redirecting

    -

    - See the - org.springframework.samples.mvc.redirect - package for the @Controller code -

    +

    RssChannelHttpMessageConverter

    +
    -
    -

    Async Requests

    -

    - Note: Links may take 2-3 seconds to complete. -

    -

    - See the - org.springframework.samples.mvc.async - package for the @Controller code. -

    +
    +
    +

    View Rendering

    +

    + See the org.springframework.samples.mvc.views package for the @Controller code +

    + + + + +
    + - - - - - - - - + + + + + + + + return false; + }); + + $("#readRss").submit(function() { + var form = $(this); + var button = form.children(":first"); + $.ajax({ type: "POST", url: form.attr("action"), data: ' My RSS feed', contentType: "application/rss+xml", dataType: "text", success: function(text) { MvcUtil.showSuccessResponse(text, button); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, button); }}); + return false; + }); + + $("#writeRss").click(function() { + var link = $(this); + $.ajax({ url: link.attr("href"), + beforeSend: function(req) { + req.setRequestHeader("Accept", "application/rss+xml"); + }, + success: function(feed) { + MvcUtil.showSuccessResponse(MvcUtil.xmlencode(feed), link); + }, + error: function(xhr) { + MvcUtil.showErrorResponse(xhr.responseText, link); + } + }); + return false; + }); + + $("#byHeader").click(function(){ + var link = $(this); + $.ajax({ url: this.href, dataType: "text", beforeSend: function(req) { req.setRequestHeader("FooHeader", "foo"); }, success: function(form) { MvcUtil.showSuccessResponse(form, link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }}); + return false; + }); + + // Include CSRF token as header in JQuery AJAX requests + // See http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#csrf-include-csrf-token-ajax + var token = $("meta[name='_csrf']").attr("content"); + var header = $("meta[name='_csrf_header']").attr("content"); + $(document).ajaxSend(function(e, xhr, options) { + xhr.setRequestHeader(header, token); + }); + +}); + \ No newline at end of file From d100a69d13a6b4b07d1e4c82d8431a76a3eb8cd6 Mon Sep 17 00:00:00 2001 From: deepanshuPC Date: Fri, 20 Feb 2015 20:15:57 +0530 Subject: [PATCH 4/4] change reverted --- .../samples/mvc/validation/ValidationController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/springframework/samples/mvc/validation/ValidationController.java b/src/main/java/org/springframework/samples/mvc/validation/ValidationController.java index 34f6b6d4d..6384e3065 100644 --- a/src/main/java/org/springframework/samples/mvc/validation/ValidationController.java +++ b/src/main/java/org/springframework/samples/mvc/validation/ValidationController.java @@ -11,6 +11,7 @@ public class ValidationController { // enforcement of constraints on the JavaBean arg require a JSR-303 provider on the classpath + @RequestMapping("/validate") public @ResponseBody String validate(@Valid JavaBean bean, BindingResult result) { if (result.hasErrors()) { @@ -19,4 +20,5 @@ public class ValidationController { return "No errors"; } } + }