9

Firstly, I am newbie in the world of Spring MVC.

I made simple program where Spring MVC will handle GET request and set a variable named "message". This variable should display set value in JSP but is not doing as expected. Code is getting compiled and running fine. Can you please suggest, what is being done wrong here?

web.xml

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

    <display-name>Archetype Created Web Application</display-name>

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

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

</web-app>

loginDispacher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <context:component-scan base-package="com.sandeep" />

    <!-- View resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

Authorization.java

@Controller
@RequestMapping("/authorization")
public class Authorization {
    String message = "This is message from Java class";

    @RequestMapping(method=RequestMethod.GET)
    public String printHello(ModelMap model){
        System.out.println("From controller");
        model.addAttribute("message", "Hellow Spring MVC Framework!");
        return "authorization";
    }

}

old authorization.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
   <head>
   <title>Hello Spring MVC</title>
   </head>
   <body>
   <h2> <c:out value="${message}" /> </h2>
   </body>
</html>

updated and working authorization.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello Spring MVC</title>
</head>
<body>
    <h2>
        <c:out value="${message}" />
    </h2>
</body>
</html>

Outputenter image description here

sidgate
  • 14,650
  • 11
  • 68
  • 119
Sandeep
  • 397
  • 3
  • 8
  • 25
  • Try adding the header `<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>` to your `.jsp` file. – chrylis -cautiouslyoptimistic- Dec 29 '13 at 07:23
  • Could you update the question and show your complete `web.xml` (including the XML schema definitions). – Alexey Dec 29 '13 at 07:34
  • @Alexey - Have updated web.xml and jsp code. – Sandeep Dec 29 '13 at 07:41
  • Sandeep, I think your modification of the JSP page is not relevant to the problem. There was some other reason why it did not work from the first attempt. `<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>` is also not necessary. – Alexey Dec 29 '13 at 22:35

5 Answers5

13

if none of the above works. add this in your jsp page:

<head> <%@ page isELIgnored="false" %> </head>

source: mkyong

Fahad
  • 346
  • 2
  • 16
9

Your problem is not related to Spring MVC. ${message} is EL (Expression Language). It's a part of Java EE (and a former part of the JSP specification). It does not work on your page for some reason.

Try to replace the beginning of your web.xml with the following:

<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
Alexey
  • 2,542
  • 4
  • 31
  • 53
  • Did as suggested, but no luck. Still the output is same. I am using Eclipse Kelper, Java 7, Tomcat 7.0 and Maven + Spring IDE are downloaded from Marketplace. So required EL should be supported. I am running on Ecplise, is that a problem? – Sandeep Dec 29 '13 at 07:55
  • have updated web.xml code with what was suggest in the forum also, but output is same :( Any other possible suggestion? – Sandeep Dec 29 '13 at 08:02
  • Following your advise that problem is with EL, I have updated header of my jsp and it is working fine. :) – Sandeep Dec 29 '13 at 08:08
  • I already have following jstl added in my pom.xml. I am using Tomcat, so adding yours one should be incorrect. Right? jstl jstl 1.2 compile – Sandeep Dec 29 '13 at 08:10
  • @Sandeep Correct. I thought I might not have one. – Alexey Dec 29 '13 at 08:18
  • Thanks Alexey for your help and time :) – Sandeep Dec 29 '13 at 08:55
  • @Sandeep You are welcome! Why don't you accept the answer? Are you waiting for a better one? :)) – Alexey Dec 29 '13 at 08:58
  • Accepted the answer :) – Sandeep Dec 29 '13 at 09:22
4

Your web.xml is declaring your application as being a Servlet 2.3 compatible web application

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

Expression Language (EL) was introduced in 2.4.

Change that and you should be in business (as long as your container also supports it).

You can find templates for the different versions here.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

Just add jstl dependency if it is a maven project.

<dependency>
     <groupId>jstl</groupId>
     <artifactId>jstl</artifactId>
     <version>1.2</version>
</dependency>

It worked for me.

-1

Add this in your jsp page:

<%@ page isELIgnored="false" %>

  • Please take the time to read the [How to Answer](http://stackoverflow.com/help/how-to-answer) guide, then edit your answer. – Johannes Dorn Mar 22 '17 at 21:47