출력에 "스프링"이 아닌 $ {message}가 표시됩니다.
내 메시지의 가치를 표시하는 데 필요한 종속성이 있습니까?
이미 스프링 MVC를 사용했지만 XML 구성을 사용했습니다. 내가 여기서 뭔가를 놓치고 있니? 당신이 이것을 이해하도록 도와주세요.스프링 MVC 모델 속성이 표시되지 않음
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
</dependencies>
제어기
package com.jwlayug.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ControllerA {
@RequestMapping(value = "/hello")
public String printHello(Model model) {
model.addAttribute("message", "Spring");
System.out.println("this method is called!");
return "hellow";
}
}
구성
package com.jwlayug.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration
// Marks this class as configuration
// Specifies which package to scan
@ComponentScan("com.jwlayug")
// Enables Spring's annotations
@EnableWebMvc
public class Config {
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
package com.jwlayug.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class WebInitializer implements WebApplicationInitializer{
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(Config.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher",
new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message}
<c:out value="${message}" />
</body>
</html>
당신이 우리에게 당신의 bean 구성을 보여줄 수 ? 즉, controllerA-context.xml이 있으면 추가하십시오. – Dropout
나는 xml이 없다. – james
나는이 방법을 사용하지 않는다. 그래서 나는 지금 대부분 추측하고있다. 여러분의 해석자가 컨트롤러에서 반환 된 것을 기반으로 사용 된 JSP를 지정하는 것을 볼 수 있습니다. 하지만 어떤 컨트롤러가 어떤 bean에 있는지를 지정하는 설정은 어디에 있습니까? 지금 물어보십시오. – Dropout