web项目加载图片资源

    在web项目中,用户会上传图片,这些图片会存在服务器上,而不是存在数据库或者应用程序路径下,同时在数据库存中入文件的路径。

<mvc:resources mapping="/Pictures/**" location="/WEB-INF/InfImages/"/>在spring-mvc中加了这句话,发现可以加载图片,路径为src="Pictures/pic2.jpg",但是web-inf中的就不行,不知道怎么回事?
<mvc:resources mapping="/res/**" location="file:D:/html_js_py/"/>加了这句话后,jsp页面就可以加载本地硬盘上的图片了,简直完美!!!这里还有一个问题,就是locat的值最好从配置文properties中加载,这样移植性好点。
<context:property-placeholder location="classpath:webConfig.properties"
                              ignore-unresolvable="true" />这样就好了${path}踩了足够多的坑我才会比他们强。

 

   这是一个比较重要的问题,也是开发过程中也解决的问题。当然,我可以跳过,但是成为合格的web开发人员,这必须要掌握,这设计到java.io相关的知识,现在在程序内部加载图,html没有问题,慢慢尝试其他的。

2、在spring中编写并注册拦截器。

<mvc:interceptors>
<!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->
<bean class="com.host.app.web.interceptor.AllInterceptor"/>
<mvc:interceptor>
<mvc:mapping path="/test/number.do"/>
<!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->
<bean class="com.host.app.web.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

 

posted @ 2018-04-25 16:38  懂得了才能做一些改变  阅读(1278)  评论(0编辑  收藏  举报