说明:示例基于Spring MVC 4.1.6。

以下示例显示了如何使用Spring MVC Framework编写一个简单的基于Web的应用程序,该MVC框架可以在<mvc:resources>标签的帮助下访问静态页面以及动态页面。首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序:

步骤

描述

1

创建一个名为HelloWeb的项目,在一个包com.tutorialspoint下,如Spring MVC - Hello World Example章节所述。

2

在com.tutorialspoint包下创建一个Java类WebController。

3

在jsp子文件夹下创建一个静态文件final.htm。

4

如下所述,在WebContent/WEB-INF文件夹下更新Spring配置文件HelloWeb-servlet.xml。

4

最后一步是创建所有源和配置文件的内容并导出应用程序,如下所述。

WebController.java



package com.tutorialspoint;

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

@Controller
public class WebController {

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}

@RequestMapping(value = "/staticPage", method = RequestMethod.GET)
public String redirect() {
return "redirect:/pages/final.htm";
}
}


HelloWeb-servlet.xml



<?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-2.5.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-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 id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
</bean>

<mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
<mvc:annotation-driven/>

</beans>


这里使用<mvc:resources .... />标签来映射静态页面。映射属性必须是一个Ant图案指定一个HTTP请求的URL模式。该位置属性必须指定为静态页面,包括图片,样式表,JavaScript和其它静态内容的一个或多个有效的资源目录位置。可以使用逗号分隔的值列表来指定多个资源位置。

以下是Spring视图文件WEB-INF/jsp/index.jsp的内容。这将是一个登陆页面,此页面将发送访问staticPage服务方法的请求,该方法将将该请求重定向到WEB-INF/pages文件夹中可用的静态页面。

index.jsp



<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<title>Spring Landing Page</title>
</head>
<body>
<h2>Spring Landing Pag</h2>
<p>Click below button to get a simple HTML page</p>
<form:form method="GET" action="/HelloWeb/staticPage">
<table>
<tr>
<td>
<input type="submit" value="Get HTML Page"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>


final.htm



<html>
<head>
<title>Spring Static Page</title>
</head>
<body>

<h2>A simple HTML page</h2>

</body>
</html>


完成创建源和配置文件后,导出应用程序。右键单击应用程序并使用Export->WAR File选项,并将您的HelloWeb.war文件保存在Tomcat的webapps文件夹中。

现在启动您的Tomcat服务器,并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试访问URL http://localhost:8080/HelloWeb/index。如果您的Spring Web应用程序一切正常,您应该看到以下结果:

Spring MVC-静态页面示例(转载实践)_spring

点击“获取HTML页面”按钮访问staticPage服务方法中提到的静态页面。如果您的Spring Web应用程序一切正常,您应该看到以下结果:

Spring MVC-静态页面示例(转载实践)_java_02

Maven示例:

​https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test3​