springmvc text html文件路径,SpringMVC之配置访问首页及html解析器

对于tomcat启动后怎么去加载首页的问题。由于了解少的缘故,这里跳坑用了很长时间。

一、默认加载规则

首先这里说明一下首页的加载规则,会优先去加载index.html;如果index.html不存在,则会找index.jsp;如果index.jsp不存在,则会返回404错误;

我们想配置指定的首页的话,在web.xml文件中添加配置指定文件

welcome.jsp

然后在webapp下创建welcome.jsp,启动项目后则会加载welcome.jsp;

welcome.html

添加此配置可以加载webapp下的welcome.html文件;

二、配置DispatchServlet之后的加载

DispatcherServlet是前端控制器设计模式的实现,提供SpringWebMVC的集中访问点;

我们知道Springmvc处理一个请求的加载过程,所以在创建完项目之后,我就参照网上资料配置了DispatchServlet,而这个配置,才是入坑的开始;我在web.xml文件中的配置:

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

SpringMVC

/

配置了前端控制器之后,我发现首页加载出问题了,404!!!按照默认加载规则来说,我webapp下存在index.html文件啊,页面找不到?然后我又创建了index.jsp文件放在webapp下,删除了index.html,页面加载到了!!!这里就使我疑惑了,然后我就想加载index.html,我就尝试第三种使用配置来访问:

index.html

还是404!!!加载不到html,我就纳闷的很,去看报错!!!

org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'SpringMVC'

没有找到URI为[/]的映射,那我就加一个映射,这里我参照网上配置也配置了视图解析器

同时也写了controller层接口返回index

aa438cedf4d0b154529fc93991f05e37.png

因为想返回html,所以配置了html后缀,但是仍然404,这里其实有错的,因为InternalResourceViewResolver是解析jsp的,无法处理html,改回jsp,同时打个断点看会不会访问这个接口。

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

SpringMVC

/

index.html

同时路径下存在index.jsp和index.html文件,这里访问成功,是index.jsp;(注意这里返回的页面名称不一定是index,可以是其他)

我绕在这里很久尝试方法,皆不可以,后来找到几种可以加载html的方法。

三、处理配置前端控制器访问html文件的方法(这里将index.html,index.jsp放回webapp下)

1.访问不到的原因是因为DisPatchServlet中配置url-pattern为"/",这里会匹配像/index这样的路径,但同时会拦截".html",".js",“.css”等静态文件, 但是不拦截jsp,所以可以访问到index.jsp;

当url-pattern值为"/*"时,这里会匹配到带后缀类型的url,基本是所有的url会被匹配,".jsp"和“.html”都不能访问,通过localhost:8080/index.jsp也无法访问,说明这里也拦截了".jsp"文件

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

SpringMVC

/

index.jsp

这里我注释了controller的接口,这个知识点有点模糊,因为网上资料说".jsp"和".html"是可以匹配到的,希望可以指出错误之处。贴出web.xml;

cf7fc783e9d2eaf853814c547b2f0890.png

这个解释的话就可以理解为啥会拦截到jsp了,同时这个解释搭配"/"时的情况,也就可以理解拦截静态资源访问不了index.html原因了。

当值为*.do或者*.action(扩展名自定义)时,匹配以.do或.action结尾的url;此时设置的首页可以访问到,无论是index.jsp和index.html。

四、当url-pattern为"/"时访问html处理

在这里,不能访问html文件是因为拦截了静态文件的访问,需要配置不拦截的静态路径,web.xml文件中配置:

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

SpringMVC

/

/static/index.html

spring-mvc文件的配置

然后在webapp下创建static文件夹,之后将静态文件就放在这个目录下

再次访问成功。but,“/*”仍然无法访问html,怎么解决呢?

参考网上,注掉了原有的视图解析器,spring-mvc.xml添加的配置:

org.springframework.web.servlet.view.freemarker.FreeMarkerView

true

.html

text/html; charset=UTF-8

applicationContext.xml配置:

0

UTF-8

0.##########

yyyy-MM-dd HH:mm:ss

true

ignore

web.xml文件配置:

Archetype Created Web Application

contextConfigLocation

classpath:applicationContext.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

org.springframework.web.context.ContextLoaderListener

SpringMVC

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

SpringMVC

/

pom文件添加这两个jar包,注意这里spring版本是4.1.6.RELEASE:

org.springframework

spring-context-support

4.1.6.RELEASE

org.freemarker

freemarker

2.3.22

主要是这两个jar包起作用;然后controller:

@RequestMapping(value = "/", method = RequestMethod.GET)

public String index(){

return "/welcome";

}

这样就可以访问到html了;不过不怎么会使用,大多使用jsp文件就满足了

0f427544a017dda008a4f71471dac625.png

这里还有很多东西需要掌握,比如web.xml文件的加载顺序等知识点,之后会补充

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值