我試圖保存一些和更新我的數據在春季會議,但它不工作。我用ajax向控制器發送數據,數據由控制器接收。我嘗試了幾個小時,但無法弄清楚問題所在。這裏是我的控制器代碼無法更新會話值春天
@Controller
@RequestMapping("/pages/CreateQuestion")
@SessionAttributes({"question", "preview", "qparameters", "qhparameters"})
public class CreateQuestionController {
@RequestMapping(value = "/SaveQuestion/two", method = RequestMethod.POST)
public @ResponseBody
ResponseObject saveQuestionTwo(HttpServletRequest request, HttpServletResponse res, ModelMap map) {
ResponseObject obj = null;
try {
int auth = UserAuthentication.checkAuthentication();
if (auth > 0) {
String question = request.getParameter("question");
String preview = request.getParameter("preview");
String[] params = request.getParameterValues("params[]");
String[] hparams = request.getParameterValues("hparams[]");
if (question != null && question.length() > 5) {
QuestionStepOne one = (QuestionStepOne) map.get("one");
if (one.getParamiterised().equals("Yes")) {
if (params == null && hparams == null) {
obj = new ResponseObject("e", "Please provide data.");
} else {
if (params != null && hparams != null) {
setSessionValue(request, "qparameters", params);
setSessionValue(request, "qhparameters", hparams);
System.out.println("\n\n\n\n add both");
} else if (params == null && hparams != null) {
setSessionValue(request, "qhparameters", hparams);
removeSessionValue(request, "qparameters");
System.out.println("\n\n\n\n add Hparam and remove param");
} else if (params != null && hparams == null) {
setSessionValue(request, "qparameters", hparams);
removeSessionValue(request, "qhparameters");
System.out.println("\n\n\n\n add param and remove hparam");
}
setSessionValue(request, "question", question);
setSessionValue(request, "preview", preview);
List<Object> list = new ArrayList<Object>();
if (params != null && params.length > 0) {
list.add(params);
}
if (hparams != null && hparams.length > 0) {
list.add(hparams);
}
obj = new ResponseObject("s", "Y", list);
}
} else {
setSessionValue(request, "question", question);
setSessionValue(request, "preview", preview);
removeSessionValue(request,"qparameters");
removeSessionValue(request,"qhparameters");
obj = new ResponseObject("s", "N");
}
} else {
obj = new ResponseObject("e", "Please enter question to be saved");
}
} else {
obj = new ResponseObject("e", "You must be logged in");
}
} catch (Exception e) {
} finally {
System.out.println(getSessionValue(request,"question"));
System.out.println(getSessionValue(request,"preview"));
System.out.println(getSessionValue(request,"qparameters"));
System.out.println(getSessionValue(request,"qhparameters"));
return obj;
}
}
private static Object getSessionValue(HttpServletRequest request, String key) {
return request.getSession().getAttribute(key);
}
private static void setSessionValue(HttpServletRequest request, String key, Object obj) {
request.getSession().setAttribute(key, obj);
}
private static void removeSessionValue(HttpServletRequest request, String key) {
request.getSession().removeAttribute(key);
}
}
我試着用ModelMap也:
map.put("qparameters",params);
這也不能正常工作。
編輯:我使用Ajax的請求:
$.ajax({
url: 'SaveQuestion/two',
type: "POST",
data: {
question: contentForTest,
preview: content,
params: params,
hparams: hparams
},
success: function(data) {
console.log(data);
}
});
之前發送響應我正在印刷這是寫,但JSP其沒有反映的值。
謝謝!
JavaScript無法訪問會話變量。響應所需的更新響應的主體。 – Slihp
如果您的會話狀態未在某些屬性上更新。檢查請求和邏輯。 – Slihp