2017-08-11 80 views
0

我有彈簧mvc的問題。 我試着學習與spring mvc handlerMapping。我有一些錯誤 此錯誤 http://localhost:8080/springmvc/page1在Spring MVC HandleMapping中的HTTP狀態404

我解決不了這個問題 我只是想學習Spring MVC的。 注意 有JSP文件夾下的文件夾02.01.beanNameHandlerMapping.view

HTTP Status 404 - /springmvc/WEB-INF/jsp/02.01.beanNameHandlerMapping.view/sayfa1.jsp 

    type Status report 

message /springmvc/WEB-INF/jsp/02.01.beanNameHandlerMapping.view/sayfa1.jsp 

description The requested resource is not available. 

Apache Tomcat/8.0.45` 

這是我的代碼

的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>springmvc</display-name> 


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

     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>    

       /WEB-INF/02.00.appContext.xml 

      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

02.00.appContext .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 
    "> 


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

      <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> 

    <bean name="/page1" class="_02_01.beanNameUrlHandlerMapping.controller.Page1Controller" /> 
    <bean name="/page2" class="_02_01.beanNameUrlHandlerMapping.controller.Page2Controller" /> 
    <bean name="/page3*" class="_02_01.beanNameUrlHandlerMapping.controller.Page3Controller" /> 
</beans> 

Page1Controller.java

import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.AbstractController; 

public class Page1Controller extends AbstractController { 

    @Override 
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 

     ModelAndView modelAndView = new ModelAndView("02.01.beanNameHandlerMapping.view/sayfa1"); 
     modelAndView.addObject("message", "Hello Page1"); 
     return modelAndView; 
    } 

} 

sayfa1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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>BeanNameUrlHandlerMapping</title> 
</head> 
<body> 
    <h2>${message}</h2> 
</body> 
</html> 

我能不能解決問題。我該怎麼辦?

在此先感謝

回答

0

感謝您的回答 但對不起我的錯誤02.01.beanNameHandlerMapping.view我在文件夾名稱中留下了一個空格。 所以它調用文件時出錯。所以找不到jsp

0

當初始化控制器內部的ModelAndView對象只是通過這sayfa1型號名稱。