前言
假设使用了MVC框架(比如,struts2)。
服务器的响应数据,分3种情况
1、响应数据是结果页面
2、响应数据是json格式的数据
3、响应数据是json格式的数据,然后再重新发出一次请求
注:响应数据要么是结果页面,要么是json格式的数据,否则,结果页面会覆盖掉json格式的数据——从而导致jquery的post的回调函数没有执行,导致struts2没有跳转到结果页面。
响应数据是结果页面
1.响应数据是结果页面,表示跳转到结果页面。
2.业务的应用场景一般是,查(询)数据就是跳转到结果页面。
代码示例
//业务控制器类
//查看任务单反馈信息(注:一般是查数据的时候,就是跳转到结果页面)
public String view() {
try {
feedbacklist = feedbackService
.getTaskFeedbackInfoListByTaskId(taskinfo.getTaskid());
} catch (Exception e) {
e.printStackTrace();
}
return "view";
}
响应数据是json格式的数据
1.响应数据是json格式的数据的话,那么肯定不是根据struts2配置文件的结果页面进行跳转——实际上,这个时候也没有配置结果页面,否则会覆盖掉json格式的数据。
2.业务的应用场景是,
1)对数据库进行增、删、改的操作。
2)刷新当前页面的部分数据。
代码示例
//.jsp代码:发出请求
<s:form action="taskInfoAction!save.action" id="myform" method="post"
cssClass='subForm'>
<s:hidden name='taskinfo.taskid' />
<div id="bodycontent">
<p align="left">
<font class="title_little">任务情况表</font>
</p>
<table width="1000" height="" border="1">
<tr>
<td class="clum_title" width="125">
项目编号:
</td>
<td width="125">
<s:hidden name="taskinfo.projectid" id="projectid"></s:hidden>
<s:textfield name="taskinfo.projectcode" maxlength="32" id="projectcode" cssStyle="width:123"/>
</td>
<td width="125">
项目简称:
</td>
<td width="125" colspan="3">
<s:textfield name="taskinfo.projectname" cssClass="required"
maxlength="64" id="projectname" cssStyle="width:325" onclick="selectProjectInfo()"/>
</td>
<!--
<td class="clum_title" width="125">
项目类型
</td>
<td width="125">
<s:select name="taskinfo.tasktype" headerKey="" id="projecttype"
cssClass="required" headerValue="--请选择--"
list="@com.telewave.projectmanagerweb.util.TwUtil@getDict('ProjectType')" onchange="showUnitModel(this)"/>
</td>
<td width="125">所属里程碑</td>
<td width="125" id="milestone_td"></td>
-->
</tr>
<tr>
<td class="clum_title">
工作计划
</td>
<td colspan="7">
<font class="introduction">(有计划必须填写)</font>
<s:hidden name="taskinfo.workplanid" id="workplanid" />
计划名称:
<s:textfield name="planname" cssClass="required"
maxlength="64" id="planname" cssStyle="width:160" />
计划拟制人:
<s:textfield name="workplanname" cssClass=""
maxlength="64" id="workplanname" cssStyle="width:205" />
<input type="button" value="选择计划信息"
onclick="selectWorkPlanInfo('add')" style="width: 125" />
</td>
</tr>
<tr>
<td class="clum_title">
任务名称
</td>
<td colspan="2">
<s:textfield name="taskinfo.taskname" cssClass="required" cssStyle="width: 100%" maxlength="64" />
</td>
<td>
所属部门
</td>
<td colspan="2">
<s:select list="organs" name="taskinfo.departmentid" listKey="organcode" listValue="organname" headerKey="" headerValue="--请选择--" cssClass="required"/>
</td>
<td>
计划外任务单
</td>
<td>
<s:select list="#{'1':'是','0':'否'}" name="taskinfo.outofplan" headerValue="--请选择--" cssClass="required"/>
</td>
</tr>
//业务控制器类
// 保存或者修改领导批示信息
public void save() {
try {
/* 新增任务单基本信息 */
// 获取自动生成的id
String guidid = GuidManager.GetGuid();
taskinfo.setTaskid(guidid);
taskinfo.setLockstate("0");
taskinfo.setTaskcode(this.getTaskcode());// add by hql 20101108
// 新增一条任务单基本信息
if("1".equals(taskinfo.getOutofplan())){
Workplandetail workplandetail=new Workplandetail();
workplandetail.setWorkplandetailid(GuidManager.GetGuid());
workplandetail.setWorkplanid(taskinfo.getWorkplanid());
workplandetail.setPrincipal(taskinfo.getWorkplanname());
workplandetail.setWorkdescription(taskinfo.getTaskname());
workplandetail.setStartdate(taskinfo.getBegindated());
workplandetail.setEnddate(taskinfo.getEnddate());
workplandetail.setPrincipal(taskinfo.getAcceptperson());
workplandetail.setProjectid(taskinfo.getProjectid());
workplandetail.setProjectname(taskinfo.getProjectname());
workplandetail.setPlanresult(taskinfo.getOutputResult());
workplandetail.setWorkcontent(taskinfo.getWorkrequired());
workplandetail.setPlanworkload(taskinfo.getWorkload());
workplandetail.setTaskrelated("是");
plandetailService.save(workplandetail);
taskinfo.setWorkplanid(workplandetail.getWorkplandetailid());
}
taskinfoService.addTaskinfo(taskinfo);
/* 新增任务单方法 */
/* 如果页面中的变更信息不为空,则新增一条变更信息的记录
if (!StringUtils.isNull(changeinfo.getChangecontent())) {
// 设置变更信息的外键
changeinfo.setTaskid(guidid);
// 新增一条变更信息
changeinfoService.save(changeinfo);
} 新增变更信息方法 */
/* 如果页面中的绩效考核信息不为空,则新增一条绩效考核信息的记录 */
if (!isEmpty(evaluateinfo)) {
if (!StringUtils.isNull(evaluateinfo.getEvakuateremark()) || !StringUtils.isNull(evaluateinfo.getEvaluatetotalscore())
|| !StringUtils.isNull(evaluateinfo.getExecutionevaluatscore()) || !StringUtils.isNull(evaluateinfo.getQualityevaluatscore())
|| !StringUtils.isNull(evaluateinfo.getScheduleevaluatscore())) {
// 新增绩效考核信息
evaluateinfo.setTaskid(guidid);
evaluateinfoService.addTaskresultevaluationinfo(evaluateinfo);
}
}/* 新增绩效考核信息方法 */
// 新增工作产物信息
if (list!=null&&!list.isEmpty()) {
for (int i = 0; i < list.size(); i++) {
Taskworkproductinfo workproductentity = new Taskworkproductinfo();
workproductentity = list.get(i);
if (!isEmpty(workproductentity)) {
if (!isNullWorkProductEntity(workproductentity)) {
workproductentity.setTaskid(guidid);
String productid = GuidManager.GetGuid();
workproductentity.setId(productid);
workproductService.addTaskworkproductinfo(workproductentity);
} else {
if (!isEmpty(workproductentity.getId())) {
workproductService.deleteTaskworkproductinfo(workproductentity.getId());
}
}
}
}
}
super.printSuccssMsg("新增信息成功", guidid);
} catch (Exception e) {
super.printFaitureMsg("新增信息失败");
log.error(e.getMessage(), e);
}
}
//业务控制器类的父类
/**
* 响应数据-成功
* @param successMessage
* @param id
*/
protected void printSuccssMsg(String successMessage, String id) {
try {
if (isEmpty(id))
getResponse().getWriter().print(
"{success:true,tip:'成功提示',msg:'" + successMessage
+ "',Data:''}");
else
getResponse().getWriter().print(
"{success:true,id:'" + id + "',tip:'成功提示',msg:'"
+ successMessage + "',Data:''}");
} catch (Exception localException) {
}
}
响应数据是json格式的数据,然后在jquery post的回调函数里再重新发出一次请求
代码示例
//.js代码:发出请求
// 遮罩层-注册
function register1(){
// 提交表单
var form = document.getElementById("register");
var param = $("#register").serialize();
$.post(form.action,
param,
function(json){
if(json.success){
$.unblockUI(); // 退出遮罩层
window.location = "registerSuccess.jsp"; //发起一次新的请求
}else{
alert("注册失败!");
}
},
"json");
}
//配置文件
<struts>
<package name="loginPackage" extends="struts-default"
namespace="/login">
<action name="register" class="loginControllerBean" method="register"></action>
</package>
</struts>
//业务控制器类
public void register(){
//
loginBusiness.register(user);
//
try {
response.getWriter().print("{\"success\":\"true\",\"data\":\"\"}");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}