Spring MVC内部资源视图解析器
2023-09-14 09:12:01 时间
InternalResourceViewResolver
用于将提供的URI解析为实际URI。下面的示例演示如何在Spring Web MVC框架中使用SpringResultViewResolver
。 InternalResourceViewResolver
允许映射网页与请求。
所下所示配置 -
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Spring MVC Framework 映射网页与请求示例!");
return "hello";
}
}
URL映射配置文件如下 -
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
例如,使用上面的配置,如果URI
:
- 对于
/hello
请求,DispatcherServlet
会将请求转发到前缀+ view-name + suffix = /WEB-INF/jsp/hello.jsp
。
首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:
- 创建一个名称为 InternalResourceViewResolver 的动态WEB项目。
- 在
com.yiibai.springmvc
包下创建一个Java类HelloController
。 - 在
jsp
子文件夹下创建一个视图文件:hello.jsp
。 - 最后一步是创建所有源和配置文件的内容并运行应用程序,详细如下所述。
完整的项目文件目录结构如下所示 -
HelloController.java 的代码如下所示 -
package com.yiibai.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Spring MVC Framework 映射网页与请求示例!");
return "hello";
}
}
InternalResourceViewResolver-servlet.xml 的代码如下所示 -
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.yiibai.springmvc" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
hello.jsp 的代码如下所示 -
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
完成创建源和配置文件后,发布应用程序到Tomcat服务器。
现在启动Tomcat服务器,当访问URL => http://localhost:8080/InternalResourceViewResolver/hello , 如果Spring Web应用程序没有问题,应该看到以下结果:
相关文章
- Spring学习笔记(五)——JdbcTemplate和spring中声明式事务
- 简单介绍一下spring bean的生命周期_Spring bean的生命周期
- Spring Boot 静态资源处理
- spring cloud之 hello world和eurake介绍及eurake使用
- Spring batch教程 之 spring batch简介
- batch spring 重复执行_Spring Batch批处理
- Spring Security 自定义资源服务器实践
- Spring MVC框架:第五章:静态资源访问与mvc:view-controller
- 解释spring框架中bean的生命周期_Spring bean的生命周期
- spring boot整合shiro_Spring框架介绍及使用
- Spring循环依赖-spring源码详解(四)
- Spring Boot 中文参考指南(二)-Web
- Spring Cloud Bus的基本概念和用途
- Spring Cloud Bus使用自定义的消息转换器(一)
- spring部分详解编程语言
- spring-Formatter(格式化器)-validator(验证器)-错误信息定制详解编程语言
- spring MVC处理静态资源详解编程语言
- Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源详解编程语言
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
- spring使用Spring整合Redis和Jedis构建高性能应用(redisjedis与)
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase
- 读取spring配置文件的方法(spring读取资源文件)