HttpSessionRequiredException programmatically exposes name of expected attribute

Issue: SPR-14206
This commit is contained in:
Juergen Hoeller
2016-04-26 22:56:25 +02:00
parent 9798912557
commit e5d52a96a7
2 changed files with 25 additions and 2 deletions

View File

@@ -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;
}
}

View File

@@ -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);
}