Transmission of front-end and back-end data

Transmission method of front and back data

1. First, the data generated by the front-end is obtained through json, and then transmitted to the back-end using Ajax. The jQuery library needs to be added because the json data operation is encapsulated in the jQuery library.
You can set the transfer mode and other attributes as shown in the figure below.

			$.ajax({
    
    
				type:'POST',
				data:a,//json
				contentType = 'application/json',
				dataType:'json',
				url:'user/saveJsonUser.do',
				success:function(data){
    
    
					alert("发送成功");
				},
				error:function(e){
    
    
					alert("发送失败");
				}

2. Use the form to transfer: There is an action attribute in the form, and the form can be checked for data before being transferred to the background. The form form has an onsubmit method, you can call js to check the data, such as whether the input is legal, if the return value of onsubmit is true, then submit the data to the backend, otherwise directly return an error on the frontend. You can use action to submit data to the backend, which is followed by the URL for background processing

<form action="${pageContext.request.contextPath}/login.action" method="post">
   用户名<input type="text" name="user.name"><br>
   密码<input type="password" name="user.password"><br>
   <input type="submit" value="登录">
</form>

3. Get the label through dom, and trigger the submit method of the label to directly submit the data to the background

So how does the backend receive data? Take action as an example here

The back-end receiving data can be
submitted by the controller or servlet action, and the method of receiving the data in the background
1. The request object obtains the request
parameters. After obtaining the Request instance through ServletActionContext.getRequest(), the parameters are obtained directly.


import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
    
    
    public String login() {
    
    
        //通过request对象获取请求参数
        HttpServletRequest request = ServletActionContext.getRequest();
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        //打印属性
        System.out.println(name);
        System.out.println(password);
        return NONE;
    }
}

2. Get request parameters through attribute set injection
. . . . . . . To be added

Guess you like

Origin blog.csdn.net/gsy_csdn1/article/details/115028810