EasyUI框架 使用Ajax提交注册信息的实现代码
一、服务器代码:
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
System.out.println("list==" + list);
toBeJson(list,studentService.getStudentTotal());
return "toJson";
}
// 转化为Json格式
public void toBeJson(List
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 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
/**
- */
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
// 通过反射<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;
}
}
三、页面代码:
<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"></interceptor>
<!-- 重新定义defaultStack拦截器栈,需要先判断权限 -->
<interceptor-stack name="defaultStack">
<interceptor-ref name="checkePrivilege" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 配置全局的Result -->
<global-results>
<result name="loginUI">/WEB-INF/jsp/user/loginUI.jsp</result>
<result name="noPrivilegeError">/noPrivilegeError.jsp</result>
</global-results>
<!-- 测试用的action,当与Spring整合后,class属性写的就是Spring中bean的名称 -->
<action name="test" class="testAction">
<result name="success">/test.jsp</result>
</action>
<action name="*_*" class="{1}Action" method="{2}">
<result name="{2}">/WEB-INF/jsp/{1}/{2}.jsp</result>
<!-- 跳转到添加与修改页面 -->
<result name="saveUI">/WEB-INF/jsp/{1}/saveUI.jsp</result>
<!-- 返回list页 -->
<result name="toList" type="redirectAction">{1}_list?parentId=${parentId}</result>
<!-- 返回主页 -->
<result name="toIndex" type="redirect">/index.jsp</result>
<!-- 返回论坛主题 -->
<result name="toShow" type="redirectAction">topic_show?id=${id}</result>
<result name="toTopicShow" type="redirectAction">topic_show?id=${topicId}</result>
<!-- json解析 -->
<result name="toJson" type="json">
<param name="root">josnMap</param>
</result>
<result name="reg">/easyui.jsp</result>
</action>
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:https://www.f2er.com/ajax/36108.html