Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - Not getting value inside JSP view [duplicate]

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

like image 643
Sandeep Avatar asked Dec 29 '13 07:12

Sandeep


3 Answers

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

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

source: mkyong

like image 104
Fahad Avatar answered Oct 21 '22 02:10

Fahad


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">
like image 37
Alexey Avatar answered Oct 21 '22 00:10

Alexey


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.

like image 43
Sotirios Delimanolis Avatar answered Oct 21 '22 02:10

Sotirios Delimanolis