亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

當(dāng)啟動(dòng)完tomcat后,它首先展示的是index.jsp ,當(dāng)我輸入http://localhost:8080/Spring_no_2/時(shí),出現(xiàn)

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. 根據(jù)spring in action中的springMVC這一章中的demo進(jìn)行測試,環(huán)境是mac下的Ideallij,jkd1.8,tomcat9

  2. 配置好了springMVC,相關(guān)的xml文件并且啟動(dòng)tomcat后(省略了配置靜態(tài)資源和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";
    }
}

我的文件結(jié)構(gòu)是

請(qǐng)問為什么無法使home.jsp呈現(xiàn)出來?

迷茫
迷茫

業(yè)精于勤,荒于嬉;行成于思,毀于隨。

reply all(4)
伊謝爾倫

I found the problem. The directory and tomcat configuration are all correct.
But because of the following code in 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>

As a result, the monitoring configuration file does not read the configuration in spitter-servlet.xml.
There are two solutions:

First:

Add the address of spitter-servlet.xml to <context-param>, so that the Bean and other configurations in spitter-servlet.xml and applicationContext.xml will be read.
As mentioned in Spring in action 4th edition Whereas DispatcherServlet is expected to load beans containing web components such as controllers, view resolvers, and handler mappings, ContextLoaderListener is expected to load the other beans in your application. These beans are typically the middle- tier and data-tier components that drive the back end of the application.
Two different configuration files are set up to deal with different parts of the configuration

Second:

就是刪除以上代碼的配置。會(huì)自動(dòng)讀取spitter-servlet.xml中的配置。


大家講道理

Dear, -------showHomePage Method show-------is it printed in the background?

Peter_Zhu

First check whether the tomcat configuration is correct.
Can localhost:8080 be accessed successfully?
Look at the showHomePage method again

劉奇

The default project structure web layer uses the webapp directory. You can check whether home.jsp really exists in the WEB-INF/views directory of the project deployed to tomcat?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template