课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring 页面重定向例子

当前位置:免费教程 » Java相关 » Spring

下面的例子说明了如何编写一个简单的基于 web 的应用程序,它利用重定向来传送一个 http 请求到另一个页面中。为了开始使用它,让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤使用 Spring 的 Web 框架来开发一个动态的基于表单的 Web 应用程序:

步骤 描述
1 创建一个名称为 HelloWeb动态 Web 项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint
2 将上面提到的 Spring 和其他库拖拽到文件夹 WebContent/WEB-INF/lib 中。
3 com.tutorialspoint 包下创建一个 Java 类 WebController
4 WebContent/WEB-INF 文件夹下创建 Spring 的配置文件 Web.xmlHelloWeb-servlet.xml
5 WebContent/WEB-INF 文件夹下创建名称为 jsp 的子文件夹。在这个子文件夹下创建视图文件 index.jspfinal.jsp
6 最后一步是创建所有的源代码和配置文件的内容,并导出该应用程序,正如下面解释的一样。

这里是 WebController.java 文件的内容:

  1. package com.tutorialspoint;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. @Controller
  6. public class WebController {
  7. @RequestMapping(value = "/index", method = RequestMethod.GET)
  8. public String index() {
  9. return "index";
  10. }
  11. @RequestMapping(value = "/redirect", method = RequestMethod.GET)
  12. public String redirect() {
  13. return "redirect:finalPage";
  14. }
  15. @RequestMapping(value = "/finalPage", method = RequestMethod.GET)
  16. public String finalPage() {
  17. return "final";
  18. }
  19. }

下面是 Spring Web 配置文件 web.xml 的内容

  1. <web-app id="WebApp_ID" version="2.4"
  2. xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6.  
  7. <display-name>Spring Page Redirection</display-name>
  8.  
  9. <servlet>
  10. <servlet-name>HelloWeb</servlet-name>
  11. <servlet-class>
  12. org.springframework.web.servlet.DispatcherServlet
  13. </servlet-class>
  14. <load-on-startup>1</load-on-startup>
  15. </servlet>
  16.  
  17. <servlet-mapping>
  18. <servlet-name>HelloWeb</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21.  
  22. </web-app>

下面是另一个 Spring Web 配置文件 HelloWeb-servlet.xml 的内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <context:component-scan base-package="com.tutorialspoint" />
  12.  
  13. <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <property name="prefix" value="/WEB-INF/jsp/" />
  15. <property name="suffix" value=".jsp" />
  16. </bean>
  17. </beans>

下面是 Spring 视图文件 index.jsp 文件的内容。这将是一个登陆页面,这个页面将发送一个请求来访问重定向 service 方法,该方法将把这个请求重定向到另一个 service 方法中,最后将显示 final.jsp 页面。

  1. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
  2. <html>
  3. <head>
  4. <title>Spring Page Redirection</title>
  5. </head>
  6. <body>
  7. <h2>Spring Page Redirection</h2>
  8. <p>Click below button to redirect the result to new page</p>
  9. <form:form method="GET" action="/HelloWeb/redirect">
  10. <table>
  11. <tr>
  12. <td>
  13. <input type="submit" value="Redirect Page"/>
  14. </td>
  15. </tr>
  16. </table>
  17. </form:form>
  18. </body>
  19. </html>

下面是 Spring 视图文件 final.jsp 的内容。这是最终的重定向页面。

  1. <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
  2. <html>
  3. <head>
  4. <title>Spring Page Redirection</title>
  5. </head>
  6. <body>
  7.  
  8. <h2>Redirected Page</h2>
  9.  
  10. </body>
  11. </html>

最后,下面是包含在你的 web 应用程序中的 Spring 和其他库的列表。你仅仅需要将这些文件拖拽到 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 File 选项,并且在 Tomcat 的 webapps 文件夹中保存你的 HelloWeb.war 文件。

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

现在单击 “Redirect Page” 按钮来提交表单,并且得到最终的重定向页面。如果你的 Spring Web 应用程序一切都正常,你应该看到下面的结果:

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号