- 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 Feed
- Spring MVC - 生成XML
- Spring MVC - 生成JSON
- Spring MVC - 生成Excel
- Spring MVC - 生成PDF
- Spring MVC - 使用log4j
- Spring 问题与解答
- Spring - 问题与解答
- Spring 有用资源
- Spring MVC - 快速指南
- Spring MVC - 有用资源
- Spring MVC - 讨论
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>
views.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 | 创建一个名为TestWeb的项目,位于com.tutorialspoint包下,如Spring MVC - Hello World章节中所述。 |
| 2 | 在com.tutorialspoint包下创建一个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>
views.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>
hello.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
创建完源文件和配置文件后,导出你的应用程序。右键单击你的应用程序,使用**导出→WAR文件**选项,并将**HelloWeb.war**文件保存到Tomcat的webapps文件夹中。
现在,启动你的Tomcat服务器,并确保你可以使用标准浏览器访问webapps文件夹中的其他网页。尝试访问URL - **https://:8080/HelloWeb/hello**,如果Spring Web应用程序一切正常,我们将看到以下屏幕。
广告