SpringMVC:视图控制器

为了防止用户通过地址栏直接访问jsp页面,通过把jsp页面放到WEB-INF下面,进行保护

 

 

 

View5Controller:

package com.baizhiedu;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/view5")
public class View5Controller {
    @RequestMapping("/view1")
    public ModelAndView view1(){
        ModelAndView modelAndView=new ModelAndView();

        ModelMap modelMap=modelAndView.getModelMap();
        modelMap.addAttribute("name","xiaohei");
        //跳转
        modelAndView.setViewName("result4");

        return modelAndView;
    }
}

 

同时需要修改dispatcher.xml:视图解析器的路径

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--设置注解扫描的路径 -->
    <context:component-scan base-package="com.baizhiedu"/>
    <!--引入springMVC的核心功能-->

    <mvc:annotation-driven />

    <!--视图解析器配置:    控制器方法返回的结果的前缀和后缀-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

这样就不能直接访问result4.jsp页面了,直接访问的话会报错 

 

 

 

 

 

通过视图保护之后,注册按钮的连接路径就不能以reg.jsp结尾了,需要写一个空的控制器,进行跳转,

这样如果需要写很多的空的控制器的话,就比较麻烦,所以通过在配置文件中配置视图控制器就可以访问受保护的视图

通过在dispatcher.xml:进行试图控制器的配置:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--设置注解扫描的路径 -->
    <context:component-scan base-package="com.baizhiedu"/>
    <!--引入springMVC的核心功能-->

    <mvc:annotation-driven />

    <!--视图解析器配置:    控制器方法返回的结果的前缀和后缀-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


<!--    进行视图控制器的配置,view-name="result4"它在通过视图解析器进行跳转-->
    <mvc:view-controller path="/result4" view-name="result4"/>
</beans>

配置视图解析器后就可以访问了,不用在写空的配置器进行跳转 ,下面可以访问result4.jsp,没有输出内容,因为直接访问,它的作用域并没有提供内容

 通过日志也可以观察跳转到result.jsp页面:

 

 

 

 

 

 View6Controller:

package com.baizhiedu;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/view6")
public class View6Controller {
    @RequestMapping("view1")
    public String view1(){
        System.out.println("View6Controller.view1");
        return "redirect:/WEB-INF/jsp/result5.jsp";//Redirect会把路径添加到地址栏,进行访问
    }
}

result5.jsp:

<%--
  Created by IntelliJ IDEA.
  User: DELL
  Date: 2022/6/6
  Time: 17:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <base href="<%=basePath%>">
</head>
<body>
  <h1>this is result5 jsp</h1>
</body>
</html>

通过地址栏访问WEB-INF的内容,是不可以的,因为我们已经进行了保护 

 

 修改View6Controller:

package com.baizhiedu;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/view6")
public class View6Controller {
    @RequestMapping("view1")
    public String view1(){
        System.out.println("View6Controller.view1");
        return "redirect:/result5";//Redirect会把路径添加到地址栏,进行访问
    }
}

配置dispatcher.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--设置注解扫描的路径 -->
    <context:component-scan base-package="com.baizhiedu"/>
    <!--引入springMVC的核心功能-->

    <mvc:annotation-driven />

    <!--视图解析器配置:    控制器方法返回的结果的前缀和后缀-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>


<!--    进行视图控制器的配置,view-name="result4"它在通过视图解析器进行跳转-->
    <mvc:view-controller path="/result4" view-name="result4"/>

    <mvc:view-controller path="/result5" view-name="result5"/>
</beans>

在地址栏输入:http://localhost:8888/view/view6/view1  发现地址栏发生了改变,说明这是一个Redirect操作 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值