Maison  >  Questions et réponses  >  le corps du texte

java - springMVC配置的controller无法返回jsp文件

当启动完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呈现出来?

迷茫迷茫2743 Il y a quelques jours1209

répondre à tous(4)je répondrai

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:56:02

    J'ai trouvé le problème, le répertoire et la configuration de Tomcat sont tous corrects.
    Mais à cause du code suivant dans web.xml

     <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>

    Par conséquent, le fichier de configuration de surveillance ne lit pas la configuration dans spitter-servlet.xml
    Il existe deux solutions :

    Première place :

    consiste à ajouter l'adresse de spitter-servlet.xml à <context-param>, afin que le Bean et les autres configurations dans spitter-servlet.xml et applicationContext.xml soient lus.
    Comme mentionné dans Spring in action 4e édition Alors que DispatcherServlet est censé charger des beans contenant des composants Web tels que des contrôleurs, des résolveurs de vues et des mappages de gestionnaires, ContextLoaderListener est censé charger les autres beans de votre application. Ces beans sont généralement au milieu. Composants de niveau et de données qui pilotent le back-end de l'application.
    Deux fichiers de configuration différents sont configurés pour gérer différentes parties de la configuration

    Deuxième :

    就是删除以上代码的配置。会自动读取spitter-servlet.xml中的配置。
    
    
    

    répondre
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:56:02

    Cher, -------showHomePage Method show-------est-il imprimé en arrière-plan ?

    répondre
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:56:02

    Vérifiez d'abord si la configuration de Tomcat est correcte.
    Localhost:8080 est-il accessible avec succès ?
    Regardez à nouveau la méthode showHomePage

    répondre
    0
  • 怪我咯

    怪我咯2017-04-18 10:56:02

    La couche Web de structure de projet par défaut utilise le répertoire webapp. Vous pouvez vérifier si home.jsp existe vraiment dans le répertoire WEB-INF/views du projet déployé sur tomcat ?

    répondre
    0
  • Annulerrépondre