亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:147日期:2024-01-04 13:45:05

問題描述

當啟動完tomcat后,它首先展示的是index.jsp ,當我輸入http://localhost:8080/Spring_no_2/時,出現

HTTP Status 404 – Not FoundType Status ReportDescription The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

根據spring in action中的springMVC這一章中的demo進行測試,環境是mac下的Ideallij,jkd1.8,tomcat9

配置好了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. */@Controllerpublic 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'; }}

我的文件結構是

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

請問為什么無法使home.jsp呈現出來?

問題解答

回答1:

找到了問題所在了,目錄,tomcat配置都對。但是因為在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>

導致了監聽的配置文件不讀取spitter-servlet.xml中的配置,解決方法有兩個:

第一:

是在 <context-param>中加入spitter-servlet.xml的地址,這樣spitter-servlet.xml和applicationContext.xml中的Bean等配置都會被讀取到。在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.兩個不同的配置文件是為了應對不同部分的配置而設置的

第二:

就是刪除以上代碼的配置。會自動讀取spitter-servlet.xml中的配置。回答2:

親,-------showHomePage Method show-------后臺有沒有打印出來?

回答3:

先看看tomcat配置的對不對。localhost:8080可以訪問成功嗎?再看看showHomePage方法

回答4:

默認項目結構web層是使用的webapp目錄,你可以看下部署到tomcat的項目的WEB-INF/views目錄下是否真的存在home.jsp?

標簽: java
相關文章:
主站蜘蛛池模板: 一级特色黄大片 | 亚洲精品成人在线 | 天堂成人av | 99精品欧美一区二区三区美图 | 日韩一级欧美一级毛片在线 | 久久综合亚洲一区二区三区 | 不卡一区二区在线 | 久草中文在线视频 | 久99频这里只精品23热 视频 | 中文字幕高清在线天堂网 | 国产v在线播放 | 欧美99视频 | 久久久久久毛片免费观看 | 国语精品视频在线观看不卡 | 成人午夜影院 | 亚洲国语在线视频手机在线 | 午夜香蕉网| 国产在线播放一区二区 | 精品国产品香蕉在线观看 | 亚洲欧美在线观看 | 模特三级在线观看 | 手机日韩理论片在线播放 | 免费欧洲毛片a级视频无风险 | 亚洲人成网7777777国产 | 国产日韩欧美自拍 | 亚洲国产精品线观看不卡 | 日本无卡码免费一区二区三区 | 日韩一级精品久久久久 | 欧美成a人免费观看久久 | 成人影院欧美大片免费看 | 国产1000部成人免费视频 | 久久久久久91 | 97视频精品 | 国产成人一区二区三中文 | 成人亚洲在线观看 | 在线播放亚洲视频 | 亚洲欧美卡通动漫丝袜美腿 | 久久不见久久见免费影院 | 一区二区三区高清在线 | 国产成人精品免费视频 | 草草影院www色欧美极品 |