我必须将cookie设置为控制器,但是当我尝试将其添加到HttpServletResponse对象时,它似乎无法正常工作,无论是使用HttpServletResponse.addCookie(String)还是使用CookieGenerator.addCookie(HttpServletResponse,String)[注释]。
@Controller("BookSlotComponentController")
@Scope("tenant")
@RequestMapping(value = ControllerConstants.Actions.Cms.BookSlotComponent)
public class BookSlotComponentController extends AbstractCMSComponentController<BookSlotComponentModel> {
private static Logger LOG = Logger.getLogger(BookSlotComponentController.class);
private final String MORNING_SLOT = "MORNING_SLOT";
private final String AFTERNOON_SLOT = "AFTERNOON_SLOT";
private final String EVENING_SLOT = "EVENING_SLOT";
private final String BOOK_SLOT_SELECTED_DATE = "BOOK_SLOT_SELECTED_DATE";
@Resource(name = "bookSlotFacade")
private BookSlotFacade bookSlotFacade;
@Resource
private TimeService timeService;
@Override
protected void fillModel(final HttpServletRequest request, final Model model, final BookSlotComponentModel component) {
}
@Override
protected void fillModel(final HttpServletRequest request, final HttpServletResponse response, final Model model, final BookSlotComponentModel component) {
LOG.debug("Start: fillModel");
Date refDate = null;
String refDateStr = null;
if (null != request.getSession().getAttribute(BOOK_SLOT_SELECTED_DATE)) {
final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
refDateStr = (String) request.getSession().getAttribute(BOOK_SLOT_SELECTED_DATE);
try {
refDate = timeService.getCurrentTime();
refDate = sdf.parse(refDateStr);
} catch (final ParseException pe) {
LOG.error("Exception while parsing date " + refDate, pe);
}
final BookSlotForm bookSlotForm = populateBookSlotForm(request, response, refDate);
LOG.debug("bookSlotForm getMorningSlots = " + bookSlotForm.getMorningSlots().size());
LOG.debug("bookSlotForm getAfternoonSlots = " + bookSlotForm.getAfternoonSlots().size());
LOG.debug("bookSlotForm getEveningSlots = " + bookSlotForm.getEveningSlots().size());
int biggestListSize = 0;
if (bookSlotForm.getMorningSlots().size() >= bookSlotForm.getAfternoonSlots().size()) {
biggestListSize = bookSlotForm.getMorningSlots().size();
} else {
biggestListSize = bookSlotForm.getAfternoonSlots().size();
}
if (biggestListSize < bookSlotForm.getEveningSlots().size()) {
biggestListSize = bookSlotForm.getEveningSlots().size();
}
model.addAttribute(bookSlotForm);
model.addAttribute("dateSelected", new Boolean(Boolean.TRUE));
request.getSession().removeAttribute(BOOK_SLOT_SELECTED_DATE);
LOG.debug("End: fillModel");
}
}
private BookSlotForm populateBookSlotForm(final HttpServletRequest request, final HttpServletResponse response, final Date refDate) {
LOG.debug("Start: populateBookSlotForm");
final BookSlotForm bookSlotForm = new BookSlotForm();
bookSlotForm.setSelectedDate(refDate);
bookSlotForm.setSelectedStr(refDate);
bookSlotForm.setDayOfWeek(BookSlotHelper.getDayNameFromDate(refDate));
Map<String, List<SlotData>> slotMap = new HashMap<String, List<SlotData>>();
List<SlotData> morningSlotDataLst = new ArrayList<SlotData>();
List<SlotData> afternoonSlotDataLst = new ArrayList<SlotData>();
List<SlotData> eveningSlotDataLst = new ArrayList<SlotData>();
slotMap = bookSlotFacade.getSlotMapForSlotGroup(refDate);
if (null != slotMap.get(this.MORNING_SLOT)) {
morningSlotDataLst = slotMap.get(this.MORNING_SLOT);
}
if (null != slotMap.get(this.AFTERNOON_SLOT)) {
afternoonSlotDataLst = slotMap.get(this.AFTERNOON_SLOT);
}
if (null != slotMap.get(this.EVENING_SLOT)) {
eveningSlotDataLst = slotMap.get(this.EVENING_SLOT);
}
bookSlotForm.setMorningSlots(morningSlotDataLst);
bookSlotForm.setAfternoonSlots(afternoonSlotDataLst);
bookSlotForm.setEveningSlots(eveningSlotDataLst);
LOG.debug("Before: bookSlotForm.getSelectedSlotStr() = " + bookSlotForm.getSelectedSlotStr());
if (null == bookSlotForm.getSelectedSlotStr()) {
final SlotData selectedSlot = bookSlotFacade.getSelectedSlot();
if (null != selectedSlot && null != selectedSlot.getSlotCode() && !"".equals(selectedSlot.getSlotCode())) {
bookSlotForm.setSelectedSlotStr(selectedSlot.getSlotCode());
// final CookieGenerator cookieGenerator = new CookieGenerator();
// cookieGenerator.setCookieName("collectionPointCookie");
// cookieGenerator.setCookieMaxAge(60*60*24*365*1000);
// cookieGenerator.addCookie(response, selectedSlot.getCollectionPointName());
final Cookie collectionPointCookie = new Cookie("collectionPointCookie", selectedSlot.getCollectionPointName());
collectionPointCookie.setMaxAge(60*60*24*365*1000);
collectionPointCookie.setPath("https://delhaize.be/bookslot");
response.addCookie(collectionPointCookie);
}
}
LOG.debug("After: bookSlotForm.getSelectedSlotStr() = " + bookSlotForm.getSelectedSlotStr());
LOG.debug("End: populateBookSlotForm");
return bookSlotForm;
}
}
最佳答案
不要将整个url放入setPath
调用中-只是要让cookie有效的网站中的路径即可:
collectionPointCookie.setPath("/bookslot");
另外,该路径必须包括设置cookie的servlet的路径-请参见docs