📜  Spring MVC-页面重定向示例

📅  最后修改于: 2020-11-11 06:19:41             🧑  作者: Mango


下面的示例演示如何编写一个简单的基于Web的应用程序,该应用程序利用重定向将http请求传输到另一个页面。首先,让我们拥有一个可用的Eclipse IDE,并考虑以下步骤以使用Spring Web Framework开发基于动态表单的Web应用程序-

Step Description
1 Create a project with a name HelloWeb under a package com.tutorialspoint as explained in the Spring MVC – Hello World chapter.
2 Create a Java class WebController under the com.tutorialspoint package.
3 Create view files index.jsp, final.jsp under jsp sub-folder.
4 The final step is to create the content of the source and configuration files and export the application as explained below.

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

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

index.jsp

Spring Page Redirection
   
   
      

Spring Page Redirection

Click below button to redirect the result to new page

final.jsp

Spring Page Redirection
   
   
   
      

Redirected Page

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

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

春季重定向表

现在,单击“重定向页面”按钮以提交表单并进入最终的重定向页面。如果我们的Spring Web Application一切正常,我们应该看到以下屏幕:

Spring重定向表单结果