Results 1 to 4 of 4

Thread: Logical URL mappings

  1. #1
    Join Date
    Sep 2004
    Posts
    9

    Default Logical URL mappings

    Is there a way to have logical URL names? So that in the view layer (jsp with jstl tags) I can go <c:url value="AddItemToCart"> and the name gets translate to "/shop/addItemToCart.do". Likewise can this be done in controllers for redirects?

    Is there a mechanism to redirect back to the user's previous page?

    Thanks,
    Cameron.

  2. #2

    Default

    You can add logical mappings to urlMappings bean in the servlet bean factory. The mappings point to a controller that can point to the real page. You can do this statically, by defining what the success view should be, or you can do it dynamically by doing a lookup in a Map or by basing it on the URI (or whatever).

  3. #3
    Join Date
    Sep 2004
    Posts
    9

    Default

    Could you provide an example?

  4. #4

    Default

    Quote 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 &#123;
    
    	protected ModelAndView handleRequestInternal&#40;
    		HttpServletRequest request, HttpServletResponse response &#41; throws Exception
    	&#123;
    		return new ModelAndView&#40;
    			FileStringUtils.removePathAndExtension&#40; request.getRequestURI&#40;&#41; &#41; &#41;;
    	&#125;
    &#125;
    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 &#123;
    
    	private Map myMap;
    
    	protected ModelAndView handleRequestInternal&#40;
    		HttpServletRequest request, HttpServletResponse response &#41; throws Exception
    	&#123;
    		return new ModelAndView&#40; myMap.get&#40; request.getRequestURI&#40;&#41; &#41; &#41;;
    	&#125;
    
    	public void setMyMap&#40; Map myMap &#41; &#123;
    		this.myMap = myMap;
    	&#125;
    
    &#125;
    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.

Similar Threads

  1. JAXRPC - Custom Type Mappings
    By derek in forum Remoting
    Replies: 2
    Last Post: Sep 23rd, 2005, 03:57 AM
  2. Replies: 3
    Last Post: Sep 9th, 2005, 06:17 AM
  3. Replies: 0
    Last Post: Aug 29th, 2005, 03:23 PM
  4. Replies: 1
    Last Post: Feb 20th, 2005, 03:39 AM
  5. Replies: 1
    Last Post: Jan 19th, 2005, 11:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •