Announcement Announcement Module
Collapse
No announcement yet.
Simplify verbose XML Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simplify verbose XML

    I am using Spring's composite view framework instead of sitemesh or tiles and I am quite happy with it. However, I've found that the XML definitions for the views can get too verbose. Are there plans to introduce additional attributes for the property tag to reduce that effect?

    Here is a snippet of my views.xml

    <bean id="baseView" class="org.springframework.web.servlet.view.JstlVi ew">
    <property name="url"><value>/WEB-INF/jsp/templates/defaultTemplate.jsp</value></property>
    <property name="requestContextAttribute"><value>rc</value></property>
    <property name="attributesMap['scripts']">
    <list>
    <value>/scripts/common.js</value>
    </list>
    </property>
    <property name="attributesMap['styles']">
    <list>
    <value>/styles/bass.css</value>
    </list>
    </property>
    <property name="attributesMap['logo']"><value>images.logo</value></property>
    <property name="attributesMap['logoText']"><value>images.logoText</value></property>
    <property name="attributesMap['loading']"><value>images.loading</value></property>
    <property name="attributesMap['loadingText']"><value>images.loadingText</value></property>
    <property name="attributesMap['menuItems']"><ref local="mainMenu"/></property>
    <property name="attributesMap['help']"><value>templates.help</value></property>
    <property name="attributesMap['top']"><value>templates.top</value></property>
    <property name="attributesMap['banner']"><value>templates.banner</value></property>
    <property name="attributesMap['tabsItems']"><null/></property>
    <property name="attributesMap['subTabsItems']"><null/></property>
    <property name="attributesMap['content']"><value>pages.blank</value></property>
    </bean>

    <bean id="demoView" parent="baseView">
    <property name="attributesMap['selectedMenuId']"><value>menuItem1</value></property>
    <property name="attributesMap['tabsItems']"><ref local="menuItem1DetailItems"/></property>
    </bean>

    <bean id="mainMenu" class="org.springframework.beans.factory.config.Li stFactoryBean">
    <property name="sourceList">
    <list>
    <bean class="spring.koed.struts.MenuItem">
    <property name="id"><value>menuItem1</value></property>
    <property name="name"><value>Spring it up</value></property>
    <property name="toolTip"><value>Hello, see spring in action</value></property>
    <property name="accessKey"><value>A</value></property>
    <property name="link"><value>springitup.html</value></property>
    <!--
    <property name="roles">
    <set>
    <value>INTELLIGENT_USERS</value>
    <value>COOL_USERS</value>
    </set>
    </property>
    -->
    </bean>
    <bean class="spring.koed.struts.MenuItem">
    <property name="id"><value>menuItem2</value></property>
    <property name="name"><value>Struts</value></property>
    <property name="toolTip"><value>I'm just hanging</value></property>
    <property name="accessKey"><value>B</value></property>
    <property name="link"><value>strutshanging.html</value></property>
    <!--
    <property name="roles">
    <set>
    <value>AVERAGE_USERS</value>
    <value>BORING_USERS</value>
    </set>
    </property>
    -->
    </bean>
    </list>
    </property>
    </bean>

  • #2
    What do you have in mind? XML simplification is a major theme in our planning for Spring 1.2. (Retaining backward compability, of course.)

    One already agreed enhancement would allow this:
    Code:
    <property name="url"><value>/WEB-INF/jsp/templates/defaultTemplate.jsp</value></property>
    to be rewritten like this:
    Code:
    <property name="url" value="/WEB-INF/jsp/templates/defaultTemplate.jsp" />
    Thus using an attribute instead of a nested tage.

    We are also considering other options, such as importing additional namespaces to allow higher-level tags to be used where appropriate. E.g.:
    Code:
    <j2ee&#58;remoteSlsb id="myService" jndiName="myServiceEJB" businessInterface="mycompany.Service" />
    Thoughts welcome!

    Rgds
    Rod

    Comment


    • #3
      I'm all for simplification, but there's also a danger of causing confusion when you have all these different ways of expressing basically the same thing.

      Is the Spring team thinking about improving the current beans.dtd or will there be an additional bean definition format? (see also: http://forum.springframework.org/showthread.php?t=11584)

      Erwin
      Last edited by robyn; May 14th, 2006, 06:19 PM.

      Comment


      • #4
        I'm all for simplification, but there's also a danger of causing confusion when you have all these different ways of expressing basically the same thing.
        I agree there's a danger, but using attributes and elements as alternatives is a pretty common practice in XML configuration. 95% of the time the attribute is appropriate; for the 5% where you have strange characters you can't have in attributes, you can use the element.

        Is the Spring team thinking about improving the current beans.dtd or will there be an additional bean definition format?
        We're thinking about evolving the existing DTD. I think that would be less confusing. However, we are seriously considering switching to XML schema, which adds a number of useful capabilities, including the ability to use additional namespaces. We will of course support existing documents referencing the DTD, though.

        The problem with this kind of format IMO is that it is not amenable to tool support. I believe tool support (code assist) is very important. I haven't typed the whole of any Spring XML tag for months...

        Rgds
        Rod
        Last edited by robyn; May 14th, 2006, 06:20 PM.

        Comment


        • #5
          I should add that we're also exploring additional configuration options like Java and Groovy.

          Comment


          • #6
            Where can I find some documentation or an example of what j2eeguru is doing in the first post of this thread? I have searched in vain for more information on how to do this.

            Thanks

            Comment


            • #7
              Originally posted by troe
              Where can I find some documentation or an example of what j2eeguru is doing in the first post of this thread? I have searched in vain for more information on how to do this.

              Thanks
              What are you trying to do? Perhaps, I can help you out.
              I was using Tiles but, decided to use a pure spring approach instead of relying on the struts jar which includes the Tiles framework (which, of course, should be a commons project). So, instead of using the typical spring properties file for my views as illustrated in all the pure spring mvc samples, I decided to use an XML file instead.


              <property name="attributesMap">
              <map>
              <entry key="someKey1"><value>test1</value></entry>
              <entry key="someKey2"><value>test2</value></entry>
              </map>
              </property>

              is equivalent to:

              <property name="attributesMap['somekey1']"><value>test1</value></property>
              <property name="attributesMap['somekey2']"><value>test2</value></property>

              Comment


              • #8
                J2EEGuru,

                I'm trying to find an alternative to tiles and am just getting started with sitemesh. I'd prefer to keep the technologies/frameworks/libraries I use to a minimum, so your approach sounded worthwhile. I understand how you are declaring the attributes, but how are you using them in the pages?

                My goal is to have one main layout page. I'd like to be able to insert page fragments into placeholders in the main page as the need warrants. I don't want to have to replicate the structure of my pages over and over, so sitemesh looked like a good possibility. Do you know of any tutorial, example, or documentation describing the method you are using?

                Thanks,

                Comment


                • #9
                  <bean id="demoView" parent="baseView">
                  <property name="attributesMap['selectedMenuId']"><value>menuItem1</value></property>
                  <property name="attributesMap['tabsItems']"><ref local="menuItem1DetailItems"/></property>
                  </bean>

                  If you refer to my original post, you'll see that I defined a main template which subsequent views extend. Hence, there is no need to specify all the attributes over again; you simply to override the attributes you want. This is similar to Tiles. Note that the attribute values can be other beans and not string values. For instance, menuItem1DetailItems is a list of MenuItem objects.
                  I am not using the sitemesh approach because it would require my controllers to be aware of the composition of my views and I don't want that. For instance, before forwarding to a view, a controller would have to specify the different menu items (MenuItem objects), tabs items (MenuItem objects) and detail tab items (MenuItem objects) that are associated with my view by putting them in a Map object whose values would exposed as request attributes in my JSP. Now, once all that information is there, then using SiteMesh would be a clear winner as I would only need to write the content JSP and therefore would not need to define composed views siimilar to the Tiles method.


                  I don't know of an example that shows how to do what I'm doing. You might want to take a look at the country sample which uses a property file for the views.

                  Comment


                  • #10
                    The attributes mechanism is pretty powerful. I agree it can be a good alternative to specialized templating tools. That's what I initially intended it for, and used it for in various projects...

                    Comment


                    • #11
                      very interesting

                      This setup looks very much like what I want to accomplish with some of my projects, but I'm still a little confused about whats going on.

                      how is your defaultTemplate.jsp putting these properties to use, and what are you using for a view resolver and how is that being setup.

                      Rod is this covered somewhere in your book? I haven't seen any examples that make use of the view properties like this.

                      Comment


                      • #12
                        I am using the XML view resolver. In my dispatcher servlet configuration file, I have the view resolver defined as follows:

                        <bean id="viewResolver" class="org.springframework.web.servlet.view.XmlVie wResolver">
                        <property name="location"><value>/WEB-INF/resources/views.xml</value></property>
                        </bean>


                        Here is a snippet of my template jsp.

                        <%@ include file="includes.jsp"%>

                        <%-- The attributes below were set in views.xml and their values are keys whose
                        corresponding values are found in a property file (messages.properties).
                        Therefore, we need to lookup the value for the keys by using either the
                        JSTL fmt tag or the spring message tag. I opted for the JSTL fmt tag.
                        For instance, in views.xml above, the value of the attribute 'logo' is
                        images.logo which is a key whose value is set in messages.properties as
                        follows:
                        images.logo=/images/logo.gif

                        So, by using the fmt tag, I can get the value /images/logo.gif which will
                        be prepended with the context path when I use <c:url .../> tag to help
                        render the image.
                        --%>

                        <fmt:message key="${logo}" var="logo"/>
                        <fmt:message key="${loading}" var="loading"/>
                        <fmt:message key="${help}" var="help"/>
                        <fmt:message key="${top}" var="top"/>
                        <fmt:message key="${banner}" var="banner"/>
                        <fmt:message key="${content}" var="content"/>


                        <html>

                        <head>
                        <title><fmt:message key="${pageTitle}"/></title>
                        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


                        <c:forEach var="css" items="${styles}">
                        <link rel="stylesheet" type="text/css" media="all" href="<c:url value="${css}"/>" />
                        </c:forEach>

                        <c:forEach var="js" items="${scripts}">
                        <script type="text/javascript" language="JavaScript" src="<c:url value="${js}"/>"></script>
                        </c:forEach>
                        </head>

                        ...
                        ...
                        ...

                        </html>

                        Comment

                        Working...
                        X