Arturo
Arturo

Reputation: 261

${} variables not showing - Spring MVC

I´m have a Maven project with Spring MVC, Spring Security and Spring Social.

When I put a message in the jsp, this show ${message} instace of the message.

I have jstl library.

My JSP:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>

    <script>
        var m = '${message}';
        alert(m);
    </script>

    ${pageContext.request.contextPath}
    <c:out value="${message}"></c:out>
    <br /><br />
    ${message}
    <br /><br />
    ${currentUserConnection.profileUrl}
    <br /><br />

</body>
</html>

My controller:

@Controller
public class LoginController {

    @RequestMapping(value = "/signup**")
    public ModelAndView signupHandler() {

        ModelAndView model = new ModelAndView("home");
        model.setViewName("home");

        model.addObject("message", "aa");

        return model;
    }

}

The web page show ${message} instace of aa. Also alert show ${message} too

What can I be doing wrong? Thanks in advance.

EDIT 1:

The configuration are in Annotations. My viewResolver:

@Bean(name = "viewResolver")
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix(viewPrefix);
    viewResolver.setSuffix(viewSuffix);
    return viewResolver;
}

SOLUTION:

The solution is the version of my web xml need to be greater than 2.5

Thanks @M.Deinum

Upvotes: 1

Views: 3243

Answers (3)

Rahul Gupta
Rahul Gupta

Reputation: 83

I also faced the same type of problem in which I put list inside ModelAndView object and tried to show that list on jsp page using foreach loop.

The problem arised because of version of web application I've used Servlet 2.3

I have exhausted my 2 hours, but search and found that adding

<%@ page isELIgnored="false" %>

I would really recommend you update your servlet version. We're coming up on 3.1 right now, doesn't make sense to be on 2.3

solved my problem, please try this.

Upvotes: 0

Maksym
Maksym

Reputation: 4574

Make sure that you have correct jsp resolver into your spring context xml.

Like:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

And proper Dispatcher Servlet configuration in your web.xml:

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

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
  </servlet-mapping>

And also for old Servlet 2.3 you can add fallow into jsp (see here) or ofc change your web.xml to new one:

<%@ page isELIgnored ="false" %> 

edited.

Upvotes: 2

M. Deinum
M. Deinum

Reputation: 124760

In most cases not working EL is related to a wrong version in the web.xml.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

For EL to work the version has to be higher then 2.3. Preferably the highest available for your container.

<?xml version="1.0" encoding="UTF-8"?>

<web-app
    version="3.0"
    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_3_0.xsd">

</web-app>

For a list of templates check this website.

Upvotes: 1

Related Questions