Announcement Announcement Module
Collapse
No announcement yet.
Best way to expose constant URLs to the View Layer? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Best way to expose constant URLs to the View Layer?

    I've searched the forums for this and I see that the question has been asked, but I can't seem to find a definitive answer.

    Can someone tell me the best way to expose the URLs (the constants, like "/shop/addItem.do") to the view layer? I'm using JSP/JSTL and I do not want to hardcode the names in the JSP pages. I am using a SimpleUrlHandlerMapping object to map my URLs to their respective Controllers. Also, I would want to be able to utilize this solution in the Controllers themselves.

    Is there an elegant solution to this problem? or should I keep passing the names down to the Controllers? Thanks!

    --JDS

  • #2
    I hate to reply to my own message, but I just thought about it.

    Probably the best way to do it is to define the URLs in an external JSP page using JSTL and then do <%@include file="constants.jsp" %> on every page.

    How bout that? Although I still have to figure out how to include them in the Controllers also.

    --JDS

    Comment


    • #3
      messages.properties

      Hi! I don't know if this is a particularly good way of doing it, but I have been using the following method (until a better one turns up):

      In my messages.properties file I have basic application URIs:

      # Examples
      url.app.login=/auth/login.html
      url.app.myDetails=/contact/my-details.html
      url.app.changePassword=/auth/password.html

      I'm using Velocity for templating, so I have a macro defined like so:

      #macro( appUrl $code )${req.contextPath}$req.getMessage("url.app.${code }")#end

      This allows my templates to use links like this:

      <a href="#appUrl('login')">Please Login</a>

      which becomes:

      <a href="/my-context/auth/login.html">Please Login</a>

      I too would like a better way of doing this, particularly for complex URLs with lots of parameters.

      Comment


      • #4
        Originally posted by theMesser
        I hate to reply to my own message, but I just thought about it.

        Probably the best way to do it is to define the URLs in an external JSP page using JSTL and then do <%@include file="constants.jsp" %> on every page.

        How bout that? Although I still have to figure out how to include them in the Controllers also.

        --JDS
        this is a reasonable way, as they are pretty localized at least. As for the controllers, all paths should normally be in the xml handler mapping anyways. When using one of the view resolution strategies that resolves a view name to a real path by just manipulating it a bit, you probably want to always get the view in in the control via an injected property, similar to getSuccessview and getFormView, so the paths are not hard-coded in the controller.

        Comment


        • #5
          When using one of the view resolution strategies that resolves a view name to a real path by just manipulating it a bit, you probably want to always get the view in in the control via an injected property, similar to getSuccessview and getFormView, so the paths are not hard-coded in the controller.
          Colin,
          Could you explain if this could be done when using a MultiActionViewController? Say there are 10 views that are displayed from this controller, I'm a bit lost as to how the views can be resolved without hardcoding the names in.?

          Comment


          • #6
            &quot;global forwards&quot; for Spring MVC

            I've attempted to solve this in a limited way. Please see http://forum.springframework.org/viewtopic.php?t=4066 for details.

            Comment


            • #7
              Any Update ?

              Has anyone resolved this or found/created a solution ?
              Struts handled this nicely, transparently resolving the protocol and all. I would like to move to SpringMVC with tiles, without losing this great struts feature. If anyone has some ideas or solutions please post !

              Example:
              Struts config:
              <action parameter=".site.home" path="/home"
              type="org.apache.struts.actions.ForwardAction" validate="false">
              <set-property property="secure" value="false"/>
              </action>

              Tiles defs:
              <definition name=".site.home" path="/jsp/common/layouts/homeLayout.jsp">
              <put name="title" value="Title"/>
              <put name="header" value=".site.header"/>
              <put name="macroNav" value=".site.macroNav"/>
              <put name="microNav" value=".site.microNav"/>
              <put name="content" value="/jsp/home.jsp"/>
              </definition>

              Jsp Page:
              <html:link page="/home.do">Go Home<html:link>

              Comment

              Working...
              X