easyui ajax post,实例详解EasyUI框架使用Ajax提交注册信息

本文详细展示了如何在EasyUI框架中使用Ajax进行注册信息的提交。通过控制器(StudentAction)的`reg()`方法处理注册逻辑,利用Struts2的Ajax响应将结果转化为Json并返回给前端。前端页面使用jQuery的Ajax方法发送请求,验证表单后提交数据,成功后关闭弹窗并显示提示信息。
摘要由CSDN通过智能技术生成

本文主要介绍了EasyUI框架 使用Ajax提交注册信息的实现代码的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下,希望能帮助到大家。

EasyUI框架 使用Ajax提交注册信息的实现代码

一、服务器代码:

@Controller

@Scope("prototype")

public class StudentAction extends BaseAction {

private static final long serialVersionUID = -2612140283476148779L;

private Logger logger = Logger.getLogger(StudentAction.class);

private String rows;// 每页显示的记录数

private String page;// 当前第几页

private Map josnMap = new HashMap<>();

// 查询出所有学生信息

public String list() throws Exception {

return "list";

}

public String regUI() throws Exception {

return "regUI";

}

// 查询出所有学生信息

public String listContent() throws Exception {

List list = studentService.getStudentList(page, rows);

System.out.println("list==" + list);

toBeJson(list, studentService.getStudentTotal());

return "toJson";

}

// 转化为Json格式

public void toBeJson(List list, int total) throws Exception {

josnMap.put("total", total);

josnMap.put("rows", list);

JSONParser.writeJson(josnMap);// 自定义的工具类

}

public String reg(){

logger.error("kkk");

try {

studentService.save(model);

josnMap.put("success", true);

josnMap.put("msg", "注册成功!");

} catch (Exception e) {

e.printStackTrace();

josnMap.put("success", false);

josnMap.put("msg", "注册失败!");

}

try {

ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");

ServletActionContext.getResponse().setCharacterEncoding("utf-8");

ServletActionContext.getResponse().getWriter().print(JSON.toJSONString(josnMap));

} catch (IOException e) {

e.printStackTrace();

}

return "toJson";

}

public void setRows(String rows) {

this.rows = rows;

}

public void setPage(String page) {

this.page = page;

}

public Map getJosnMap() {

return josnMap;

}

public void setJosnMap(Map josnMap) {

this.josnMap = josnMap;

}

}

二、BaseAction代码:

import java.lang.reflect.ParameterizedType;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import cn.oppo.oa.service.DepartmentService;

import cn.oppo.oa.service.ForumService;

import cn.oppo.oa.service.PrivilegeService;

import cn.oppo.oa.service.RoleService;

import cn.oppo.oa.service.StudentService;

import cn.oppo.oa.service.UserService;

import com.alibaba.fastjson.JSON;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

public abstract class BaseAction extends ActionSupport implements ModelDriven {

/**

*

*/

private static final long serialVersionUID = 1L;

@Resource

protected RoleService roleService;

@Resource

protected DepartmentService departmentService;

@Resource

protected UserService userService;

@Resource

protected PrivilegeService privilegeService;

@Resource

protected ForumService forumService;

@Resource

protected StudentService studentService;

protected T model;

@SuppressWarnings("unchecked")

public BaseAction() {

try {

// 得到model的类型信息

ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();

Class clazz = (Class) pt.getActualTypeArguments()[0];

// 通过反射生成model的实例

model = (T) clazz.newInstance();

} catch (Exception e) {

throw new RuntimeException(e);

}

}

public void writeJson(Object object){

try {

String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");

ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");

ServletActionContext.getResponse().setCharacterEncoding("utf-8");

ServletActionContext.getResponse().getWriter().write(json);

ServletActionContext.getResponse().getWriter().flush();

ServletActionContext.getResponse().getWriter().close();

} catch (Exception e) {

e.printStackTrace();

}

}

public T getModel() {

return model;

}

}

三、页面代码:

EasyUI框架

$(function(){

if(${"#easyui_regForm"}.form('validate')){

$.ajax({

url:'${pageContext.request.contextPath}/student_reg.action',

data:${"#easyui_regForm"}.serialize(),

dataType:'json',

success:function(obj,status,jqXHR){

if(obj.success){

$("#easyui_regDialog").dialog('close');

}

$.message.show({

title:'提示',

msg:obj.msg

});

}

});

}else{

alert('验证失败');

}

});

aa

cc

dd

kk

登陆名称:
登陆密码:
登陆名称:
登陆密码:
确定密码:

四、struts2.xml配置

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/WEB-INF/jsp/user/loginUI.jsp

/noPrivilegeError.jsp

/test.jsp

/WEB-INF/jsp/{1}/{2}.jsp

/WEB-INF/jsp/{1}/saveUI.jsp

{1}_list?parentId=${parentId}

/index.jsp

topic_show?id=${id}

topic_show?id=${topicId}

josnMap

/easyui.jsp

相关推荐:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值