Ajax cross-Page Submission form, ajax cross-Form
As mentioned above, in addition to token password verification and redirection, a frequently used method is to process the form submission on a new page and close the current page after it is completed, and refresh the previously sent request page.
ArtDialog. js is used here.
1. File structure
2. user. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri ="/struts-tags "prefix =" s "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "Prefix =" c "%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
3. userAdd. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib uri ="/struts-tags "prefix =" s "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "Prefix =" c "%> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
4. UserManageAction. java
Package com. myssh2.action; import java. io. IOException; import java. io. printWriter; import javax. annotation. resource; import javax. servlet. servletException; import org. apache. struts2.ServletActionContext; import org. springframework. context. annotation. scope; import org. springframework. stereotype. controller; import com. myssh2.bean. user; import com. myssh2.service. userService; import com. opensymphony. xwork2.ActionContext; import com. opensymphony. xwork2.ActionSupport; @ Controller @ Scope ("prototype") public class UserManageAction extends ActionSupport {@ Resource UserService userService; private User user; public User getUser () {return user ;} public void setUser (User user) {this. user = user;} public String addUI () {return "add";} public void add () throws ServletException, IOException {ServletActionContext. getResponse (). setContentType ("text/html; charset = UTF-8"); PrintWriter out = ServletActionContext. getResponse (). getWriter (); try {userService. addUser (user); ActionContext. getContext (). put ("message", "saved successfully"); out. write ("{\" success \ ": true}");} catch (Exception e) {e. printStackTrace (); out. write ("{\" success \ ": false, \" msg \ ": \" error \"}");}}}
Page Effect
Use $ ('# userForm'). serialize (); to serialize form data when submitting a form.
Window. opener. art. dialog ({time: 2, content: 'saved successfully'}); returns window. open pages (or read as parent pages), and call the artDialog to disable dialog periodically.
SetTimeout (function () {window. opener. location. reload () ;}, 3); Use the timer to refresh the window. for open pages (or parent pages), you need to reset the time settings of dialog and reload.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.