Write an item today about a merchandise added to the shopping cart function, originally designed to store data after clicking a button, and pop up a div to inform the user that the addition was successful, the page does not jump, seemingly simple function, but stuck at the point of "no jump". I don't want to jump so hard?I checked a lot of data and learned to use ajax, because I was dull and could not solve my small problem, once wanted to give up, thank you very much Xiong Shiwei The impatient guidance from my classmates helped me solve this small problem.
The purpose of this blog is to implement a form submission function that does not jump off without a thorough understanding of ajax (because I don't understand it at all)
1. Import jQuery
This is very simple, add in the jsp page head er
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
2. Write functions that submit forms
Also add in jsp
<script type="text/javascript">
function addcart(){
$.get("insertCart",function(data,status){
document.getElementById("insertCartMessage").style.display="block";
});
return false;
}
</script>
The insertCart above is the name of my action, and the insertCartMessage is the id that displays the div prompt
This should be the case after simplification
<script type="text/javascript">
function ajaxsubmit(){
$.get("actionname",function(data,status){
});
return false;
}
</script>
If you want to use the post method, this is
$.post("actionname", "", function(message, status) {
3. Specify a function for the submit button
This is simple, just click and execute the above function.I'm using label a:
<a href="javascript:addcart();">Join the shopping cart</a>
4. Change action
- Add an InputStream and provide it with getter and setter methods
- Set the value for inputStream in execute(), where it can be set as required, such as "Add Success"/"Add Failure" or something else
package org.action;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import com.opensymphony.xwork2.ActionSupport;
public class InsertCartAction extends ActionSupport{
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String execute() throws Exception {
//System.out.println("adding to cart");
String str = "";
inputStream = new ByteArrayInputStream(str.getBytes("UTF-8"));
return SUCCESS;
}
}
5. Configure struts.xml
In the struts configuration file, the type of result is set to stream.There are two parameters, the first is contentType, which indicates the type of response. If you have Chinese, it is best to set the encoding. The second parameter is to specify the corresponding input stream in the Action. Its default value is inputStream, so it can be omitted.
<action name="insertCart" class="org.action.InsertCartAction">
<result name="success" type="stream">
<param name="contentType">text/html; charset=utf-8</param>
<param name="inputName">inputStream</param>
</result>
</action>