我们如何在Spring MVC中重定向页面

时间:2019-02-22 08:13:12

标签: html xml spring spring-mvc jsp

ive提出了一个应用程序,但我想做的是。用户选择国家/地区后,会将他们重定向到该国家/地区标题的另一页。    这是我的代码

public class Place {
     @RequestMapping(value="/addcountry")

    public String place(@RequestParam(value="name", required=false, 
             defaultValue="PLACE") String name, Model     model) {
        model.addAttribute("name", name);
        return "form/Place";}

    @RequestMapping(value = "/redirect", method = RequestMethod.GET)
    public String redirect() {
       return "redirect:form/Country";
    }
    @RequestMapping(value = "/finalPage", method = RequestMethod.GET)
    public String finalPage() {
       return "form/Country";
    }
}

JSP:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
  <!DOCTYPE html>
 <html>
  <head>
  <meta charset="UTF-8">
  <form action="some.jsp" class="form">



  <title>choose a country</title>
 </head>
 <body>
 <h1>choose your next destination</h1>
 <!DOCTYPE html>
 <html>
 <head>
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <style>
  * {
  box-sizing: border-box;
 }

 body {
  font: 16px Arial;  
  }


  .autocomplete {
   position: relative;
   display: inline-block;
 }

 input {
   border: 1px solid transparent;
    background-color: #f1f1f1;
   padding: 10px;
   font-size: 16px;
 }

 input[type=text] {
   background-color: #f1f1f1;
   width: 100%;
 }

 input[type=submit] {
   background-color: DodgerBlue;
   color: #fff;
   cursor: pointer;
 }

 .autocomplete-items {
  position: centre;
    border: 1px solid #d4d4d4;
   border-bottom: none;
   border-top: none;
   z-index: 99;

  top: 100%;
   left: 0;
   right: 0;
 }

 .autocomplete-items div {
    padding: 10px;
   cursor: pointer;
   background-color: #fff; 
   border-bottom: 1px solid #d4d4d4; 
 }


 .autocomplete-items div:hover {
   background-color: #e9e9e9; 
 }


 .autocomplete-active {
   background-color: DodgerBlue !important; 
   color: #ffffff; 
 }
 </style>
</head>     
 <body>

 <h2>Travel the world</h2>

 <p>choose a country you'd like to go</p>


 <form:form method = "GET" action = "/HelloWeb/redirect" autocomplete="off"   action="/action_page.php">
   <div class="autocomplete" style="width:300px;">
    <input id="myInput" type="text" name="myCountry" placeholder="Country">
  </div>
  <input type=submit value ="lets go"/>
</form>

这不是完整的Jsp文件
 当用户单击“提交”按钮时,我想重定向到另一个页面。我已经为此创建了一个基本的Js页面,但它仍然不起作用。 自2天以来,我一直在努力,但没有任何进展。我在线使用资源仍然无法正常工作,

1 个答案:

答案 0 :(得分:0)

您可以重定向到控制器映射,而不是重定向到视图模板路径:

@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirect() {
   return "redirect:/finalPage"; // Which is mapped to finalPage()
}

但是,在您的情况下,只需将“ / finalPage”作为简单的表单操作目标即可。为什么在两者之间需要“ /重定向”?