我们平时的jsp文件都是放到web目录下,
现在我们如果把jsp文件放到WEB-INF下,
就发现没有办法访问,这样的设计的目的就是为了提高项目访问的安全性
所以我们就访问不了了............................................................................................................................
遇到的问题
放到web-inf下的文件该如何访问呢?
解决的方案
WEB-INF下的所有文件必须通过转发的形式访问
打♥推荐
方式一 前端页面转发到我们指定的页面中
jsp页面代码
<%--转发到指定的页面中--%>
<jsp:forward page="WEB-INF/update.jsp"></jsp:forward>
方式二 后台代码控制单元进行转发跳转
@RequestMapping("update")
public String demo1(){
return "/WEB-INF/update.jsp";
}
♥方式三 自定义视图解析器
统一的给控制单元要跳转的路径增加前缀和后缀,
也就是不用我们手打多余重复的东西
代码可以写成这样
applicationConetxt-MVC.xml
把jsp文件放到web-inf下为了提高项目访问的安全性 。
使用自定义视图解析器InternalResourceViewResolver 访问WEB-INF下的文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置SpringMVC包扫描-->
<context:component-scan base-package="com.lin.controller"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--静态资源放行-->
<mvc:resources mapping="/static/**" location="/static/"/>
</beans>
最后,在controller包下新建一个页面跳转的controller就可以了
配置完后又出现新问题了
如果我们这样写是走第一个呢还是第二个呢?
demo2--@RequestMapping("/demo2")等于 demo2
如果没有 @RequestMapping("/{page}")
我们发现,
在返回路径的时候还是会走自定义视图解析器,如果我们不希望走自定义视图解析器
就需要书写具体的跳转代码 :forward:/index.jsp redirect:/index.jsp
这样,从前的那一份爽就多了一个forward了............
Balmy:[Spring-SpringMVC-MyBatis] 03- SSM-转发 & 重定向zhuanlan.zhihu.com