Announcement Announcement Module
Collapse
No announcement yet.
Step-by-step snag Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Step-by-step snag

    I'm working through the step by step and hit a snag. I'm sure it it a really simple issue I'm missing, but I've been looking at it a long time.

    I am simply trying to print out some data provided in the servlet.xml. I'm getting the date and the string from the Map returned in the controller, but I'm not getting the data from the two lists (one created in the controller, one created from the servlet.xml).

    I'm getting the date and name, so the data object is making it to the jsp. I've logged the values in the lists from the controller, and they are there, so the servlet.xml is ok. I'm getting the following result on the page. Where am I going wrong? Am I screwing up the <c:forEach>?
    Matt's Test Spring Application

    Hi. Fri Oct 06 11:35:24 PDT 2006

    Name manbearpig

    Stuff
    ${data.stuff}

    Users
    -
    springapp-servlet.xml
    Code:
        <bean id="springappController" class="web.SpringappController">
            <property name="userManager">
                <ref bean="userManagerBean"/>
            </property>
        </bean>
    
        <bean id="userManagerBean" class="business.UserManager">
            <property name="users">
                <list>
                    <ref bean="user1"/>
                    <ref bean="user2"/>
                    <ref bean="user3"/>
                </list>
            </property>
        </bean>
    
        <bean id="user1" class="business.User">
            <property name="username">
                <value>Matt</value>
            </property>
            <property name="age">
                <value>27</value>
            </property>
        </bean>
    
        <bean id="user2" class="business.User">
            <property name="username">
                <value>Jonathan</value>
            </property>
            <property name="age">
                <value>24</value>
            </property>
        </bean>
    
        <bean id="user3" class="business.User">
            <property name="username">
                <value>Zac</value>
            </property>
            <property name="age">
                <value>19</value>
            </property>
        </bean>
    SpringappController.java
    Code:
    package web;
    
    import business.*;
    import org.apache.commons.logging.*;
    import org.springframework.web.servlet.*;
    import org.springframework.web.servlet.mvc.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    
    public class SpringappController implements Controller {
    
        protected final Log logger = LogFactory.getLog(getClass());
    
        private UserManager userManager;
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            Map data = new HashMap();
    
            Date now = new java.util.Date();
            data.put("now", now.toString());
    
            String name = new String("manbearpig");
            data.put("name", name);
    
            List stuff = new ArrayList();
            stuff.add(new String("man"));
            stuff.add(new String("bear"));
            stuff.add(new String("pig"));
            data.put("stuff", stuff);
    
            List users = getUserManager().getUsers();
            data.put("users", users);
    
            return new ModelAndView("hello", "data", data);
        }
    
        public UserManager getUserManager() {
            return userManager;
        }
    
        public void setUserManager(UserManager userManager) {
            this.userManager = userManager;
        }
    }
    hello.jsp
    HTML Code:
    <%@ include file="/WEB-INF/jsp/include.jsp" %>
    
    <html>
    <head>
      <title><fmt:message key="title"/></title>
    </head>
    
    <body>
    <h1><fmt:message key="heading"/></h1>
    
    <p>
      <fmt:message key="greeting"/>
      <c:out value="${data.now}"/>
    </p>
    
    <p>
      <fmt:message key="name"/>
      <c:out value="${data.name}"/>
    </p>
    
    <h2><fmt:message key="stuff"/></h2>
    <c:forEach items="${data.stuff}" var="thing">
      <c:out value="${thing}"/><br/>
    </c:forEach>
    
    <h2><fmt:message key="users"/></h2>
    <c:forEach items="${data.users}" var="user">
      <c:out value="${user.username}"/> - <c:out value="${user.age}"/><br/>
    </c:forEach>
    
    </body>
    </html>

  • #2
    My guess, without any testing of your code or so, is that you've forgat to add the JSTL-TAG-library reference to you JSP-file (and to web.xml-file).

    Add this line to your JSP-file (the first line of the JPS file)
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

    and following setting to your web.xml (see the web.xml DTD for the right "element order"):
    <taglib>
    <taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
    <taglib-location>/WEB-INF/tlds/c.tld</taglib-location>
    </taglib>

    (I'm not sure that is it mandatory to have these lines in your web.xml-file?? I have though...).

    The tag-library can be found from jstl.jar-file. Try to find it from some JAR-repository (recommendation: start using maven if it's not already in your toolbox !!).

    Comment


    • #3
      I put the references in the included file at the top. And my <c:out> tags are working fine.

      Comment


      • #4
        Originally posted by Checkerbelt View Post
        I put the references in the included file at the top. And my <c:out> tags are working fine.
        Sorry... my fault, didn't read your 1st post properly.

        Try to list the Strings without the 'data' prefix like

        <c:forEach items="${stuff}" var="thing">
        <c:out value="${thing}"/><br/>
        </c:forEach>

        I noticed one other thing. Usually I don't transfer the 'model' name (map) to the view:
        return new ModelAndView("caseList", model);

        where's the 'model' is the Map containing the data. I didn't test that has this any affect on anything.

        Comment


        • #5
          The step-by-step guide I was using instructed me to use the three paramter constuctor:
          http://static.springframework.org/sp...elAndView.html
          It seems this constructor takes a name to be given to the model, and one single model.

          Comment


          • #6
            My latest experiments

            Code:
                public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                    ModelAndView mav = new ModelAndView("hello");
            
                    mav.addObject("now", new Date().toString());
                    mav.addObject("usernames", userManager.getUsernames());
            
                    return mav;
                }
            HTML Code:
              <h2>Now</h2>
              <c:out value="${now}"/>
            
              <h2>Usernames</h2>
              <ul>
              <c:forEach items="${usernames}" var="username">
                <li><c:out value="${username}"/></li>
              </c:forEach>
              </ul>
            
              <h2>Usernames array</h2>
              <c:out value="${usernames}"/>
            
              <h2>Numbers</h2>
              <c:forEach var="i" begin="3" end="20" step="3">
                <c:out value="${i}"/><br/>
              </c:forEach>
            Results:
            Now
            Tue Oct 10 16:31:48 PDT 2006

            Usernames
            * ${usernames}

            Usernames array
            [matt, ceme, kid, senior]

            Numbers
            3
            6
            9
            12
            15
            18
            As you can see the <c:out> is working for the date ("Now").
            The username array is in the jsp ("Username array"). Those are my four users.
            The <c:forEach> isn't a completely useless tag ("Numbers").
            But the <c:forEach> won't list out the usernames.

            I'm using the Spring 2.0 Framework, but I don't think that matters.

            Comment

            Working...
            X