Losing modelattribute between methods
This controller works as it should
@SessionAttributes("giveForm")
@Controller
public class GiveFormController {
private PersonDao personManager;
private KeyCardManager database;
private GiveFormValidator validator;
public 开发者_StackOverflow社区GiveFormController() {
}
@Autowired
public GiveFormController(KeyCardManager database, PersonDao personManager, GiveFormValidator validator) {
this.database = database;
this.validator = validator;
this.personManager = personManager;
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(getValidator());
}
@RequestMapping(value = "/give", method = RequestMethod.GET)
public String give(Model model) {
GiveForm giveForm = new GiveForm();
model.addAttribute(giveForm);
return "give";
}
@RequestMapping(method = RequestMethod.POST, params = {"continue"})
public String go(@ModelAttribute("giveForm") @Valid GiveForm giveForm,
BindingResult result, Model model, SessionStatus status, HttpServletRequest request) {
String customer = giveForm.getCustomer();
if (!customer.isEmpty()) {
Person person = getPersonManager().findByUid(customer);
if (person != null) {
model.addAttribute("customerName", person.getCn());
model.addAttribute("userIdActive", person.isActive());
}
}
if (result.hasErrors()) {
return "give";
} else {
return "give-confirm";
}
}
@RequestMapping(method = RequestMethod.POST, params = {"cancel"})
public String cancel(@ModelAttribute("giveForm") GiveForm giveForm, SessionStatus status) {
status.setComplete();
return "index";
}
@RequestMapping(method = RequestMethod.POST, params = {"ready"})
public String ready(@ModelAttribute("giveForm") GiveForm giveForm, SessionStatus status,
HttpServletRequest request, Model model) {
System.out.println(giveForm.getKeyId());
status.setComplete();
model.addAttribute(giveForm);
if(getDatabase().insertGive(giveForm, request.getRemoteUser())) {
return "success";
}
else {
return "operationfailed";
}
}
But this returns null when getting the modelattribute to ready-method:
@SessionAttributes("takeForm")
@Controller
public class XXTakeFormController {
private TakeFormValidator validator;
private KeyCardManager database;
private PersonDao ldap;
public XXTakeFormController(){
}
@Autowired
public XXTakeFormController(TakeFormValidator validator, KeyCardManager database, PersonDao ldap) {
this.database = database;
this.validator = validator;
this.ldap = ldap;
}
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(getValidator());
}
@RequestMapping(value = "/take", method = RequestMethod.GET)
public String take(Model model) {
TakeForm takeForm = new TakeForm();
model.addAttribute(takeForm);
return "take";
}
@RequestMapping(method = RequestMethod.POST, params = {"continue"})
public String go(@ModelAttribute("takeForm") @Valid TakeForm takeForm,
BindingResult result, Model model, SessionStatus status) {
if (result.hasErrors()) {
return "take";
}
else {
String criteria = takeForm.getCriteria();
if (Character.isDigit(criteria.charAt(0))) {
// criteria is a keyId
String customer = getDatabase().getHolderOf(criteria);
takeForm.setCustomer(customer);
Person person = getLdap().findByUid(customer);
if (person != null) {
model.addAttribute("customerName", person.getCn());
}
model.addAttribute("customer", customer);
List keys = new ArrayList();
keys.add(getDatabase().getKey(criteria));
model.addAttribute("keys", keys);
} else {
// criteria is a userId
takeForm.setCustomer(criteria);
model.addAttribute("customer", criteria);
model.addAttribute("customerName", getLdap().findByUid(criteria).getCn());
model.addAttribute("keys", getDatabase().getKeysOf(criteria));
}
}
return "take-confirm";
}
@RequestMapping(method = RequestMethod.POST, params = {"cancel"})
public String cancel(@ModelAttribute("takeForm") TakeForm takeForm,
SessionStatus status) {
status.setComplete();
return "index";
}
@RequestMapping(method = RequestMethod.POST, params = {"ready"})
public String ready(@ModelAttribute("takeForm") TakeForm takeForm, SessionStatus
status, HttpServletRequest request,Model model) {
status.setComplete();
model.addAttribute(takeForm);
System.out.println(takeForm.getKeyId());
if(getDatabase().executeTake(takeForm.getKeyId(), request.getRemoteUser(), takeForm.getNotes())){
return "success";
}
else {
return "operationfailed";
}
}
It is probably something I'm blind to. Both ready-methods return the same simple view. Can someone borrow eyes for me? :)
model.addAttribute("key","value");
It is mainly used to set the data to model in controller. That data will be retrieved in jsp file directly by using key name as ${key}
All the data, which is added to the Model will be accessed in the jsp file directly by using ${name}
.
精彩评论