Reputation: 1807
The application is more complex than this so I've tried to extract the relevant part of the code.
The flow of the web application is that I select from a list the executingShop by calling the selectItemForOrder()
method.
The id of the executingShop is set correctly.
But then when I press the finishOrder button that calls the finishOrder()
method i get NullPointerException
. How can I transmit the data since I believe that the controller class gets destroyed and a new controller is created when I press the finishOrder button.
Saving the id in the database is not an option (that is the problem that I am trying to fix).
Sorry if this is a trivial question. I'm new to Spring MVC.
public class MyController {
private Integer executingShopId;
public View selectItemForOrder() {
setExecutingShopId(1);
return View;
}
public View finishOrder() {
System.out.println("Shop id: " + getExecutingShopId());
computeOrder(executingShopId);
return View;
}
}
Upvotes: 1
Views: 325
Reputation: 311
yes you can have keep maintain the value of id at jsp page by using hidden field like:
<form:hidden path="id"/>
Upvotes: 1
Reputation: 1350
Did you change the config of Controller? for the default the Controller is singleton mode, so your code doesn't cause the problem. If you do change the config that the Controller is prototype mode, it will cause the problem. definitely you can store the value in hidden, but I recommend you to store the value in session, because it makes more sense and more secure.
public View selectItemForOrder(HttpSession session) {
session.setAttribute(XXXX,YYY);
return View;
}
public View finishOrder(HttpSession session) {
Session.getAttribute(XXXX)
System.out.println("Shop id: " + getExecutingShopId());
computeOrder(executingShopId);
return View;
}
Upvotes: 1