2016-08-16 5 views
0

스프링 MVC & Tomcat을 시작하려고 합니다만, 최소한의 페이지를 렌더링하는 단일 핸들러를 만들 수있는 것 같지 않습니다. .스프링 MVC : URI [/hello.jsp]가있는 HTTP 요청에 대한 매핑이 없습니다.

다음 파일이 생성되었습니다

web.xml을

<?xml version="1.0" encoding="UTF-8"?> 
<web-app id="springapp" version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd 
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <servlet> 
     <servlet-name>springapp</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/springapp-servlet.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>springapp</servlet-name> 
     <url-pattern>*.jsp</url-pattern> 
    </servlet-mapping> 
    <display-name>Spring MVC Framework Test</display-name> 
</web-app> 

springapp-servlet.xml에

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

    <!--<mvc:annotation-driven />--> 
    <context:component-scan base-package="springapp.web.*"/> 

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

    <bean name="/hello.jsp" class="springapp.web.HelloController"/> 
</beans> 

Hello.jsp 라

,
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
<head> 
    <title>JSP Title</title> 
</head> 
<body> 

${message} 

</body> 
</html> 

HelloController.java

package springapp.web; 

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 HelloController { 
    @RequestMapping(value="/hello", method=RequestMethod.GET) 
    public String printHello(ModelMap model) { 
     model.addAttribute("message", "Hello Spring MVC Framework!"); 
     return "hello"; 
    } 
} 

폴더 구조 :

enter image description here

핸들러 찾는에서 내 시도 :

16-Aug-2016 16:31:18.867 WARNING [http-nio-8080-exec-8] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/hello.jsp] in DispatcherServlet with name 'springapp' 
16-Aug-2016 16:31:22.991 WARNING [http-nio-8080-exec-9] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/web/hello.jsp] in DispatcherServlet with name 'springapp' 
16-Aug-2016 16:31:23.023 WARNING [http-nio-8080-exec-10] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/springapp/web/hello.jsp] in DispatcherServlet with name 'springapp' 

수있는 사람의 항변 내가 뭘 잘못하고 있다고 말해?

+1

? 이것을 할 필요가 없습니다. 이 값은 "/ hello.jsp"입니까? –

+0

네 말이 맞다! 해당 줄을 제거해도 현재 상황에는 아무런 영향을 미치지 않습니다. – rolandvarga

+0

@RequestMapping (value = "/ hello.jsp"... 잘못되었습니다.) @RequestMapping (value = "/ hello"... 시도해야합니다. 질문을 업데이트하십시오. –

답변

1

코드는 다음과 같아야합니다

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.ui.ModelMap; 

    @Controller 
    @RequestMapping("/hello") 
    public class HelloController{ 

     @RequestMapping(method = RequestMethod.GET) 
     public String printHello(ModelMap model) { 
      model.addAttribute("message", "Hello Spring MVC Framework!"); 

      return "hello"; 
     } 

    } 

의 web.xml : 은 또한 당신이 /springapp/안녕하세요 대신 /springapp/hello.jsp

컨트롤러를 사용하여 실행해야합니다

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring MVC Application</display-name> 

    <servlet> 
     <servlet-name>springapp</servlet-name> 
     <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

HelloWeb-servlet.xml

<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="springapp.web" /> 

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

</beans> 

당신의 JSP 당신이이 줄에서 달성하려고하는 무엇

<%@ page contentType="text/html; charset=UTF-8" %> 
<html> 
<head> 
<title>Hello World</title> 
</head> 
<body> 
    <h2>${message}</h2> 
</body> 
</html> 
+0

고맙습니다. 그러나, 나는 아직도 당신이 그것을 작동하게하기 위해 바꾼 것을 찾으려고 노력하고 있습니다. – rolandvarga

1

폴더 구조와 springapp-servlet.xml을 볼 수있어서 hello.jsp 파일을 WEB-INF 폴더에 넣어야한다고 생각합니다. 이유는 페이지를 찾을 수없는 이유 일 수 있습니다. 이 유형의 오류 404 표시). springapp-servlet.xml에 따르면 jsp 파일이 WEB-INF 폴더 안에 있어야 함을 나타냅니다.

+0

파일을 WEB-INF 폴더에 저장했지만 여전히 404가 발생했습니다. – rolandvarga

+0

이 태그를 다음과 같이 선언해야한다는 것을 잊었습니다. 는 다음과 같이 설정하면 안됩니다 : id ="viewResolver "class ="org.springframework.web.servlet.view.InternalResourceViewResolver "> – Nto

+0

여기 와서 제거하십시오. – Nto

관련 문제