2013-11-01 2 views
0

내 응용 프로그램에서 한 사용자가 bean 객체를 사용할 때 라이프 사이클을 유지해야합니다. 컨트롤러 클래스의 세션으로도 사용됩니다. 다른 사용자는 다른 bean 객체 여야합니다. 따라서 다음과 같은 세션 범위를 추가하십시오.봄 mvc를 사용하여 콩 세션 범위를 만들고 싶습니다. 내 로컬 호스트 PC에서 잘 작동하지만 Google 응용 프로그램 엔진에서는 작동하지 않습니다.

<bean id="constants" class="com.project.barlexamquize.model.Constants" scope="session" > 
     <aop:scoped-proxy/> 
</bean> 

다른 브라우저에서 동시에 확인하면 잘 작동합니다. 하지만이 앱이 Google App Engine에 배포되는 동안에는 작동하지 않습니다. 여기 요청 범위처럼 동작합니다. 각각의 http 요청에서 새로운 bean 객체를 생성한다. 하지만 각 사용자에 대해 콩 세션을 한 번 초기화해야합니다. 내 코드는 다음과 같습니다. 다음을 방문하십시오 : my site

@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()가 중요합니다. 다음 질문에서 증가합니다. 따라서 증분 값은 한 세션에 남아 있어야합니다.

+0

상수가 Serializable인지 확인 했습니까? –

+0

나는 Serializable으로 상수를 만들지 않았다. 하지만 이제는 Serializable으로 구현합니다. 그것은 Woking 벌금이고 그것은 세션을 유지할 수 있습니다. 대단히 고마워. –

답변

0

상수 bean을 일련 화 가능하게 만드십시오.