I want to make a bean session scope using spring mvc and it works fine in my local host PC but do not work in google app engine

StackOverflow https://stackoverflow.com/questions/19731907

Pregunta

In my application, When one user will use a bean object it's life cycle have to be maintained as session in controller class also. Different user have to be different bean object. So I add session scope like:

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

It works fine in local PC checked in different browser at a same time. But it does not work while this apps is deployed in Google App Engine. Here it behaves like request scope ie. in each http request it create new bean object. But my needs to make the bean session one time initialize for each user. My code are given bellow. Please visit: 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";
        }               
    }
}

Please help me to find the problems of my code. For understanding my thoughts, My application has a Datastore of questions. Application shows one question at a time and take answer of that question, then shows next question. In the controller the constants.getId() is the concern. It is increment in the next question. So the incremental value should be remain in one session.

¿Fue útil?

Solución

Make the Constants bean Serializable.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top