EasyUI框架 使用Ajax提交注冊(cè)信息的實(shí)現(xiàn)代碼
EasyUI框架 使用Ajax提交注冊(cè)信息的實(shí)現(xiàn)代碼
一、服務(wù)器代碼:
@Controller
@Scope("prototype")
public class StudentAction extends BaseAction<Student> {
private static final long serialVersionUID = -2612140283476148779L;
private Logger logger = Logger.getLogger(StudentAction.class);
private String rows;// 每頁(yè)顯示的記錄數(shù)
private String page;// 當(dāng)前第幾頁(yè)
private Map<String, Object> josnMap = new HashMap<>();
// 查詢出所有學(xué)生信息
public String list() throws Exception {
return "list";
}
public String regUI() throws Exception {
return "regUI";
}
// 查詢出所有學(xué)生信息
public String listContent() throws Exception {
List<Student> list = studentService.getStudentList(page, rows);
System.out.println("list==" + list);
toBeJson(list, studentService.getStudentTotal());
return "toJson";
}
// 轉(zhuǎn)化為Json格式
public void toBeJson(List<Student> 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", "注冊(cè)成功!");
} catch (Exception e) {
e.printStackTrace();
josnMap.put("success", false);
josnMap.put("msg", "注冊(cè)失敗!");
}
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 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<T> extends ActionSupport implements ModelDriven<T> {
/**
*
*/
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<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
// 通過(guò)反射生成model的實(shí)例
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;
}
}
三、頁(yè)面代碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>EasyUI框架</title>
<%@ include file="/WEB-INF/jsp/public/common.jspf" %>
<script type="text/javascript">
$(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('驗(yàn)證失敗');
}
});
</script>
</head>
<body class="easyui-layout">
<div data-options="region:'north',split:true" style="height:100px;">aa</div>
<!-- <div data-options="region:'south',split:true" style="height:100px;">bb</div>-->
<div data-options="region:'east',title:'East',split:true" style="width:200px;">cc</div>
<div data-options="region:'west',title:'West',split:true" style="width:200px;">dd</div>
<div data-options="region:'center',title:'center title'" style="padding:5px;background:#eee;">kk</div>
<div class="easyui-dialog" data-options="title:'登陸', modal:true,
closable:false,
toolbar:[{
text:'Edit',
iconCls:'icon-edit',
handler:function(){alert('edit')}
},{
text:'Help',
iconCls:'icon-help',
handler:function(){alert('help')}
}],
buttons:[{
text:'登陸',
handler:function(){alert('登陸')}
},{
text:'注冊(cè)',
handler:function(){
$('#easyui_regForm input').val('');
$('#easyui_regDialog').dialog('open');
}
}]" >
<table>
<tr>
<td>登陸名稱:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>登陸密碼:</td>
<td><input type="password" name="password"/></td>
</tr>
</table>
</div>
<div id="easyui_regDialog" class="easyui-dialog" data-options="title:'注冊(cè)', modal:true,
closable:true,
closed:true,
buttons:[{
text:'注冊(cè)',
handler:function(){
$('#easyui_regForm').form('submit',{
url : '${pageContext.request.contextPath}/student_reg.action',
success : function(data) {
var obj = jQuery.parseJSON(data);
if (obj.success) {
$('#easyui_regDialog').dialog('close');
}
$.messager.show({
title : '提示',
msg : obj.msg
});
}
});
}
},{
text:'取消',
handler:function(){alert('注冊(cè)')}
}]" >
<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><input type="password" name="repassword" class="easyui-validatebox" data-options="required:true,missingMessage:'確認(rèn)密碼不能為空',validType:'equals[\'#reg_pwd\']'" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
四、struts2.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 配置為開(kāi)發(fā)模式 -->
<constant name="struts.devMode" value="true" />
<!-- 配置擴(kuò)展名為action -->
<constant name="struts.action.extension" value="action" />
<!-- 配置主題 -->
<constant name="struts.ui.theme" value="simple" />
<package name="default" namespace="/" extends="json-default">
<interceptors>
<!-- 聲明一個(gè)攔截器 -->
<interceptor name="checkePrivilege" class="cn.oppo.oa.interceptor.CheckPrivilegeInterceptor"></interceptor>
<!-- 重新定義defaultStack攔截器棧,需要先判斷權(quán)限 -->
<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>
<!-- 測(cè)試用的action,當(dāng)與Spring整合后,class屬性寫(xiě)的就是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>
<!-- 跳轉(zhuǎn)到添加與修改頁(yè)面 -->
<result name="saveUI">/WEB-INF/jsp/{1}/saveUI.jsp</result>
<!-- 返回list頁(yè) -->
<result name="toList" type="redirectAction">{1}_list?parentId=${parentId}</result>
<!-- 返回主頁(yè) -->
<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>
</package>
</struts>
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
jQuery除指定區(qū)域外點(diǎn)擊任何地方隱藏DIV功能
這篇文章主要介紹了jQuery除指定區(qū)域外點(diǎn)擊任何地方隱藏DIV的相關(guān)資料,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
jQuery中需要注意的細(xì)節(jié)問(wèn)題小結(jié)
jQuery中需要注意的細(xì)節(jié)問(wèn)題小結(jié),使用jquery的朋友可以參考下。2011-12-12
基于JQuery和CSS3實(shí)現(xiàn)仿Apple TV海報(bào)背景視覺(jué)差特效源碼分享
這是一款效果非常炫酷的jQuery和CSS3仿Apple TV海報(bào)背景視覺(jué)差特效。該視覺(jué)差特效在使用鼠標(biāo)在屏幕上下左右移動(dòng)的時(shí)候,海報(bào)中的各種元素以不同的速度運(yùn)動(dòng),形成視覺(jué)差效果,并且還帶有一些流光特效。2015-09-09
jQuery動(dòng)態(tài)加載css文件實(shí)現(xiàn)方法
使用jQuery來(lái)加載一個(gè)外部的 css 文件,首先創(chuàng)建一個(gè) link 元素,并將它添加到 標(biāo)記中即可。那么基于jquery代碼如何實(shí)現(xiàn)呢?下面小編給大家介紹jQuery動(dòng)態(tài)加載css文件實(shí)現(xiàn)方法,需要的朋友參考下吧2016-06-06
jquery ui dialog實(shí)現(xiàn)彈窗特效的思路及代碼
這篇文章介紹了jquery ui dialog實(shí)現(xiàn)彈窗特效的思路及代碼,有需要的朋友可以參考一下2013-08-08
jQuery實(shí)現(xiàn)簡(jiǎn)單的下拉菜單導(dǎo)航功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡(jiǎn)單的下拉菜單導(dǎo)航功能,涉及jQuery針對(duì)頁(yè)面元素的遍歷與節(jié)點(diǎn)修改相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
jQuery實(shí)現(xiàn)帶滾動(dòng)線條導(dǎo)航效果的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)帶滾動(dòng)線條導(dǎo)航效果的方法,分析了帶滾動(dòng)條導(dǎo)航效果的實(shí)現(xiàn)原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
關(guān)于jquery中動(dòng)態(tài)增加select,事件無(wú)效的快速解決方法
下面小編就為大家?guī)?lái)一篇關(guān)于jquery中動(dòng)態(tài)增加select,事件無(wú)效的快速解決方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08

