在搭建spring mvc,maven web项目时,遇到了这样一个问题:访问index.jsp是可以的,但就是访问不到controller层映射的jsp地址,网上搜了很多办法,从昨天开始,到现在,终于解决了,昨天搜的办法都无效,有说什么配置文件问题的,web.xml配置问题,application-servlet.xml配置问题,我就是很奇怪,我的配置文件是对的,不知道为什么会出错,今天意外间又搜到一个答案,竟然解决了 ,哈哈哈,有时候真的觉得,问题到了一定时间真的会自然解决,之前很多次也是这样,神奇了。
要把依赖都加到classpath下:
步骤:项目右键->properties->Deployment Assembly-->add-->Java Build Path Entries->Maven dependencies
再 项目右键->maven ->update ,重启项目就可以啦。
虽然解决了,但我其实还是有点怀疑的, 因为我昨天的时候,一开始是可以访问的,后面我不知道为啥特然就访问不到了,在此期间我也没改配置文件,只是加了个controller,表示很奇怪。
然后今天把依赖加到classpath下竟然好了
把我的项目目录粘一下:以便我以后参考哈哈哈哈
重要的就是web.xml和applicationContext-servlet.xml的配置了
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 配置DispatcherServlet -->
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置DispatcherServlet的一个初始化参数:配置SpringMvc配置文件的位置和名称 -->
<!-- 实际上也可以不通过 contextConfigLocation 来配置Spring MVC的配置文件,而使用默认的
默认的配置文件为: /WEB-INF/<servlet-name>-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-servlet.xml</param-value>
</init-param>
<!-- 默认的 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是我之前看视频学到的web.xml的配置。
applicationContex-servlet.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 自动扫描com.baobaotao.web 包下的@Controller标注的类控制器类 -->
<context:component-scan base-package="com" />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<mvc:annotation-driven />
<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/"
p:suffix=".jsp" />
</beans>
现在觉得配置文件其实蛮简单的嘛,没什么太多东西,搞不懂自己为什么卡壳那么久....