mvc框架怎么打开一个html,问题之Spring MVC配置后,可以打开jsp页面,但打不开html页面...

本文介绍了如何配置SpringMVC,但遇到静态资源无法访问的问题。通过分析配置,发现原因是DispatcherServlet拦截了所有请求,导致静态资源URL未匹配到处理器。解决方法是调整web.xml中 DispatcherServlet 的路径,使其只拦截特定资源。
摘要由CSDN通过智能技术生成

一、配置Spring MVC

1.导入jar

spring-framework-4.x.x.RELEASE-dist.zip压缩文件

d1ab776baea7c116558d20a0ba496d9c.png

解压之后将jar放入

57cc39e53a16e3d7385c43d03fe86523.png

2.在web.xml中配置DispatcherServlet

spring

org.springframework.web.servlet.DispatcherServlet

1

spring

/

3.加入spring mvc 的配置文件spring-servlet.xml

注意:框架默认读取 {servlet-name}-servlet.xml是配置文件,所以我们在web.xml中写了

spring

那么我们写的配置文件就是   spring-servlet.xml

0e6711cf7566cebacd5d44144662d066.png

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.0.xsd"default-lazy-init="false">

4.编写处理请求的处理器controller,并标记为处理器

packagecom.ttz.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;

@Controllerpublic classHelloWorld {

@RequestMapping("/helloworld")publicString hello() {

System.out.println("hello world");return "success";

}

}

5.编写视图(页面)

hello.jsp

Insert title here

helloworld

success.jsp

Insert title here

成功!

二、问题

启动tomcat可以成功打开hello.jsp,点击后跳转到success.jsp。

但是编写index.html,和success.html,均无法打开

三月 24, 2019 10:02:17 下午 org.springframework.web.servlet.PageNotFound noHandlerFound

警告: No mapping found for HTTP request with URI [/Springmvc-01/index.html] in DispatcherServlet with name 'spring'

三、解决

1.原因分析

spring

/

从配置上分析,如此配置会把所有的请求都会进行拦截,交给spring去处理。而spring所有请求的URL都是在controller中使用注解@RequestMapping标明,所以这样的情况下访问静态资源是访问不到的。

spring将index.html页面拦截成请求,而在接口层HelloWorld中没有该请求url对应的处理方法。

???不明白为什么jsp可以

2.解决办法

修改web.xml

spring

/api/*

注意:不能写   /api/      ,得写/api/*

对应修改页面的请求

helloworld

配置会把/api/*所有的请求都会进行拦截

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值