0

Контролер возвращает не изменяет .jsp файл. Не выводиться информация в ModelMap. ${message} не изменяется на нужное значение. Файл с контролером

package com.pimmpo.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 MainController {

@RequestMapping(method = RequestMethod.GET, path = "/")
public String printHello(ModelMap model){
    model.addAttribute("message", "TEST");
    System.out.println("LOOK HERE");
    return "index";
}

//@RequestMapping(method = RequestMethod.GET, path = "/")
}

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>MainController</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>MainController</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

MainController-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

index.jsp

<html>
<body>
<h2>Hello World!</h2>
<h2>${message}</h2>
</body>
</html>
2
  • а что выводится? Сервер рестартовали после изменений?
    – tym32167
    19 июл 2019 в 7:27
  • @tym32167 выдаёт просто: Hello world! ${message}
    – whbrat
    19 июл 2019 в 7:55

1 ответ 1

0

Так пробовал выводить?

Нужно подключить в pom

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring3</artifactId>
    <version>3.0.8.RELEASE</version>
</dependency>

Выводить так

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Title</title>
</head>
<body>
    <div>Web Application. Passed parameter : th:text="${message}"</div>
</body>
</html>

Или так. Здесь нужна библиотека jstl.

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

<html>
   <head>
      <title>Header</title>
   </head>

   <body>
      <c:out value = "${messsage}"/>
   </body>
</html>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.