brainZap
brainZap

Reputation: 85

JSTL variables not being displayed

I have been doing a couple of Spring tutorials and both have failed at the stage where the view prints out variables using JSTL.

The view:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/include.jsp" %>
<!DOCTYPE html>
<html>
 <head><title><fmt:message key="title"/></title></head>
  <body>
   <h1><fmt:message key="heading"/></h1>
   <p><fmt:message key="greeting"/><c:out value="${model.now}"/></p>
   <h3>Products</h3>
   <c:forEach items="${model.products}" var="prod">
    <c:out value="${prod.name}"/> <i>$<c:out value="${prod.number}"/></i><br><br>
   </c:forEach>
  </body>
 </html>

The fmt:message parts display on my page properly, but the ${model.now} part is not being rendered.

The above is on a page "hello.jsp", that includes "include.jsp" which contains:

taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"

JSTL is in my libraries folder (I'm using NetBeans)

Anyone know why this keeps happening and absolutely nothing is rendered?

Here is the controller:

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse            response)
throws ServletException, IOException 
    { 
    String now = (new java.util.Date()).toString();
    logger.info("Returning hello view with " + now);

    Map<String, Object> myModel = new HashMap<String, Object>();
    myModel.put("now", now);
    myModel.put("product", this.productManager.getProducts());
    return new ModelAndView("hello", "model", myModel);
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
si:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener- class>
</listener>

<servlet>
    <servlet-name>springapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Upvotes: 0

Views: 1982

Answers (3)

brainZap
brainZap

Reputation: 85

Have successfully discovered why this wasn't working:

I was incorrectly mapping to my hello view in my servlet, it was loading the page but was not correctly loading the model, hence why there were no variables being displayed

Upvotes: 1

geoand
geoand

Reputation: 64009

Add the following taglib:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

And also <%@ page isELIgnored="false"%>

Finally make sure that you have JSTL 1.2 taglib dependencies on your classpath

Upvotes: 0

Prasad
Prasad

Reputation: 3795

Just have form tag specified as:

    <html>
    <head>
    <%@ include file="/WEB-INF/jsp/include.jsp" %>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
    </head>
    <body">
    <form:form id="model" modelAttribute="model">
        <h1><fmt:message key="heading"/></h1>
        <p><fmt:message key="greeting"/><c:out value="${model.now}"/></p>
    </form:form>
    </body> 
    </html> 

Upvotes: 0

Related Questions