我正在尝试保存一些并在Spring会话中更新我的数据,但它无法正常工作。我用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中未反映的值。
谢谢!
答案 0 :(得分:0)
我认为以下内容无效:request.getParameterValues("params[]")
,因为我认为您的页面中没有名为params[]
的参数。相反,您可以在方法签名中添加@RequestParam (required = false) String[] params
(并为所有参数执行此操作)。
然后,当您声明@SessionAttributes
时,将这些属性放入模型中会将它们添加到会话中。但您可能需要使用org.springframework.ui.Model
而不是ModelMap
,因为后者已被弃用。然后执行model.addAttribute("params", params)
。
答案 1 :(得分:-1)
JavaScript无法访问会话变量。回复所需的更新响应正文