2017-04-05 1 views
0

Spring MVC 프레임 워크를 사용하는 동적 웹 프로젝트에서 $ {}은 JSP 페이지에 변수 값을 표시하지 않습니다.

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>SpringMVCTest</display-name> 
    <welcome-file-list> 

    <welcome-file>index.jsp</welcome-file> 

    </welcome-file-list> 

    <servlet> 

    <servlet-name>dispatcher</servlet-name> 

    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    </servlet> 

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

</web-app> 

디스 - 서블릿

<?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:p="http://www.springframework.org/schema/p" 
    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.xsd"> 


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


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

</beans> 

BaseController.java 컨트롤러 클래스

,536 : 코드 아래

찾아주세요

package com.springtutorial.basecontroller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.servlet.ModelAndView; 


@Controller 

public class BaseController { 

    @RequestMapping(value="/", method=RequestMethod.GET) 
    public ModelAndView printHello(){ 
     return new ModelAndView("index", "message", "Hi"); 
    } 

} 

는 index.jsp를 단지를 배우기 시작하는 유일한 JSP 페이지

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 

<!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=UTF-8"> 
<title>Spring MVC Framework</title> 
</head> 
<body> 
<h1>learning</h1> 
    <h2>${message}</h2> 
</body> 
</html> 

나는 스프링 MVC 프레임 워크에 새로운 오전입니다. 나는 인터넷에서 코드를 확인했는데 거의 비슷하지만 여전히 운이 없다.

제가 누락 된 항목이 있으면 알려주십시오.

감사

답변

0

나는 BaseController.java 파일 및 디스패처-servlet.xml 파일의 변경했다 :

BaseController.java을

package com.springtutorial.basecontroller; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
//import org.springframework.web.bind.annotation.RequestMethod; 
//import org.springframework.web.servlet.ModelAndView; 


@Controller 

public class BaseController { 

    @RequestMapping("/") 
    public String index(Model model){ 
     model.addAttribute("message", "Hi"); 
     return "index"; 
    } 
} 

디스패처-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:p="http://www.springframework.org/schema/p" 
    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.xsd"> 


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


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

</beans> 

시도해보십시오. 그것은 나를 위해 일하고있다

관련 문제