java - springMVC配置的controller无法返回jsp文件
问题描述
当启动完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'; }}
我的文件结构是
请问为什么无法使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?
相关文章:
1. java基础,求解答。2. css - 关于input标签disabled问题3. debian - docker依赖的aufs-tools源码哪里可以找到啊?4. javascript - 关于一段 for 循环代码执行顺序的问题5. javascript - 静态页面引公共头尾文件,js怎么写吖?6. javascript - vue 数据更新了。但是dom没有更新,,,,,如图7. html5 - 百度Ueditor代码高亮和代码段滚动条冲突是怎么回事?8. docker api 开发的端口怎么获取?9. java - 为什么要将Runnable接口的子类对象传递给Thread的构造函数?10. 如何调整IDEA中直接运行java程序的日志输出级别?