当前位置: 首页 > 文档资料 > Spring 中文教程 >

Spring Page Redirection Example

优质
小牛编辑
132浏览
2023-12-01

以下示例显示如何编写一个简单的基于Web的应用程序,该应用程序使用redirect将http请求传输到另一个页面。 首先,让我们使用一个有效的Eclipse IDE,并采用以下步骤使用Spring Web Framework开发动态Formbased Web应用程序 -

描述
1使用名称HelloWeb创建一个Dynamic Web Project ,并在创建的项目中的src文件夹下创建一个包cn.xnip
2将下面提到的Spring和其他库拖放到WebContent/WEB-INF/lib文件夹中。
3cn.xnip包下创建一个Java类WebController
4WebContent/WEB-INF文件夹下创建Spring配置文件Web.xmlHelloWeb-servlet.xml
5WebContent/WEB-INF文件夹下创建名为jsp的子文件夹。 在此子文件夹下创建视图文件index.jspfinal.jsp
6最后一步是创建所有源文件和配置文件的内容,并导出应用程序,如下所述。

这是WebController.java文件的内容

package cn.xnip;
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 = "/redirect", method = RequestMethod.GET)
   public String redirect() {
      return "redirect:finalPage";
   }
   @RequestMapping(value = "/finalPage", method = RequestMethod.GET)
   public String finalPage() {
      return "final";
   }
}

以下是Spring Web配置文件web.xml

<web-app id = "WebApp_ID" version = "2.4"
   xmlns = "http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>Spring Page Redirection</display-name>
   <servlet>
      <servlet-name>HelloWeb</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

以下是另一个Spring Web配置文件HelloWeb-servlet.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<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 = "cn.xnip" />
   <bean id = "viewResolver" 
      class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value=".jsp" />
   </bean>
</beans>

以下是Spring视图文件index.jsp的内容。 这将是一个登陆页面,此页面将发送访问redirect服务方法的请求,该方法将此请求重定向到另一个服务方法,最后将final.jsp页面。

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
   <head>
      <title>Spring Page Redirection</title>
   </head>
   <body>
      <h2>Spring Page Redirection</h2>
      <p>Click below button to redirect the result to new page</p>
      <form:form method = "GET" action = "/HelloWeb/redirect">
         <table>
            <tr>
               <td>
                  <input type = "submit" value = "Redirect Page"/>
               </td>
            </tr>
         </table>  
      </form:form>
   </body>
</html>

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

<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
   <head>
      <title>Spring Page Redirection</title>
   </head>
   <body>
      <h2>Redirected Page</h2>
   </body>
</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选项并将您的HelloWeb.war文件保存在Tomcat的webapps文件夹中。

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

Spring重定向表单

单击“重定向页面”按钮以提交表单并获取最终的重定向页面。 如果Spring Web Application的一切正常,您应该看到以下结果。

Spring重定向表单结果