From e5d52a96a7c3a80100b0239f4fd7165e1bbb7a06 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 26 Apr 2016 22:56:25 +0200 Subject: [PATCH] HttpSessionRequiredException programmatically exposes name of expected attribute Issue: SPR-14206 --- .../web/HttpSessionRequiredException.java | 25 ++++++++++++++++++- .../web/method/annotation/ModelFactory.java | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/HttpSessionRequiredException.java b/spring-web/src/main/java/org/springframework/web/HttpSessionRequiredException.java index 448bd4eddf..d17f8d39f9 100644 --- a/spring-web/src/main/java/org/springframework/web/HttpSessionRequiredException.java +++ b/spring-web/src/main/java/org/springframework/web/HttpSessionRequiredException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,9 @@ import javax.servlet.ServletException; @SuppressWarnings("serial") public class HttpSessionRequiredException extends ServletException { + private String expectedAttribute; + + /** * Create a new HttpSessionRequiredException. * @param msg the detail message @@ -35,4 +38,24 @@ public class HttpSessionRequiredException extends ServletException { super(msg); } + /** + * Create a new HttpSessionRequiredException. + * @param msg the detail message + * @param expectedAttribute the name of the expected session attribute + * @since 4.3 + */ + public HttpSessionRequiredException(String msg, String expectedAttribute) { + super(msg); + this.expectedAttribute = expectedAttribute; + } + + + /** + * Return the name of the expected session attribute, if any. + * @since 4.3 + */ + public String getExpectedAttribute() { + return this.expectedAttribute; + } + } diff --git a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java index d3b1b5e5df..be4472d3e9 100644 --- a/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java +++ b/spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java @@ -112,7 +112,7 @@ public final class ModelFactory { if (!container.containsAttribute(name)) { Object value = this.sessionAttributesHandler.retrieveAttribute(request, name); if (value == null) { - throw new HttpSessionRequiredException("Expected session attribute '" + name + "'"); + throw new HttpSessionRequiredException("Expected session attribute '" + name + "'", name); } container.addAttribute(name, value); }