0

So, I'm trying to learn some Spring MVC and the first tutorial I try has a model.addAttribute("printme", "From spring"); and in the JSP a ${printme}.

My controller is simple:

@RequestMapping(value = "index", method = RequestMethod.GET)
public String index(Model modelMap) {
    System.out.println("on method");
    modelMap.addAttribute("printme", "Hello Spring FROM INDEX !!");
    return "index";
}

When I run the code it doesn't work, so I started adding to the JSP.

I wound up with this in the body:

        <h1>
            ${param.printme}
            <br />
            ${printme}
            <br />
            ${requestScope.printme}
            <br />
            <%=request.getParameter("printme")%>
            <br />
            <%=request.getAttribute("printme")%>
            <br />
            <%=pageContext.findAttribute("printme")%>
        </h1>

and my output source looks like this:

    <h1>

            <br />

            <br />
            Hello Spring FROM INDEX !!
            <br />
            null
            <br />
            Hello Spring FROM INDEX !!
            <br />
            Hello Spring FROM INDEX !!
        </h1>

I expected param.printme to me empty string, as well as null from request.getParameter().

Shouldn't ${printme} search requestScope and find it?

Shouldn't ${printme} be the same as

  • ${requestScope.printme}
  • <%=requestScope.getAttribute("printme")%>, and
  • <%=pageContext.findAttribute("printme")%>?

What's going on here, why isn't ${printme} finding the attribute?

I know I can just keep using ${requestScope.printme}, but it's more verbose, and I want to know why it's acting the way it is.

In case it matters I'm using Tomcat7.0.52, Spring 4.0 xsds, and java ee 3.0 xsds.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MatrixPeckham
  • 447
  • 1
  • 4
  • 16

2 Answers2

1

I have the following simple project:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>biz.tugay</groupId>
    <artifactId>smvcelex</artifactId>
    <packaging>war</packaging>

    <version>1.0-SNAPSHOT</version>
    <name>smvcelex Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.5.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>smvcelex</finalName>
        <plugins>
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.1.v20140609</version>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd"
         version="3.0">

    <servlet>
        <servlet-name>springDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servletContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <url-pattern>*.jspf</url-pattern>
            <page-encoding>UTF-8</page-encoding>
            <scripting-invalid>true</scripting-invalid>
            <trim-directive-whitespaces>true</trim-directive-whitespaces>
            <default-content-type>text/html</default-content-type>
        </jsp-property-group>
    </jsp-config>

</web-app>

servletContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

    <bean id="sampleController" class="biz.tugay.SampleController"/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

</beans>

SampleController.java

package biz.tugay;

/* User: koray@tugay.biz Date: 2016/08/16 */


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/")
public class SampleController {

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String indexController(Model model) {
        model.addAttribute("printme", "Hello Spring FROM INDEX !!");
        return "index.jsp";
    }
}

and finally index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Hello</title>
</head>
<body>
    ${printme}
</body>
</html>

And when I visit localhost:8080 I will see the text

Hello Spring FROM INDEX !!

just fine..

Please compare your project with this one, provide additional code and ask further if required.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • 1
    Well, I deleted the project that had the flaw, so I can no-longer compare what I had and what you provided. However using this, it works just fine... I do know that I had spring configured without explicitly declaring the controller class as a bean, (note: I used netbeans, and not maven) I'm still trying to figure out what xml bits control what, I had a context:annotation-config (which I don't think I was using properly) a context:component-scan with an appropriate base package and an mvc:annotation-driven... Marking your's as correct, as it gets the expected results.. – MatrixPeckham Aug 16 '16 at 19:56
  • @MatrixPeckham This may help you: http://stackoverflow.com/questions/35807056/how-many-ways-are-there-to-configure-the-spring-framework-what-are-the-differen – Koray Tugay Aug 17 '16 at 05:14
-3

It looks to me like you don't have jstl referenced in the top of your jsp files. The dollar sign is jstl shorthand. Make sure you have the following in the top of your jsp files.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
javaMoca
  • 175
  • 7
  • Sorry, this "answer" is utter nonsense. To learn what exactly JSTL and EL are, head to http://stackoverflow.com/tags/jstl/info and http://stackoverflow.com/tags/el/info – BalusC Aug 15 '16 at 07:06