Passing java variables to jsp with Spring @Controller ModelAndView

gTang

The controller class looks like this,

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
public class MainController {
    @RequestMapping(value = "/chart", method = RequestMethod.GET)
    public ModelAndView helloWorld() {
        System.out.println("Controller accessed...");
        String message = "<br><div style='text-align:center;'> CONTROLLER ACCESSED</div><br><br>";
        return new ModelAndView("chart", "hello", message);
    }
}

My jsp file, named "chart.jsp" looks like

<html>
<head>
<title>Spring MVC - Chart
    Example</title>
</head>
<body>${hello}

    <br>
    <br>
    <div style="font-family: verdana; padding: 10px; border-radius: 10px; font-size: 12px; text-align:center;">

        Insert Chart here. ${hello}<br>
    </div>
</body>
</html>

My problem now is that both "${hello}" in .jsp file stays as "${hello}" rather than the string I gave in index.jsp.

Also, the controller is accessed because the println printed successfully on the console.

ADDED: The web.xml looks like this as it could be the cause of the problem.

<!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>
    <display-name>Web Application</display-name>
    <!-- Processes application requests -->
    <!-- <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-context.xml</param-value>
    </context-param> -->

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>
    </servlet>        

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/chart.jsp</url-pattern>
    </servlet-mapping>  

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/chart.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>
ASP

add this line to jsp on top this will do the trick. (adding as an answer for someone looking similar)

<%@taglib uri="springframework.org/tags/form"; prefix="form"%> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring JSP passing variables

Passing parameters from jsp to Spring Controller method

Passing Array from Jsp to Controller [Spring MVC]

Passing parameter from controller to jsp in spring

Spring controller cannot passing parameter to jsp

Spring Boot & Thymeleaf,passing variables to the controller

My controller returns ModelAndView but it doesn't load jsp

Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax

JSP to Spring MVC Controller

AJAX - Spring Controller - JSP

Java Spring adding model to ModelAndView throws StackOverflowException

JUnit Test a Controller View with Spring Security throws java.lang.AssertionError: No ModelAndView found after HTTP Status 302

How to alternate the web controller response between HttpEntity and ModelAndView in Spring

Return same view controller using ModelAndView of Spring Web MVC

Which is better, return "ModelAndView" or "String" on spring3 controller

How javascript can get a ModelAndView object from a function in spring controller

Passing array of String in html form and submit to Java Spring Controller?

Passing variables before controller in CodeIgniter

Passing javascript variables to rails controller

How to call java constant variables from jsp expression language in Spring?

In MVC passing a list from JSP to the Controller

Not Returning JSP from Spring Controller

Spring with JSP - Controller class is not called

passing variables from one jsp page to another?

Passing variables while redirecting jsp pages

Pass multiple "random" variables by controller to jsp

Laravel - Passing variables from Middleware to controller/route

Passing variables from page view controller to another

Laravel problem passing variables from controller to mailer