Spring controller - send html as response -
I have a jsp page loaded with products and add links against it. I am trying to show the cart in a div in the same page. I want to send html as feedback. This is what I have done. This is just the string & lt; Div & gt; Output & lt; / Div & gt;
returns. Someone can show me how I should do it.
controller
@RequestMapping (value = "/ addtocart {id}"), produces = "text / plain; charset = UTF -8 ") @ Risponsbodi public string Adtoart (@ Path variable (" ID ") Int ID, @ model attribute (" carriage ") train carts, models) {product product = Utpadsewa. ProductivityIID; If (product! = Null) {cartline line = new cartline (); Line.setProduct (product); Line.setQuantity (1); ProductService.updateProduct (product); } Return " Output "; } JSP
& lt; Td> & Lt; An id = "demo4" href = "addtocart $ {product. Id}" & gt; Add to Cart & lt; / A & gt; & Lt; / TD & gt; Click $ ( '# Demo4') to (function () {$. Ajax ({url: '/ addtocart {id}', datatype 'Jason', content type "text / html", type: 'GET' Data: {Id: id}, success: function (response) {$ ('# output'). Html (response);}})}}; & Lt; Div id = "output" style = "display: none" & gt; & Lt; H2 & gt; Cart content (s): & lt; / H2 & gt; & Lt; / Div & gt;
I use the scene as well as the side of a different page on the AJAX call I am However, whatever you are asking for, just to produce your production = "text / plain; described = UTF-8" output = "text There are many other aspects that appear to be incorrect, not related to Spring MVC, so even with the product You still have to make some corrections to get what you are expecting -
I think you have an AJAX L'are not sent. You are completely redirecting to a full browser. When you read the first time, I was confused as "text / plain" versus "text / html" make the difference in an Ajax response, but now I believe that you directed the re-through the browser it and id = "addtoocart $ {product.id}" & gt; Add to Cart & lt; / A & gt;
Change it like this & lt; An id = "demo 4" href = "#" & gt; Add to Cart & lt; / A & gt;
and add return
at the end of your function. This function will execute and return will ensure that the link has not been followed
-
When you do this, you will see some problems. Even with AJAX calls; First, url: '/ addtocart {id}'
should url: '/ addtocart $ {product.id}
-
Capture your reaction in full work, not successfully, and get output as feedback. The response will return the text, the response will return, but the browser will try to parse it as Jason and fail.
-
Your Diviv be invisible, you should add some JS to toggle it
-
Got a Spring MVC, your cart It seems that there is also an asset ID, due to this, your ID path variable should be renamed, else it will be ignored
It will be something like this, if it works Not closer to more than, then it will not be completely
Rename
@RequestMapping (value = "/ addtocart {productId}", output = "text / plain; charset = UTF-8") Public String Adtart (@Path Variable ("Product ID") Ent Product ID, @ Model Attribute ("Cart") Cart Cart, Model Model)
Comments
Post a Comment