[
https://issues.apache.org/struts/browse/STR-2143?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul Benedict resolved STR-2143.
--------------------------------
Resolution: Duplicate
Fix Version/s: 1.4.0
Assignee: Paul Benedict (was: Struts Developers)
> make errors and messages work even if redirect
> ----------------------------------------------
>
> Key: STR-2143
> URL: https://issues.apache.org/struts/browse/STR-2143
> Project: Struts 1
> Issue Type: Improvement
> Components: Core
> Affects Versions: 1.1 Final
> Environment: Operating System: All
> Platform: All
> Reporter: Jean-Baptiste Nizet
> Assignee: Paul Benedict
> Priority: Minor
> Fix For: 1.4.0
>
>
> One of my frustrations with Struts is that errors and messages saved during
> the
> request, either manually or automatically, are lost if my action returns a
> forward with redirect="true".
> I think this could be relatively easily fixed with the following mechanism:
> When Struts handles a redirect forward, it checks if there are errors saved in
> the request. If there are errors, It generates a unique ID, saves the errors
> in
> the session using the unique ID as a session key, and appends the following
> request parameter to the forward path:
> org.apache.struts.action.ERROR=<unique_id>
> The same goes for messages saved in the request.
> When Struts receives a request (after processPreprocess, for example), it
> checks
> for the parameter org.apache.struts.action.ERROR and gets its value (the
> unique
> id). Then it gets the errors saved in the session using the parameter value
> as a
> key, saves the errors retrieved from the session in the request, and removed
> the
> errors from the session.
> This way, errors and messages are not lost anymore when redirecting (even with
> chained redirects). It's totally transparent to the developer; it works even
> if
> the user uses multiple browser windows and submits multiple concurrent
> requests;
> and it shouldn't clutter the session (unless the browser doesn't redirect for
> any reason)
> Here's some code doing this:
> protected String processErrorsAndMessagesBeforeRedirect(HttpServletRequest
> request,
> String uri) {
> Object errors = request.getAttribute(Globals.ERROR_KEY);
> if (errors != null) {
> String errorParameterValue = generateUniqueId(request, "errors");
> uri = appendParameter(uri, Globals.ERROR_KEY,
> errorParameterValue);
> request.getSession(true).setAttribute(errorParameterValue,
> errors);
> }
> Object messages = request.getAttribute(Globals.MESSAGE_KEY);
> if (messages != null) {
> String messageParameterValue = generateUniqueId(request,
> "messages");
> uri = appendParameter(uri, Globals.MESSAGE_KEY,
> messageParameterValue);
> request.getSession(true).setAttribute(messageParameterValue,
> messages);
> }
> return uri;
> }
>
> protected void processErrorsAndMessagesAfterRedirect(HttpServletRequest
> request) {
> HttpSession session = request.getSession(true);
>
> String errorParameterValue = request.getParameter(Globals.ERROR_KEY);
> if (errorParameterValue != null) {
> Object errors = session.getAttribute(errorParameterValue);
> if (errors != null) {
> request.setAttribute(Globals.ERROR_KEY, errors);
> session.removeAttribute(errorParameterValue);
> }
> }
>
> String messageParameterValue =
> request.getParameter(Globals.MESSAGE_KEY);
> if (messageParameterValue != null) {
> Object messages = session.getAttribute(messageParameterValue);
> if (messages != null) {
> request.setAttribute(Globals.MESSAGE_KEY, messages);
> session.removeAttribute(messageParameterValue);
> }
> }
> }
>
> private String generateUniqueId(HttpServletRequest request, String type) {
> return type + "_" +
> TokenProcessor.getInstance().generateToken(request);
> }
> private String appendParameter(String uri,
> String parameterName,
> String parameterValue) {
> StringBuffer buffer = new StringBuffer(uri);
> if (uri.indexOf('?') >= 0) {
> buffer.append('&');
> }
> else {
> buffer.append('?');
> }
> buffer.append(parameterName);
> buffer.append('=');
> buffer.append(parameterValue);
> return buffer.toString();
> }
> What's your opinion about this mechanism?
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.