0
<form id="submitform" action="">
<button id="demo" onclick="saveObject()">Click me to save.</button>
    <input type="hidden" id="jsonstring" name="jsonstring"/> 
</form>
<script>
    function saveObject(){
    var JsonString = JSON.stringify(jsonObject);
    document.getElementById('jsonstring').value = JsonString; 
    document.getElementById('submitform').action = "saveAction"; 
    document.getElementById('submitform').submit();
    }

I would like to pass my JsonString as String to the struts2 action classes by clicking on a button, this string is created dynamically inside saveObject() function. Now my question is, for the short string, I can send with the form, but when the String is very long, errors comes... Failed to load resource: the server responded with a status of 400 (Bad Request)

Actually, I don't want my string shown in the url by the form submit as well. Does anyone have any solution in my case? Thank you very much in advanced!!!

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • use POST method instead of GET. POST don't really have limits (well, there is but your are safe in most case) – AxelH Dec 01 '16 at 10:31
  • *"How could I passing javascript variable(not by submit in form) from jsp to struts2 action?"* You can't. JSP code (on the server) doesn't have access to JavaScript variables (on the client). *(I'm assuming here you're not running Rhino or Nashorn code in your JSP.)* Presumably you're asking how to send information from the client *through* JSP to Struts2? – T.J. Crowder Dec 01 '16 at 10:31
  • [Ajax comes to mind](https://struts.apache.org/docs/ajax.html) – mplungjan Dec 01 '16 at 10:32
  • thanks guys~ it works now with adding post~ – user1156202 Dec 01 '16 at 10:43
  • it works with small amount of data, however, the glassfish server returns post too large error to me. I have change the post max size as the post http://dgielis.blogspot.de/2014/01/increase-post-back-size-in-glassfish.html It doesn't work for me~ I would like to know if there is any other way to send jsp object to struts action class, or share variable between two jsp files since my goal is to change the values dynamically in a edit.jsp show it in the original jsp. Thank you very much! – user1156202 Dec 01 '16 at 12:19

2 Answers2

1

Add the attribute method=POST to your form

<form id="submitform" action="" method="POST">

However, your struts2 action should be able to support POST requests too

EDIT ( not using form submit )

Use XHRs

HTML
<form action="saveAction" method="POST" onsubmit="AJAXSubmit(this); return false;">
JS
function AJAXSubmit (oFormElement) {
   var oReq = new XMLHttpRequest();
   oReq.onload = ajaxSuccess;
   oReq.open("post", oFormElement.action);
   oReq.send(new FormData(oFormElement));
}

function ajaxSuccess () {
    console.log(this.responseText);
}

This should provide you with the right context.

jkris
  • 5,851
  • 1
  • 22
  • 30
  • It works!!!Thank you so much!!! – user1156202 Dec 01 '16 at 10:37
  • it works with small amount of data, however, the glassfish server returns post too large error to me. I have change the post max size as the post http://dgielis.blogspot.de/2014/01/increase-post-back-size-in-glassfish.html It doesn't work for me~ I would like to know if there is any other way to send jsp object to struts action class, or share variable between two jsp files since my goal is to change the values dynamically in a edit.jsp show it in the original jsp. Thank you very much! – user1156202 Dec 01 '16 at 12:19
0

The error 400 Bad Request you are getting might be due to large URL which is being formed due to GET method of your form submit.

You can change it to POST by adding an attribute method="POST" to your HTML form. for example:

<form id="formId" action="anyAction.do" method="POST"></form>

In your case you can change it to:

<form id="submitform" action="" method="POST">
  <button id="demo" onclick="saveObject()">Click me to save.</button>
  <input type="hidden" id="jsonstring" name="jsonstring"/> 
</form>

By changing to POST, your data will not be shown in the URL, instead it would be sent in the body of the request.

Gaurav Agarwal
  • 173
  • 1
  • 10
  • it works with small string, since my string is too long it returns post too large~ thanks any way, I am trying with ajax now~ – user1156202 Dec 01 '16 at 11:09