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

前端之家收集整理的这篇文章主要介绍了EasyUI框架 使用Ajax提交注册信息的实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

一、服务器代码

{ private static final long serialVersionUID = -2612140283476148779L;

private Logger logger = Logger.getLogger(StudentAction.class);
private String rows;// 每页显示的记录数
private String page;// 当前第几页
private Map<String,Object> 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<String,Object> getJosnMap() {
return josnMap;
}

public void setJosnMap(Map<String,Object> josnMap) {
this.josnMap = josnMap;
}

}

二、BaseAction代码

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];

  // 通过反射<a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" class="keywords">生成</a>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框架 <%@ include file="/WEB-INF/jsp/public/common.jspf" %>


<table>
<tr>
<td>登陆名称:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>登陆密码:</td>
<td><input type="password" name="password"/></td>
</tr>
</table>


<form id="easyui_regForm" method="post">
<table>
<tr>
<td>登陆名称:</td>
<td><input type="text" name="loginName" class="easyui-validateBox" data-options="required:true,missingMessage:'用户名称不能为空'"/></td>
</tr>
<tr>
<td>登陆密码:</td>
<td><input id="reg_pwd" type="password" name="password" class="easyui-validateBox" data-options="required:true,missingMessage:'用户密码不能为空'"/></td>
</tr>
<tr>
<td>确定密码:</td>
<td></td>
</tr>
</table>

四、struts2.xml配置

<package name="default" namespace="/" extends="json-default">

<interceptors>
  <!-- 声明一个拦截器 -->
  <interceptor name="checkePrivilege" class="cn.oppo.oa.interceptor.CheckPrivilegeInterceptor"&gt;</interceptor>

  <!-- 重新定义defaultStack拦截器栈,需要先判断权限 -->
  <interceptor-stack name="defaultStack"&gt;
    <interceptor-ref name="checkePrivilege" />
    <interceptor-ref name="defaultStack" />
  </interceptor-stack>
</interceptors>


<!-- 配置全局的Result -->
<global-results>
  <result name="loginUI"&gt;/WEB-INF/jsp/user/loginUI.jsp</result>
  <result name="noPrivilegeError"&gt;/noPrivilegeError.jsp</result>
</global-results>


<!-- 测试用的action,当与Spring整合后,class属性写的就是Spring中bean的名称 -->
<action name="test" class="testAction"&gt;
  <result name="success"&gt;/test.jsp</result>
</action>


<action name="*_*" class="{1}Action" method="{2}"&gt;
  <result name="{2}"&gt;/WEB-INF/jsp/{1}/{2}.jsp</result>
  <!-- 跳转到添加与修改页面 -->
  <result name="saveUI"&gt;/WEB-INF/jsp/{1}/saveUI.jsp</result>
  <!-- 返回list页 -->
  <result name="toList" type="redirectAction"&gt;{1}_list?parentId=${parentId}</result>
  <!-- 返回主页 -->
  <result name="toIndex" type="redirect"&gt;/index.jsp</result>
  <!-- 返回论坛主题 -->
  <result name="toShow" type="redirectAction"&gt;topic_show?id=${id}</result>
  <result name="toTopicShow" type="redirectAction"&gt;topic_show?id=${topicId}</result>
  <!-- json解析 -->
  <result name="toJson" type="json"&gt;
    <param name="root"&gt;josnMap</param>
  </result>

  <result name="reg"&gt;/easyui.jsp</result>


</action>

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持

原文链接:https://www.f2er.com/ajax/36108.html

猜你在找的Ajax相关文章