- Spring MVC 基础知识
- Spring MVC - 主页
- Spring MVC - 概述
- Spring MVC - 环境设置
- Spring MVC - Hello World 示例
- Spring MVC - 表单处理
- Spring MVC - 表单处理
- Spring MVC - 页面重定向
- Spring MVC - 静态页面
- Spring MVC - 表单标签库
- Spring MVC - 文本框
- Spring MVC - 密码
- Spring MVC - 文本区域
- Spring MVC - 复选框
- Spring MVC - 复选框
- Spring MVC - 单选按钮
- Spring MVC - 单选按钮
- Spring MVC - 下拉菜单
- Spring MVC - 列表框
- Spring MVC - 隐藏
- Spring MVC - 错误
- Spring MVC - 上传
- Spring MVC - 处理程序映射
- Bean 名称 Url 处理程序映射
- 控制器类名处理程序映射
- 简单的 URL 处理程序映射
- Spring MVC - 控制器
- Spring MVC - 多动作控制器
- 属性方法名称解析器
- 参数 方法名称 解析器
- 可参数化视图控制器
- Spring MVC - 视图解析器
- 内部资源视图解析器
- Spring MVC - Xml 视图解析器
- 资源包视图解析器
- 多解析器映射
- Spring MVC - 集成
- Spring MVC - Hibernate 验证器
- Spring MVC - 生成 RSS 提要
- Spring MVC - 生成 XML
- Spring MVC - 生成 JSON
- Spring MVC - 生成 Excel
- Spring MVC - 生成 PDF
- Spring MVC - 使用 log4j
- 春季问答
- 春天 - 问题与解答
Spring MVC - Xml 视图解析器示例
XmlViewResolver 用于使用 xml 文件中定义的视图 bean 来解析视图名称。以下示例展示了如何使用 Spring Web MVC 框架使用 XmlViewResolver。
TestWeb-servlet.xml
<bean class = "org.springframework.web.servlet.view.XmlViewResolver">
<property name = "location">
<value>/WEB-INF/views.xml</value>
</property>
</bean>
视图.xml
<bean id = "hello" class = "org.springframework.web.servlet.view.JstlView"> <property name = "url" value = "/WEB-INF/jsp/hello.jsp" /> </bean>
例如,使用上面的配置,如果 URI -
/hello 被请求时,DispatcherServlet 会将请求转发到 view.xml 中 bean hello 定义的 hello.jsp。
首先,让我们准备好一个可用的 Eclipse IDE,并按照以下步骤使用 Spring Web 框架开发基于动态表单的 Web 应用程序。
| 步 | 描述 |
|---|---|
| 1 | 按照 Spring MVC - Hello World 章节中的说明,在 com.tutorialspoint 包下创建一个名为 TestWeb 的项目。 |
| 2 | 在 com.tutorialspointpackage 下创建一个 Java 类 HelloController。 |
| 3 | 在jsp子文件夹下创建一个视图文件hello.jsp。 |
| 4 | 下载 JSTL 库jstl.jar。将其放入您的 CLASSPATH 中。 |
| 5 | 最后一步是创建源文件和配置文件的内容并导出应用程序,如下所述。 |
HelloController.java
package com.tutorialspoint;
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", "Hello Spring MVC Framework!");
return "hello";
}
}
TestWeb-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.tutorialspoint" />
<bean class = "org.springframework.web.servlet.view.XmlViewResolver">
<property name = "location">
<value>/WEB-INF/views.xml</value>
</property>
</bean>
</beans>
视图.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">
<bean id = "hello"
class = "org.springframework.web.servlet.view.JstlView">
<property name = "url" value = "/WEB-INF/jsp/hello.jsp" />
</bean>
</beans>
你好.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
创建源文件和配置文件后,导出您的应用程序。右键单击您的应用程序,使用Export → WAR File选项并将HelloWeb.war文件保存在 Tomcat 的 webapps 文件夹中。
现在,启动 Tomcat 服务器并确保您能够使用标准浏览器从 webapps 文件夹访问其他网页。尝试访问 URL - http://localhost:8080/HelloWeb/hello,如果 Spring Web 应用程序一切正常,我们将看到以下屏幕。
