java - springMVC配置的controller无法返回jsp文件
迷茫
迷茫 2017-04-18 10:54:02
0
4
1211

当启动完tomcat后,它首先展示的是index.jsp ,当我输入http://localhost:8080/Spring_no_2/时,出现

HTTP Status 404 – Not Found

Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
  1. 根据spring in action中的springMVC这一章中的demo进行测试,环境是mac下的Ideallij,jkd1.8,tomcat9

  2. 配置好了springMVC,相关的xml文件并且启动tomcat后(省略了配置静态资源和SpitterService类),无法通过访问url使controller返回在WEB-INF/views中的jsp文件

以下是代码文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>spitter</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spitter</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

spitter-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: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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">



    <!--use annotations to create the mapping between-->
    <!-- url and class deal with request(Controller) -->
    <mvc:annotation-driven/>

    <!--scan the component and auto regist as bean-->
    <context:component-scan base-package="com.springmvc.controller"/>

    <!--Use this bean to map the jsp file according to the name return by Controller-->
    <!--It will automatically add the prefix and suffix to the name string-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

controller

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by han on 29/3/2017.
 */
@Controller
public class HomeController {
//    public static final int DEAFAULT_SPITTLES_PER_PAGE = 25;


    public HomeController() {
        System.out.println("-------HomeController init-------");
    }

    @RequestMapping("/")
    public String showHomePage() {

        System.out.println("-------showHomePage Method show-------");


        return "home";
    }
}

我的文件结构是

请问为什么无法使home.jsp呈现出来?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

모든 응답(4)
伊谢尔伦

문제를 발견했는데 디렉토리와 Tomcat 구성이 모두 정확합니다.
하지만 web.xml의 다음 코드 때문에

으아악

결과적으로 모니터링 구성 파일은 spitter-servlet.xml의 구성을 읽지 못합니다.
두 가지 해결 방법이 있습니다.

1위:

은 <context-param>에 spitter-servlet.xml의 주소를 추가하여 spitter-servlet.xml과 applicationContext.xml에 있는 Bean과 기타 구성을 읽어오게 하는 것입니다.
Spring in action 4th edition에서 언급했듯이 DispatcherServlet은 컨트롤러, 뷰 확인자, 핸들러 매핑과 같은 웹 구성 요소가 포함된 Bean을 로드하는 반면 ContextLoaderListener는 애플리케이션의 다른 Bean을 로드하는 것이 일반적입니다. -애플리케이션의 백엔드를 구동하는 계층 및 데이터 계층 구성 요소.
구성의 서로 다른 부분을 처리하기 위해 두 개의 서로 다른 구성 파일이 설정됩니다

두 번째:

으아악
大家讲道理

님, -------showHomePage 메소드 show---------백그라운드에 인쇄되나요?

Peter_Zhu

먼저 Tomcat 구성이 올바른지 확인하세요.
localhost:8080에 성공적으로 액세스할 수 있나요?
showHomePage 메소드를 다시 살펴보세요

刘奇

기본 프로젝트 구조 웹 레이어는 webapp 디렉토리를 사용합니다. tomcat에 배포된 프로젝트의 WEB-INF/views 디렉토리에 home.jsp가 실제로 존재하는지 확인할 수 있나요?

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!