簡體   English   中英

addCookie方法不會將cookie添加到httpServletResponse

[英]addCookie method doesn't adds the cookie to an httpServletResponse

我必須將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的路徑-請參閱文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM