jenius
jenius

Reputation: 257

Show data from controller into jsp page

I saw a lot of examples how to show data, which I defined and put into java controller, but I can't do it. The code is here.

@Controller
public class HomeController {
    @RequestMapping({"/","/test"})
    public String showHomePage(ModelMap model) {
        String mes = "Here I am";
        model.addAttribute("message",mes);
        return "new";
    }
}

new.jsp file

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>
${message}
</body>
</html>

When I'm starting the jsp page show only like this ${message}

mvc-dispatcher-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">

    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    <context:component-scan base-package="ru.sbt"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

Upvotes: 1

Views: 3583

Answers (1)

Mitul Sanghani
Mitul Sanghani

Reputation: 230

Do not directly hit the jsp page means do not put direct jsp name in url instead put /test(requestMapping url) that is call your controller and then controller will send jsp with model(data) to client request

Upvotes: 1

Related Questions