
Originally Posted by
grom358
Could you provide an example?
Here is a quick and dirty controller that converts a logical page and points it to the freemarker template in the /WEB-INF/ftl directory. For example, it takes /myapp/blah.htm and creates a ModelAndView with the view name "blah". Then, my view Resolver appends the ".ftl" extension and the path /WEB-INF/
Controller:
Code:
public class HtmlPageController extends AbstractController {
protected ModelAndView handleRequestInternal(
HttpServletRequest request, HttpServletResponse response ) throws Exception
{
return new ModelAndView(
FileStringUtils.removePathAndExtension( request.getRequestURI() ) );
}
}
Bean factory:
Code:
<!-- CONTROLLERS -->
<bean name="htmlPageController" class="org.auroramvc.web.HtmlPageController" />
<!-- URL MAPPING -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="*.htm">htmlPageController</prop>
</props>
</property>
</bean>
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache"><value>true</value></property>
<property name="requestContextAttribute"><value>rc</value></property>
<property name="prefix"><value></value></property>
<property name="suffix"><value>.ftl</value></property>
</bean>
In your example, you can just make a map of logical names (the map key) to concrete pages (the map entries), pass it into the controller inside bean factory and have the controller create the ModelAndView object with the entry in the map. Something like this:
Code:
public class HtmlPageController extends AbstractController {
private Map myMap;
protected ModelAndView handleRequestInternal(
HttpServletRequest request, HttpServletResponse response ) throws Exception
{
return new ModelAndView( myMap.get( request.getRequestURI() ) );
}
public void setMyMap( Map myMap ) {
this.myMap = myMap;
}
}
It's not that much different than what I have above. It's really simple. Of course, you should do something sensible if it can't find the page in the map and so on (like create a specific exception and have an exception resolver trap it to show a page not found view or something like that).
To handle redirects, just toss a redirect=true query string parameter or something and wrap the view with a RedirectView object if that parameter exists.
Also, keep in mind that dynamic views like this isn't the best approach. It's not really that useful unless you have pages changing throughout the life of the project. If all the pages are known at design time, you should be adding the pages to the urlMappings bean directly instead and just pointing them to controllers.
Anyway, hope it helps.