关于重定向和访问WEB-INF目录的问题

项目中有使用重定向访问,尝试用重定向访问不同目录下的.jsp文件
在这里插入图片描述

重定向访问webapp下的不同jsp文件如下:
1、webapp/redirect.jsp的写法:

	//方法1:使用response重定向
	response.sendRedirect(request.getContextPath()+"/redirect.jsp");
	//方法2:配置视图解析器重定向,视图解析器代码在最后
	return "redirect:/redirect.jsp";

2、webapp/test/testOne.jsp的写法:

	//方法1:使用response重定向
	response.sendRedirect(request.getContextPath()+"/test/testOne.jsp");
	//方法2:配置视图解析器重定向,视图解析器代码在最后
	return "redirect:/test/testOne.jsp";

3、webapp/WEB-INF/pages/success.jsp的写法:

在尝试很多次路径的写法后,发现webapp/WEB-INF下的文件访问不了,会报404错误:
Description:The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

那我浏览器直接访问呢?同样404,同样的Description。

发现是被服务器保护了,WEB-INF是Java的WEB应用的安全目录。也就是客户端无法访问,只有服务端可以源访问的目录。
重定向是两次不同请求,而且是在客户端完成的。

解决方法:

客户端想要访问WEB-INF下的文件,需要配置web.xml

    <servlet>
          <servlet-name>success</servlet-name>
          <jsp-file>/WEB-INF/pages/success.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>success</servlet-name>
        <url-pattern>/success</url-pattern>
    </servlet-mapping>

在浏览器输入http://localhost:8080/success 直接回车,可以访问到,
所以,在配置好web.xml之后,就可以重定向到WEB-INF下的文件了,
重定向的写法:

	//方法1:使用response重定向
	response.sendRedirect(request.getContextPath()+"/success");
	//方法2:配置视图解析器重定向,视图解析器代码在最后
	return "redirect:/success";

成功进入。


----------------------------------------分割线---------------------------------------

方法2中的视图解析器代码:
springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="cn.tds"></context:component-scan>

    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:annotation-driven></mvc:annotation-driven>
</beans>
  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值