public class Mixer2XhtmlViewResolver
extends org.springframework.web.servlet.view.UrlBasedViewResolver
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.2.RELEASE (or higher)</version>
</dependency>
Spring MVC configuration Sample
<bean id="mixer2Engine" class="org.mixer2.Mixer2Engine" scope="singleton" />
<bean class="org.mixer2.spring.webmvc.Mixer2XhtmlViewResolver">
<property name="order" value="1" />
<property name="prefix" value="classpath:m2mockup/m2template/" />
<!-- Also, you can use file system path like "file:/var/foo/" -->
<property name="suffix" value=".html" />
<property name="basePackage" value="com.example.yourproject.web.view" />
<property name="mixer2Engine" ref="mixer2Engine" />
<!-- Default value is true that means this resolver returns null -->
<!-- if template file not found and continue to resolve view name -->
<!-- by another subsequent view resolver. -->
<!-- Set "false" if you expect FileNotFoundException for unknown view name -->
<property name="returnNullIfTemplateFileNotFound" value="true" />
<!-- If this resolver can not find the view class, -->
<!-- returns html template "as is" with defaultView. -->
<!-- But if use this property, raise ClassNotFoundException . -->
<!-- Default value is false. -->
<property name="raiseErrorIfViewClassNotFound" value="true" />
</bean>
Using this ViewResolver, Your controller method can returns template
html file path and name without file extension.
See sample below.
@Controller
public class HelloWorldController {
@Autowired
protected helloMessageService helloMessageService;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String showHello(Model model) {
String helloMessage = helloMessageService.getMessage();
model.addAttribute("helloMessage", helloMessage);
return "foo/bar/helloWorld";
}
}
In this case, "foo/bar/helloWorld" will be attached to "com.example.yourproject.web.view.foo.bar.HelloWorldView" class .
AbstractMixer2XhtmlView which is the base class of HelloWorldView loads the file "m2mockup/m2template/foo/bar/helloWorld.html" as a html template and convert it to the instance of org.mixer2.jaxb.xhtml.Html . You can change the html object in HelloWorldView class.
The template file is "prefix" + viewName + "suffix" . See "prefix" and "suffix" property of Mixer2XhtmlViewResolver bean configuration sample above.
If there were no "com.example.yourproject.web.view.foo.bar.HelloWorldView" class in classpath,default view will be created automatically that load the template file. You can not change the the html object. That means the view resolver returns html on template file as is.
FORWARD_URL_PREFIX, REDIRECT_URL_PREFIX
Constructor and Description |
---|
Mixer2XhtmlViewResolver() |
Modifier and Type | Method and Description |
---|---|
protected org.springframework.web.servlet.view.AbstractUrlBasedView |
buildView(String viewName) |
protected boolean |
canHandle(String viewName,
Locale locale) |
protected AbstractMixer2XhtmlView |
createDefaultView() |
protected String |
createUrl(String viewName) |
boolean |
isRaiseErrorIfViewClassNotFound() |
boolean |
isReturnNullIfTemplateFileNotFound() |
void |
setBasePackage(String basePackage) |
void |
setClassNameSuffix(String classNameSuffix) |
void |
setDocType(String docType) |
void |
setMixer2Engine(Mixer2Engine mixer2Engine) |
void |
setRaiseErrorIfViewClassNotFound(boolean raiseErrorIfViewClassNotFound) |
void |
setReturnNullIfTemplateFileNotFound(boolean returnNullIfTemplateFileNotFound) |
protected String |
toViewClassNamePrefix(String viewName) |
createView, getAttributesMap, getCacheKey, getContentType, getExposeContextBeansAsAttributes, getExposedContextBeanNames, getExposePathVariables, getOrder, getPrefix, getRedirectHosts, getRequestContextAttribute, getSuffix, getViewClass, getViewNames, initApplicationContext, isRedirectContextRelative, isRedirectHttp10Compatible, loadView, requiredViewClass, setAttributes, setAttributesMap, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setOrder, setPrefix, setRedirectContextRelative, setRedirectHosts, setRedirectHttp10Compatible, setRequestContextAttribute, setSuffix, setViewClass, setViewNames
clearCache, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheLimit, setCacheUnresolved
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
public void setMixer2Engine(Mixer2Engine mixer2Engine)
public void setBasePackage(String basePackage)
public void setClassNameSuffix(String classNameSuffix)
public void setDocType(String docType)
public boolean isReturnNullIfTemplateFileNotFound()
public void setReturnNullIfTemplateFileNotFound(boolean returnNullIfTemplateFileNotFound)
public boolean isRaiseErrorIfViewClassNotFound()
public void setRaiseErrorIfViewClassNotFound(boolean raiseErrorIfViewClassNotFound)
protected boolean canHandle(String viewName, Locale locale)
canHandle
in class org.springframework.web.servlet.view.UrlBasedViewResolver
protected org.springframework.web.servlet.view.AbstractUrlBasedView buildView(String viewName) throws Exception
buildView
in class org.springframework.web.servlet.view.UrlBasedViewResolver
Exception
protected AbstractMixer2XhtmlView createDefaultView()
Copyright © 2021 Project Mixer2. All rights reserved.