gpt4 book ai didi

java - addCookie 方法不会将 cookie 添加到 httpServletResponse

转载 作者:行者123 更新时间:2023-11-30 04:25:31 24 4
gpt4 key购买 nike

我必须在 Controller 中设置一个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

关于java - addCookie 方法不会将 cookie 添加到 httpServletResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15970958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com