springspring-mvcjspel

Why ${message} appear as it is in View and not the real message passed in Spring MVC controller


I'm trying to learn Spring MVC and encountered an issue. Please see the attached code. I'm not getting the real message on View and instead ${message} on View. What could be the issue here?

This is the code for

Controller:

@Controller
public class HelloController {


@RequestMapping("/hello")
public String display(@RequestParam("name")String name,@RequestParam("pass")String pass,Model m) {
    
    System.out.println("Inside display ");
    if(pass.equals("admin")) {
        
        System.out.println("Inside IF ");
        String msg = "Hello "+name;
        m.addAttribute("message", msg);
        return "viewpage";
    }
    else {
        System.out.println("Inside ELSE ");
        String msg = "Sorry "+name+" Wrong Password";
        m.addAttribute("message", msg);
        return "errorpage";
    }

}

}

Here is my jsp viewpage where the message needs to be displayed.

viewpage.jsp:

<html>
<head>
<meta charset="UTF-8">
 </head>
 <body>
 ${message}
 </body>
 </html>

This the index page.

index.jsp:

<html>
<body>
<form action="hello">
Username : <input type="text" name="name">
Password : <input type="password" name="pass">
<input type="submit" name="submit">
</form>
</body>
</html>

Here is my spring-servlet.xml

spring-servlet.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" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<context:component-scan base-package="com.deepthi" />


<mvc:annotation-driven/>
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>        
 </bean>

Here is my web.xml

web.xml:

<!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>Archetype Created Web Application</display-name>
<servlet>  
<servlet-name>spring</servlet-name>  
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet- 
class>  
<load-on-startup>1</load-on-startup>    
</servlet>  
<servlet-mapping>  
<servlet-name>spring</servlet-name>  
<url-pattern>/</url-pattern>  
</servlet-mapping>  
</web-app>

I have added the pom.xml too here:

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>com.deepthi</groupId>
<artifactId>SprinMVCReqParam</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SprinMVCReqParam Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->  
<dependency>  
<groupId>org.springframework</groupId>  
<artifactId>spring-webmvc</artifactId>  
<version>5.1.1.RELEASE</version>  
</dependency>  
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->  
<dependency>    
<groupId>javax.servlet</groupId>    
<artifactId>servlet-api</artifactId>    
<version>3.0-alpha-1</version>    
</dependency>  
</dependencies>
<build>
<finalName>SprinMVCReqParam</finalName>
</build>
</project>

This is how my Output looks like : Sceenshot of output


Solution

  • The web.xml is wrong, it doesn't have any valuable information. So you can remove it. Instead you can register a DispatcherServlet with ServletContext using

    WebApplicationInitializer interface.

    You can find more information on spring-source site.

    This is a header of the web application descriptor. And it shows 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" >
        ^^^
    

    Also dependency on servlet-api should be provided. The servlet container already has this API used by your application.

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>