我必须将一个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;
}
}
答案 0 :(得分:2)
不要将整个网址放入setPath
来电 - 只是网站中您希望Cookie有效的路径:
collectionPointCookie.setPath("/bookslot");
此外,路径必须包含设置cookie的servlet的路径 - 请参阅docs