Announcement Announcement Module
Collapse
No announcement yet.
Logical URL mappings Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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).

    Comment


    • #3
      Could you provide an example?

      Comment


      • #4
        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.

        Comment

        Working...
        X