在我的应用程序中,当一个用户使用 bean 对象时,它的生命周期也必须作为控制器类中的会话来维护。不同的用户必须是不同的 bean 对象。所以我添加了会话范围,例如:
<bean id="constants" class="com.project.barlexamquize.model.Constants" scope="session" >
<aop:scoped-proxy/>
</bean>
它在同时在不同浏览器中检查的本地 PC 上运行良好。但是,当此应用程序部署在 Google App Engine 中时,它不起作用。在这里,它的行为类似于请求范围,即。在每个 http 请求中,它都会创建新的 bean 对象。但是我需要为每个用户初始化一次 bean 会话。我的代码如下。请访问:我的网站
@Controller
@RequestMapping("/barlexamquize")
public class BarlExamQuizeController {
public static long totalQuestion=390;
@Autowired Constants constants;
@RequestMapping(value = "/giveAnswer", method = RequestMethod.GET)
public String practiceQuestions(HttpServletRequest request, ModelMap model) {
PersistenceManager pm=null;
Query q=null;
List<BarlQuestion> barlQuestion = null;
pm = PMF.get().getPersistenceManager();
q= pm.newQuery(BarlQuestion.class, "id == idParameter");
q.declareParameters("long idParameter");
try {
barlQuestion = (List<BarlQuestion>) q.execute(constants.getId());
if (barlQuestion.isEmpty()) {
model.addAttribute("barlQuestion", null);
} else {
model.addAttribute("barlQuestion", barlQuestion.get(0));
}
} finally {
q.closeAll();
pm.close();
}
return "giveAnswer";
}
@RequestMapping(value = "/checkAnswer", method = RequestMethod.POST)
public String checkQuestionAnswer(@RequestParam String result,
ModelMap model) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query q = pm.newQuery(BarlQuestion.class, "id == idParameter");
q.declareParameters("long idParameter");
List<BarlQuestion> barlQuestion = null;
try {
barlQuestion = (List<BarlQuestion>) q.execute(constants.getId());
if (result.equals(barlQuestion.get(0).getAnswer())) {
constants.setRs("Right Choice");
constants.incrementRightAns();
} else
constants.setRs("Wrong!!! Correct: " + barlQuestion.get(0).getAnswer());
model.addAttribute("status", constants.getRs());
} finally {
q.closeAll();
pm.close();
}
return "questionResult";
}
@RequestMapping(value = "/nextQuestion", method = RequestMethod.POST)
public String nextQuestion(HttpServletRequest request, ModelMap model) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Query q = pm.newQuery(BarlQuestion.class, "id == idParameter");
q.declareParameters("long idParameter");
List<BarlQuestion> barlQuestion = null;
if (constants.getId() < totalQuestion) {
constants.incrementId();
try {
barlQuestion = (List<BarlQuestion>) q.execute(constants.getId());
if (barlQuestion.isEmpty()) {
model.addAttribute("barlQuestion", null);
} else {
model.addAttribute("barlQuestion", barlQuestion.get(0));
}
} finally {
q.closeAll();
pm.close();
}
return "giveAnswer";
} else {
String score = ((constants.getRightAns() * 100) / totalQuestion) + "%";
model.addAttribute("score", score);
constants.setRightAns(0);
constants.setId(1);
return "examScore";
}
}
}
请帮我找出我的代码的问题。为了理解我的想法,我的应用程序有一个问题数据存储区。应用程序一次显示一个问题并回答该问题,然后显示下一个问题。在控制器中,constants.getId() 是关注点。在下一个问题中是增量。所以增量值应该保留在一个会话中。