2

我正在尝试找到使用 POST 请求的解决方法,因为我无法使用表单、传递参数并重定向到另一个页面而不在 URL 中显示参数。我听说可以通过 XHR GET 请求来做到这一点,但我不知道怎么做。我也用 POST 尝试过。

Index.jsp(Peter 是我通过表单进行 POST 的示例,它有效,但我不能使用。George 是我的 GET 示例。我可以看到函数 HttpGet 被调用,responseText 是 helloworld.jsp 的 html。Sam 是我的 POST 示例。我可以看到参数已传递给控制器​​并添加到 mv,但它没有重定向到 helloworld.jsp。)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
<script type="text/javascript">
function httpGet(theUrl)
{
    var xmlHttp = null;
    alert('httpGet is called');
    alert(theUrl);
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false );
    xmlHttp.send( null );
    alert(xmlHttp.responseText);
    return xmlHttp.responseText;
}

function doPost() {
    var url = "http://localhost:8080/HelloWorld/hello";
    var params = "name=Sam";
    alert('Entered Sam');
    var xhr = new XMLHttpRequest();
    xhr.open("POST", url, true);
    //Send the proper header information along with the request
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(params);
}
</script>
</head>
<body>

<h2>Hello World</h2>

<form action="hello" id="test" method="POST" style="display:none;">
    <input type="hidden" name="name" value="Eric" />
    <input type="hidden" name="lastname" value="Peters" />
    <button>Go to user 123</button>
</form>

<a href="javascript:;" onclick="javascript:
document.getElementById('test').submit()">Eric Peters</a><br/>

<a href="javascript:;" onclick="httpGet('hello?name=George')">George</a>

<a href="javascript:;" onclick="doPost()">Sam</a>
</body>
</html>

控制器:

package com.programcreek.helloworld.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {
    String message = "Welcome to Spring MVC!";

    @RequestMapping("/hello")
    public ModelAndView showMessage(
            @RequestParam(value = "name", required = true) String name, 
            @RequestParam(value = "lastname", required = false, defaultValue = "Doe") String lastname) {
        System.out.println("in controller");

        ModelAndView mv = new ModelAndView("helloworld");
        mv.addObject("message", message);
        mv.addObject("name", name);
        mv.addObject("lastname", lastname);

        return mv;
    }
}

Helloworld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
    <center>
        <h2>Hello World</h2>
        <h2>
            ${message} ${name} ${lastname}
        </h2>
    </center>
</body>
</html>

我的问题是:1)是否可以使用 XHR 获取或发布而不在 URL 中显示参数?2) 哪个方法(george 或 sam)接近(如果有),我应该查看哪些文件以使其重定向到 helloworld.jsp(控制器或 index.jsp)?谢谢您的帮助。

4

0 回答 0