Results 1 to 10 of 10

Thread: View shows ${foo} instead of the real value

  1. #1
    Join Date
    Aug 2010
    Posts
    5

    Default View shows ${foo} instead of the real value

    just simple web app. I set string "bar" into variable foo, and try to show it in a jsp page. the controller and jsp is copied from mvc-showcase. The problem is when I run it on jetty, jsp page does not translate ${foo} into value "bar". Anything I am missing? By the way, I configure the dependency as spring-mvc 3.0.3.RELEASE in my Maven. Any suggestion is appreciated.

  2. #2
    Join Date
    Aug 2010
    Posts
    5

    Default


  3. #3
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    You'll hardly get any help without more info...post your controller and your jsp as a start...

  4. #4
    Join Date
    Aug 2010
    Posts
    5

    Default

    Quote Originally Posted by Enrico Pizzi View Post
    You'll hardly get any help without more info...post your controller and your jsp as a start...
    Thank you for the reply. the controller is as simple as

    @Controller
    public class MessageController {

    @RequestMapping(value="/show", method=RequestMethod.GET)
    public String prepare(Model model) {
    model.addAttribute("foo", "bar");
    model.addAttribute("fruit", "apple");
    return "show";
    }
    }

    and the jsp is

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>
    <html>
    <head>
    <title>My HTML View</title>
    </head>
    <body>
    <h1>foo = ${foo}</h1>
    <h1>fruit = ${fruit}</h1>
    </body>
    </html>

    no exception thrown. it just shows ${foo} and ${fruit} on the page instead of "bar" and "apple". Any clues? Thanks a lot.

  5. #5
    Join Date
    Aug 2010
    Posts
    5

    Default

    Thank you for the reply. the controller is as simple as

    Code:
    @Controller
    public class MessageController {
    	
    	@RequestMapping(value="/show", method=RequestMethod.GET)
    	public String prepare(Model model) {
    		model.addAttribute("foo", "bar");
    		model.addAttribute("fruit", "apple");
    		return "show";
    	}
    }
    and the jsp is

    Code:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>
    <html>
    <head>
    	<title>My HTML View</title>
    </head>
    <body>
    <h1>foo = ${foo}</h1>
    <h1>fruit = ${fruit}</h1>
    </body>
    </html>
    no exception thrown. it just shows ${foo} and ${fruit} on the page instead of "bar" and "apple". Any clues? Thanks a lot.

  6. #6
    Join Date
    Mar 2010
    Location
    Boston, MA
    Posts
    316

    Default

    Make sure you are using the right servlet version in your web.xml

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>   
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
       version="2.5">
    and if that doesnt work try adding this directive in your jsp
    Code:
    <%@ page isELIgnored="false" %>

  7. #7
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    Hi Gao,

    you can't use jstl EL directly inside a jsp page and expect it to be resolved...use a tag like the c:out tag instead:

    Code:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>
    <html>
    <head>
    	<title>My HTML View</title>
    </head>
    <body>
    <h1>foo = <c:out value="${foo}"/></h1>
    <h1>fruit = <c:out value="${fruit}"/></h1>
    </body>
    </html>
    and it will work...

  8. #8
    Join Date
    Mar 2010
    Location
    Boston, MA
    Posts
    316

    Default


    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page session="false" %>
    <html>
    <head>
    <title>My HTML View</title>
    </head>
    <body>
    <h1>foo = <c:out value="${foo}"/></h1>
    <h1>fruit = <c:out value="${fruit}"/></h1>
    </body>
    </html>
    Not true..JSTL and EL and two different things. EL can work perfectly fine without a c tag. EL is a feature of the JSP engine, not JSTL. Here is an example from one of my production applications

    Code:
     <tr>
    <td>Customer Name</td>
    <td>${customer.name}</td>
    </tr>

  9. #9
    Join Date
    Aug 2010
    Posts
    5

    Default

    Cool. It works after I change the servlet version to 2.5. The automatically generated one is 2.3 by STS. Thank you guys very much!

  10. #10
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    710

    Default

    EL has become a standard feature only for JSP 2.0...if you are using older JSP standard you only access EL through jstl tags...since direct access is not working for him, it means he is not using JSP 2.0 so he needs the tag...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •