SpringMVC学习(6)—— InternalResourceViewResolver(jsp的视图解析器)

之前一直用的是Thymeleaf视图解析器:ThymeleafViewResolver。需要在SpringMVC的核心配置文件配置ThymeleafViewResolver。参考:https://blog.csdn.net/Archer__13/article/details/124260242

InternalResourceViewResolver是jsp的视图解析器,需要将SpringMVC的核心配置文件改为如下

<?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"
       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">

    <!-- 开启组件扫描:如果扫描多个包,多个包之间使用逗号隔开或者直接写这些包共有的上层目录 -->
    <context:component-scan base-package="com.mvc.controller"></context:component-scan>
    
    <!-- 配置JSP视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/templates/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

配置好jsp视图解析器之后,可以使用JSP页面。

package com.mvc.controller;

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

@Controller
public class JspController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping("/testJsp")
    public String goToTest() {
        return "test";
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>index.jsp页面</h1>

    <a href="${pageContext.request.contextPath}/testJsp">访问test.jsp页面</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>test.jsp页面</h1>
</body>
</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值