3

The output displays ${message} not the "Spring".
Is there any dependency needed to show the value of my message?
I already used Spring MVC but i used xml configuration. Am I missing something here? Hope you can help me figure this out.

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>

    </dependencies>

controller

package com.jwlayug.controller;

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

@Controller
public class ControllerA {
     @RequestMapping(value = "/hello")
       public String printHello(Model model) {
          model.addAttribute("message", "Spring");
          System.out.println("this method is called!");
          return "hellow";
       }


}

config

package com.jwlayug.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
// Marks this class as configuration
// Specifies which package to scan
@ComponentScan("com.jwlayug")
// Enables Spring's annotations
@EnableWebMvc
public class Config {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}


package com.jwlayug.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebInitializer implements WebApplicationInitializer{
    public void onStartup(ServletContext servletContext)
            throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(Config.class);

        ctx.setServletContext(servletContext);

        Dynamic servlet = servletContext.addServlet("dispatcher",
                new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);

    }
}

jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message}
<c:out value="${message}" />
</body>
</html>
4
  • Can you show us your beans configuration? i.e. please add controllerA-context.xml, if you have one. Commented Feb 18, 2014 at 8:15
  • i dont have any xml's. Commented Feb 18, 2014 at 8:25
  • I don't use this way, so I'm just guessing now mostly. I can see that your resolver specifies the used JSP, based on what's returned from the controller. But where is the configuration that specifies which controller is in which bean and so on? Just asking now. Commented Feb 18, 2014 at 8:43
  • take a look at this tutorial. javahash.com/spring-4-mvc-hello-world-tutorial-full-example Commented Feb 18, 2014 at 9:21

3 Answers 3

25

I found the solution by adding this two lines of code on top of your jsp..

<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
Sign up to request clarification or add additional context in comments.

2 Comments

That is correct solution. Mark your answer as "accepted answer" so that others will also get to it quickly
Thanks for the solution. It helps me. Note: I actually added only the 2nd line indicating the EL expression should not be ignored.
2

${message} is not a valid syntax for a JSP outside an EL expression like the one you have in <c:out/>.

Please have a look at http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

Comments

-1

You need to return the model to the view.

@Controller
public class ControllerA {
     @RequestMapping(value = "/hello")
     public ModelAndView printHello() {
         ModelAndView mav = new ModelAndView("viewname");
         mav.addObject("message", "Spring");
         return mav;
     }
}

then in your jsp:

${message}

2 Comments

He doesn't actually.. It's perfectly ok to return the name/path of the JSP file. It depends on the configuration. See the setupViewResolver
Still not working with this code. It shows ${message} not the Spring. Damn. this is hard >.< no error message..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.