以下示例演示如何使用Spring MVC Framework编写基于Web的简单应用程序,该框架可以借助 标记访问静态页面和动态页面。
首先,让我们使用一个可用的Eclipse IDE,并遵循以下步骤,使用Spring Web Framework开发基于动态表单的Web应用程序。
序号
描述
1
在Spring MVC - Hello World章节中解释,在com.codingdict包下创建一个名为HelloWeb的项目。
2
在com.codingdict包下创建一个Java类WebController。
3
在jsp子文件夹下创建一个静态文件final.htm。
4
更新WebContent / WEB-INF文件夹下的Spring配置文件HelloWeb-servlet.xml,如下所示。
5
最后一步是创建源文件和配置文件的内容并导出应用程序,如下所述。
WebController.java
package com.codingdict;
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
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">
这里, 标签用于映射静态页面。mapping属性必须是 Ant 模式
,用于指定http请求的URL模式。location属性必须指定一个或多个有效资源目录位置,其中包含静态页面,包括图像,样式表,JavaScript和其他静态内容。可以使用逗号分隔的值列表指定多个资源位置。
以下是Spring视图文件 WEB-INF / jsp / index.jsp的内容 。这将是一个登陆页面; 此页面将发送访问
staticPage服务方法 的请求,该 方法 将此请求重定向到WEB-INF / pages文件夹中可用的静态页面。
index.jsp
Spring Landing PageSpring Landing Pag
Click below button to get a simple HTML page
final.htm
Spring Static PageA simple HTML page
完成创建源文件和配置文件后,导出应用程序。右键单击您的应用程序,使用Export→WAR
File选项并将您的HelloWeb.war文件保存在Tomcat的webapps文件夹中。
现在,启动Tomcat服务器并确保您可以使用标准浏览器从webapps文件夹访问其他网页。现在尝试访问URL - http://
localhost:8080 / HelloWeb / index。如果Spring Web Application的一切正常,我们将看到以下屏幕。
单击“获取HTML页面”按钮以访问staticPage服务方法中提到的静态页面。如果您的Spring Web应用程序一切正常,我们将看到以下屏幕。