2

I am trying to find a workaround for using POST request, because I cannot use form, pass parameter and redirect to another page without displaying the parameter in the URL. I heard it is possible to do that via XHR GET request but I am not sure how. I have also tried it with POST.

Index.jsp (Peter is my example of POST via form, which works, but I cannot use. George is my example of GET. I can see that the function HttpGet is called and the responseText is the html of helloworld.jsp. Sam is my example of POST. I can see that the parameter is passed to the controller and added to mv but it stops short of redirecting to 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>

Controller:

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>

My questions are: 1) is it possible to use XHR get or post and not show the parameter in the URL? 2) which method (george or sam) is close, if any, and which files should I look at changing to make it redirect to helloworld.jsp (controller or index.jsp)? Thank you for your help.

user1067577
  • 247
  • 1
  • 7
  • 15
  • 1
    what error it displays? – dReAmEr Oct 27 '14 at 08:30
  • @MadProgrammer I don't see any errors. After I click George or Sam, I just stay on the same page with no action taken. In the console, I can see that they both enter the controller. In the debugger, I see that the ModelandView is populated (ex. ModelAndView: reference to view with name 'helloworld'; model is {message=Welcome to Spring MVC!, name=Sam, lastname=Doe}). In the DispatcherServlet.class the method is still recognized as POST and the modelview is still populated. After that I'm not sure where to look.. – user1067577 Oct 27 '14 at 11:59
  • I believe as you are calling through XHR the response is coming to you in the form of responseText, try window.location after getting the response. – Ramesh Kotha Oct 27 '14 at 18:02
  • @Ramesh Kotha When I do that, I do get an error: HTTP Status 400 - Required String parameter 'name' is not present type Status report message Required String parameter 'name' is not present description The request sent by the client was syntactically incorrect. – user1067577 Oct 27 '14 at 22:13
  • But when I make the name not required, it does not get passed to the controller – user1067577 Oct 27 '14 at 22:21

0 Answers0