java - springMVC配置的controller無(wú)法返回jsp文件
問(wèn)題描述
當(dāng)啟動(dòng)完tomcat后,它首先展示的是index.jsp ,當(dāng)我輸入http://localhost:8080/Spring_no_2/時(shí),出現(xiàn)
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.
根據(jù)spring in action中的springMVC這一章中的demo進(jìn)行測(cè)試,環(huán)境是mac下的Ideallij,jkd1.8,tomcat9
配置好了springMVC,相關(guān)的xml文件并且啟動(dòng)tomcat后(省略了配置靜態(tài)資源和SpitterService類(lèi)),無(wú)法通過(guò)訪問(wèn)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'; }}
我的文件結(jié)構(gòu)是
請(qǐng)問(wèn)為什么無(wú)法使home.jsp呈現(xiàn)出來(lái)?
問(wèn)題解答
回答1:找到了問(wèn)題所在了,目錄,tomcat配置都對(duì)。但是因?yàn)樵趙eb.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>
導(dǎo)致了監(jiān)聽(tīng)的配置文件不讀取spitter-servlet.xml中的配置,解決方法有兩個(gè):
第一:
是在 <context-param>中加入spitter-servlet.xml的地址,這樣spitter-servlet.xml和applicationContext.xml中的Bean等配置都會(huì)被讀取到。在Spring in action 4th edition中說(shuō)到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.兩個(gè)不同的配置文件是為了應(yīng)對(duì)不同部分的配置而設(shè)置的
第二:
就是刪除以上代碼的配置。會(huì)自動(dòng)讀取spitter-servlet.xml中的配置。回答2:
親,-------showHomePage Method show-------后臺(tái)有沒(méi)有打印出來(lái)?
回答3:先看看tomcat配置的對(duì)不對(duì)。localhost:8080可以訪問(wèn)成功嗎?再看看showHomePage方法
回答4:默認(rèn)項(xiàng)目結(jié)構(gòu)web層是使用的webapp目錄,你可以看下部署到tomcat的項(xiàng)目的WEB-INF/views目錄下是否真的存在home.jsp?
相關(guān)文章:
1. ddos - apache日志很多其它網(wǎng)址,什么情況?2. 老師 我是一個(gè)沒(méi)有學(xué)過(guò)php語(yǔ)言的準(zhǔn)畢業(yè)生 我希望您能幫我一下3. PHP類(lèi)中的$this4. PHP單例模式5. php自學(xué)從哪里開(kāi)始?6. Thinkphp 下載地址找不到了?7. index.php錯(cuò)誤,求指點(diǎn)8. nginx 504 Gateway Time-out 請(qǐng)問(wèn)如何設(shè)置9. 在cmd下進(jìn)入mysql數(shù)據(jù)庫(kù),可以輸入中文,但是查看表信息,不顯示中文,是怎么回事,怎新手,請(qǐng)老師10. 導(dǎo)入數(shù)據(jù)庫(kù)不成功
