/**
* Copyright 2005-2013 hdiv.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hdiv.web.servlet.support;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.hdiv.dataComposer.IDataComposer;
import org.hdiv.urlProcessor.FormUrlProcessor;
import org.hdiv.util.Constants;
import org.hdiv.util.HDIVUtil;
import org.springframework.web.servlet.support.RequestDataValueProcessor;
/**
* {@link RequestDataValueProcessor} implementation for Thymeleaf.
*
* Method invocation order is different in Thymeleaf compared to Spring MVC.
* <ol>
* <li>processAction(..) method for form action</li>
* <li>getExtraHiddenFields(..) method for extra hidden field</li>
* <li>processFormFieldValue(..) method for each form field.</li>
* </ol>
*/
public class ThymeleafHdivRequestDataValueProcessor extends HdivRequestDataValueProcessor {
@Override
public String processAction(HttpServletRequest request, String action, String method) {
IDataComposer dataComposer = (IDataComposer) request.getAttribute(HDIVUtil.DATACOMPOSER_REQUEST_KEY);
if (dataComposer != null && dataComposer.isRequestStarted()) {
// End with the last form
dataComposer.endRequest();
}
// Start with the new form
return super.processAction(request, action, method);
}
public Map<String, String> getExtraHiddenFields(HttpServletRequest request) {
IDataComposer dataComposer = (IDataComposer) request.getAttribute(HDIVUtil.DATACOMPOSER_REQUEST_KEY);
Map<String, String> extraFields = new HashMap<String, String>();
if (this.innerRequestDataValueProcessor != null) {
Map<String, String> innerExtras = this.innerRequestDataValueProcessor.getExtraHiddenFields(request);
if (innerExtras != null) {
extraFields.putAll(innerExtras);
}
}
if (dataComposer == null || dataComposer.isRequestStarted() == false) {
return extraFields;
}
// Use the state id generated by the form action processing
String formStateId = (String) request.getAttribute(FormUrlProcessor.FORM_STATE_ID);
if (formStateId != null && formStateId.length() > 0) {
String hdivStateParam = (String) request.getSession().getAttribute(Constants.HDIV_PARAMETER);
extraFields.put(hdivStateParam, formStateId);
}
return extraFields;
}
}