Announcement Announcement Module
No announcement yet.
Step-by-step snag Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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


        <bean id="springappController" class="web.SpringappController">
            <property name="userManager">
                <ref bean="userManagerBean"/>
        <bean id="userManagerBean" class="business.UserManager">
            <property name="users">
                    <ref bean="user1"/>
                    <ref bean="user2"/>
                    <ref bean="user3"/>
        <bean id="user1" class="business.User">
            <property name="username">
            <property name="age">
        <bean id="user2" class="business.User">
            <property name="username">
            <property name="age">
        <bean id="user3" class="business.User">
            <property name="username">
            <property name="age">
    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.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;
    HTML Code:
    <%@ include file="/WEB-INF/jsp/include.jsp" %>
      <title><fmt:message key="title"/></title>
    <h1><fmt:message key="heading"/></h1>
      <fmt:message key="greeting"/>
      <c:out value="${}"/>
      <fmt:message key="name"/>
      <c:out value="${}"/>
    <h2><fmt:message key="stuff"/></h2>
    <c:forEach items="${data.stuff}" var="thing">
      <c:out value="${thing}"/><br/>
    <h2><fmt:message key="users"/></h2>
    <c:forEach items="${data.users}" var="user">
      <c:out value="${user.username}"/> - <c:out value="${user.age}"/><br/>

  • #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="" %>

    and following setting to your web.xml (see the web.xml DTD for the right "element order"):

    (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 !!).


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


      • #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/>

        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.


        • #5
          The step-by-step guide I was using instructed me to use the three paramter constuctor:

          It seems this constructor takes a name to be given to the model, and one single model.


          • #6
            My latest experiments

                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:
              <c:out value="${now}"/>
              <c:forEach items="${usernames}" var="username">
                <li><c:out value="${username}"/></li>
              <h2>Usernames array</h2>
              <c:out value="${usernames}"/>
              <c:forEach var="i" begin="3" end="20" step="3">
                <c:out value="${i}"/><br/>
            Tue Oct 10 16:31:48 PDT 2006

            * ${usernames}

            Usernames array
            [matt, ceme, kid, senior]

            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.