java - display welcome messsage on browser using spring mvc -


here trying print message "welcome spring - mvc" on browser. not getting printed. here controller class handles 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 spring - mvc");          return "welcome";     }     @requestmapping(value="/helloagain",method=requestmethod.get)     public string sayhelloagain(modelmap model){         model.addattribute("message", "welcome again spring - mvc");          return "welcome";     } } 

here welcome.jsp , view displayed

<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%>

<html>  <head>  <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>helloworldxmlspring</title>  </head>  <body>  <h1>welcome spring mvc</h1>  <h3>${message}</h3>  <h2>hiiiiiiiiiii..</h2>  </body>  </html 

>

here 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 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 spring mvc , hiiiiiiiiiii..

but model data sent servlet . i.e {message} not printing. reason?

@requestmapping(value="/",method=requestmethod.get) public modelandview sayhello(model model){     model.addattribute("message", "welcome spring - mvc");     return new modelandview("welcome"); } @requestmapping(value="/helloagain",method=requestmethod.get) public modelandview sayhelloagain(model model){     model.addattribute("message", "welcome again spring - mvc");     return new modelandview("welcome"); }     

in spring frameworkorg.springframework.ui.model designed adding attributes model. allows accessing overall model java.util.map. model attribute used data/information transfer barrier in spring web application.

org.springframework.web.servlet.modelandview holder both model , view in web mvc framework.this class merely holds both make possible controller return both model , view in single return value.

so best practice use return new modelandview("welcome"); or return new modelandview("welcome","data",model); , pass view name in controller. because return "welcome"; may fail add model data in view.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -