org.thymeleaf.spring3.expression.Mvc Maven / Gradle / Ivy
/*
* =============================================================================
*
* Copyright (c) 2011-2016, The THYMELEAF team (http://www.thymeleaf.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.thymeleaf.spring3.expression;
import org.thymeleaf.exceptions.ConfigurationException;
import org.thymeleaf.util.ClassLoaderUtils;
/**
*
* Expression object in charge of the creation of URLs using the controller-based mechanism in Spring MVC 4.1.
*
*
* This mimics the s:mvcUrl behaviour explained at
* http://docs.spring.io/spring/docs/4.1.0.RELEASE/spring-framework-reference/html/mvc.html#mvc-links-to-controllers-from-views
* using the same method (function) names in the Spring JSP tag library.
*
*
* @author Daniel Fernández
*
* @since 2.1.4
*
*/
public class Mvc {
private static final MvcUriComponentsBuilderDelegate mvcUriComponentsBuilderDelegate;
// No reason to have a Spring4.1-valid implementation in the thymeleaf-spring3 package
private static final String NON_SPRING41_MVC_URI_COMPONENTS_BUILDER_DELEGATE_CLASS_NAME = Mvc.class.getName() + "$NonSpring41MvcUriComponentsBuilderDelegate";
static {
final String delegateClassName = NON_SPRING41_MVC_URI_COMPONENTS_BUILDER_DELEGATE_CLASS_NAME;
try {
final Class> implClass = ClassLoaderUtils.loadClass(delegateClassName);
mvcUriComponentsBuilderDelegate = (MvcUriComponentsBuilderDelegate) implClass.newInstance();
} catch (final Exception e) {
throw new ExceptionInInitializerError(
new ConfigurationException(
"Thymeleaf could not initialize a delegate of class \"" + delegateClassName + "\" for taking " +
"care of the " + SpringStandardExpressionObjectFactory.MVC_EXPRESSION_OBJECT_NAME + " expression utility object", e));
}
}
public MethodArgumentBuilderWrapper url(final String mappingName) {
return mvcUriComponentsBuilderDelegate.fromMappingName(mappingName);
}
static interface MvcUriComponentsBuilderDelegate {
public MethodArgumentBuilderWrapper fromMappingName(String mappingName);
}
static class NonSpring41MvcUriComponentsBuilderDelegate implements MvcUriComponentsBuilderDelegate {
NonSpring41MvcUriComponentsBuilderDelegate() {
super();
}
public MethodArgumentBuilderWrapper fromMappingName(final String mappingName) {
throw new UnsupportedOperationException(
"MVC URI component building is only supported in Spring versions 4.1 or newer");
}
}
public static interface MethodArgumentBuilderWrapper {
public MethodArgumentBuilderWrapper arg(int index, Object value);
public String build();
public String buildAndExpand(Object... uriVariables);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy