2
回答
新手菜鸟求助各位大神帮帮忙 看看代码错在呢
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
@RequestMapping("/enter/{id}")
public String enter(@PathVariable Long id, ModelMap model,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException {
Product product = productService.getById(id);
List<Product> heatList = productService.find("heat", -1l)
.setMaxResults(10).list();
CookieTools cookieTools = new CookieTools(request, response);
String ids = cookieTools.getCookieValue("SHOPCAT");
model.addAttribute("product", product);
model.addAttribute("productTypeList", findProductType());
model.addAttribute("heatList", heatList);
model.addAttribute("shopCount", ids.split("-").length);
return "/shop/detail";

}



@RequestMapping("/cookie/add/{id}")
public String add(@PathVariable Long id, HttpServletRequest request,
HttpServletResponse response) {
CookieTools cookieTools = new CookieTools(request, response);
boolean result = false;
int count = 0;
try {
String cookieValue = cookieTools.getCookieValue("SHOPCAT");
if (cookieValue != null && !"".equals(cookieValue)) {
String[] ids = cookieValue.split("-");
boolean findProductId = true;
for (String productId : ids) {
if (productId.equals(id.toString())) {
findProductId = false;
break;
}
}
if (findProductId) {
cookieValue = cookieValue + "-" + id;
count = ids.length + 1;
}
} else {
cookieValue = id + "";
count = 1;
}
cookieTools.saveCookieInfo("SHOPCAT", cookieValue);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.setCharacterEncoding("utf-8");
response.getWriter().print(result + "-" + count);
response.getWriter().close();
response.getWriter().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

我在添加时获取cookie的值没有任何问题,可是却发现本地并没有找到,可我再一次进行添加时却可以查看到值 

而在enter()方法也查询不到值 


举报
sbwvtibi
发帖于5年前 2回/151阅
共有2个答案 最后回答: 5年前
@RequestMapping("/enter/{id}")
	public String enter(@PathVariable Long id, ModelMap model,
			HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		Product product = productService.getById(id);
		List<Product> heatList = productService.find("heat", -1l)
				.setMaxResults(10).list();
		CookieTools cookieTools = new CookieTools(request, response);
		String ids = cookieTools.getCookieValue("SHOPCAT");
		model.addAttribute("product", product);
		model.addAttribute("productTypeList", findProductType());
		model.addAttribute("heatList", heatList);
		model.addAttribute("shopCount", ids.split("-").length);
		return "/shop/detail";
	}
@RequestMapping("/cookie/add/{id}")
 public String add(@PathVariable Long id, HttpServletRequest request,
 HttpServletResponse response) {
 CookieTools cookieTools = new CookieTools(request, response);
 boolean result = false;
 int count = 0;
 try {
 String cookieValue = cookieTools.getCookieValue("SHOPCAT");
 if (cookieValue != null && !"".equals(cookieValue)) {
 String[] ids = cookieValue.split("-");
 boolean findProductId = true;
 for (String productId : ids) {
 if (productId.equals(id.toString())) {
 findProductId = false;
 break;
 }
 }
 if (findProductId) {
 cookieValue = cookieValue + "-" + id;
 count = ids.length + 1;
 }
 } else {
 cookieValue = id + "";
 count = 1;
 }
 cookieTools.saveCookieInfo("SHOPCAT", cookieValue);
 result = true;
 } catch (Exception e) {
 e.printStackTrace();
 } finally {
 try {
 response.setCharacterEncoding("utf-8");
 response.getWriter().print(result + "-" + count);
 response.getWriter().close();
 response.getWriter().flush();
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }
 return null;
 }
顶部