0
点赞
收藏
分享

微信扫一扫

SpringMVC-配置JSP视图解析器

香小蕉 2022-04-15 阅读 100
后端java

一、介绍
(1)Tomcat中提供了JSPServlet负责处理JSP文件。
(2)SpringMVC默认有转发视图和重定向视图,我们可以利用转发视图,将JSP文件的视图解析任务交给JSPServlet处理,故配置JSP视图解析器实际上是配置转发视图解析器,同时可以配置视图解析器的前缀和后缀属性来减少冗余代码。

二、项目测试
(1)在/WEB-INF/templates下创建index.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>jsp</h1>
</body>
</html>

在这里插入图片描述

(2)编写SpringMVC-config.xml文件,配置JSP视图解析器

<?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"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.wsh.controller"></context:component-scan>
    <!--配置JSP视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/templates/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--开启注解驱动,否则控制器中带有@RequestMapping的方法失效-->
    <mvc:annotation-driven/>
    <!--指定请求返回对应页面-->
    <mvc:view-controller path="/index" view-name="index"></mvc:view-controller>
</beans>

(3)运行
在这里插入图片描述

举报

相关推荐

0 条评论