下面的例子展示了如何使用Spring MVC框架,它可以访问静态网页与动态网页与标记的帮助下写了一个简单的基于Web的应用程序。要开始使用它,我们使用Eclipse IDE,并按照以下步骤使用Spring Web框架开发动态表单的Web应用程序:
步骤
描述
1
Create a Dynamic Web Project with a name HelloWeb and create a packagecom.yiibai under the src folder in the created project.
2
Drag and drop below mentioned Spring and other libraries into the folder WebContent/WEB-INF/lib.
3
Create a Java class WebController under the com.yiibai package.
4
Create Spring configuration files Web.xml and HelloWeb-servlet.xml under theWebContent/WEB-INF folder.
5
Create a sub-folder with a name jsp under the WebContent/WEB-INF folder. Create a view fileindex.jsp under this sub-folder.
6
Create a sub-folder with a name pages under the WebContent/WEB-INF folder. Create a static file final.html under this sub-folder.
7
The final step is to create the content of all the source and configuration files and export the application as explained below.
这里是WebController.java文件的内容:
packagecom.yiibai;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;@ControllerpublicclassWebController{@RequestMapping(value="/index",method=RequestMethod.GET)publicStringindex(){return"index";}@RequestMapping(value="/staticPage",method=RequestMethod.GET)publicStringredirect(){return"redirect:/pages/final.html";}}
以下是Spring Web配置文件web.xml 的内容
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">Spring Page RedirectionHelloWeborg.springframework.web.servlet.DispatcherServlet1HelloWeb/
下面是另一个Spring Web配置文件的HelloWeb-servlet.xml 的内容
<?xml version="1.0"encoding="UTF-8"?>
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">
这里标签被用来映射静态页面。映射属性必须是一个Ant模式,指定URL的HTTP请求的模式。位置属性必须指定具有静态页面,包括图片,样式表,JavaScript和其他静态内容的一个或多个有效的资源目录位置。多个资源位置可以使用逗号分隔的值的列表来指定。
以下是Spring视图文件WEB-INF/jsp/index.jsp的内容。这将是一个登陆页面,此页面会发送一个请求来访问staticPage服务方法,将这个请求重定向到WEB-INF/pages文件夹中提供一个静态页面。
Spring Landing PageSpring Landing Pag
Click below button to get a simple HTML page
以下是Spring视图文件WEB-INF/pages/final.html 的内容。
Spring Static PageA simple HTML page
最后,下面是Spring和其他库包含在您的Web应用程序的列表。你只需将这些文件拖放它们到 WebContent/ WEB-INF/ lib文件夹中。
commons-logging-x.y.z.jar
org.springframework.asm-x.y.z.jar
org.springframework.beans-x.y.z.jar
org.springframework.context-x.y.z.jar
org.springframework.core-x.y.z.jar
org.springframework.expression-x.y.z.jar
org.springframework.web.servlet-x.y.z.jar
org.springframework.web-x.y.z.jar
spring-web.jar
创建源代码和配置文件完成后,导出您的应用程序。右键单击应用程序和使用Export> WAR文件选项并保存HelloWeb.war文件到Tomcat 的webapps文件夹中。
现在启动Tomcat服务器,并确保您能够访问来自的webapps文件夹,使用标准的浏览器。现在尝试访问该URL http://localhost:8080/HelloWeb/index。如果一切顺利,你应该看到以下结果:
点击“Get HTML Page”按钮来访问staticPage服务方法提到一个静态页面。如果一切顺利应该看到以下结果:
¥ 我要打赏
纠错/补充
收藏
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。