yashu
yashu

Reputation: 29

display welcome messsage on browser using spring mvc

Here i am trying to print message "Welcome from spring - mvc" on browser. But its not getting printed. Here is my controller class which handles the request.

package org.spring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod;

@Controller public class HelloWorldController {

    @RequestMapping(value="/",method=RequestMethod.GET)
    public String sayHello(Model model){
        model.addAttribute("message", "Welcome from spring - mvc");

        return "welcome";
    }
    @RequestMapping(value="/helloAgain",method=RequestMethod.GET)
    public String sayHelloAgain(ModelMap model){
        model.addAttribute("message", "Welcome again from spring - mvc");

        return "welcome";
    }
}

Here is the welcome.jsp, a view to be displayed.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Helloworldxmlspring</title>
</head> 
<body> 
<h1>Welcome to Spring MVC</h1> 
<h3>${message}</h3>
<h2>hiiiiiiiiiii..</h2> 
</body> 
</html>

Here is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>HelloWorldxml</display-name>
    <welcome-file-list>
    <welcome-file>/WEB-INF/views/welcome.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>dispatch</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>

            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatch-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatch</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Here is my bean configuration file.

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

<context:component-scan base-package="org.spring.controller.HelloWorldController"/>


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

<property name="prefix">
<value>/WEB-INF/views/</value></property>
<property name="suffix">
<value>.jsp</value></property>
</bean>

</beans>

Its printing

Welcome to spring MVC and hiiiiiiiiiii..

But the model data sent by servlet. i.e {message} is not printing. what is the reason?

Upvotes: 1

Views: 4202

Answers (2)

Biraj Sahoo
Biraj Sahoo

Reputation: 44

@RequestMapping(value="/",method=RequestMethod.GET)
public ModelAndView sayHello(Model model){
    model.addAttribute("message", "Welcome from spring - mvc");
    return new ModelAndView("welcome");
}
@RequestMapping(value="/helloAgain",method=RequestMethod.GET)
public ModelAndView sayHelloAgain(Model model){
    model.addAttribute("message", "Welcome again from spring - mvc");
    return new ModelAndView("welcome");
}    

In Spring frameworkorg.springframework.ui.Model is primarily designed for adding attributes to the model. Allows for accessing the overall model as a java.util.Map. Model attribute is used as a data/information transfer barrier in spring web application.

org.springframework.web.servlet.ModelAndView is a holder for both Model and View in the web MVC framework.This class merely holds both to make it possible for a controller to return both model and view in a single return value.

So it is always best practice to use return new ModelAndView("welcome"); or return new ModelAndView("welcome","DATA",model); and pass the view name in controller. Because return "welcome"; may sometimes fail to add Model data in View.

Upvotes: 1

Amer Qarabsa
Amer Qarabsa

Reputation: 6574

You need to use Model not ModelMap, ModelMap contains information for model only.

@RequestMapping(value="/helloAgain",method=RequestMethod.GET)
public String sayHelloAgain(Model model){
model.addAttribute("message", "Welcome again from spring - mvc");

return "welcome";
}

Do this for all your controller API's which need to access view.

Upvotes: 1

Related Questions