请问Spring+Thymeleaf+Html 怎么用Session实现分页 15
我做一个购物车,想实现添加10个物品时,自动分页,11个物品在最顶页面里,刚才的10个物品自动刷新到第2页,该怎么实现呢Spring@RequestMapping(pat...
我做一个购物车,想实现添加10个物品时,自动分页,11个物品在最顶页面里,刚才的10个物品自动刷新到第2页,该怎么实现呢
Spring
@RequestMapping(path = "/basket/shopping", method = RequestMethod.POST)
public String shopping(Model model, HttpSession session, ItemForm form) {
List<Item> shoppingBasket = new ArrayList<Item>();
if (session.getAttribute("shoppingBasket") != null) {
shoppingBasket = (List<Item>) session.getAttribute("shoppingBasket");
}
session.setAttribute("id", form.getId());
Item item = itemRepository.findOne(Integer.parseInt(form.getId()));
for (int i = 0; i < shoppingBasket.size(); i++) {
Item chek = shoppingBasket.get(i);
if (Integer.parseInt(form.getId()) == chek.getId()) {
return "basket/shopping_basket";
}
}
shoppingBasket.add(item);
session.setAttribute("shoppingBasket", shoppingBasket);
return "basket/shopping_basket";
}
Html
<tr th:each="item: ${session.shoppingBasket}">
<td><a th:href="@{/item/detail/{id}(id=${item.id})}"
th:text="${item.name}"></a></td>
<td th:id="'a' + ${item.id}">1</td>
<td><input type="button" value="个数-1"
th:attr="onclick='subtractNum(' +'a'+ ${item.id} + ');'" /> <input
type="button" value="个数+1"
th:attr="onclick='addNum(' +'a'+ ${item.id} + ');'" /> <input
type="hidden" th:value="${item.stock}" th:id="'c' + ${item.id}" /></td>
<td>
<form th:action="@{/basket/shopping/Delete/} + ${item.id}"
method="post">
<input type="hidden" /> <input type="submit" value="削除"
class="delete" />
</form>
</td>
</tr> 展开
Spring
@RequestMapping(path = "/basket/shopping", method = RequestMethod.POST)
public String shopping(Model model, HttpSession session, ItemForm form) {
List<Item> shoppingBasket = new ArrayList<Item>();
if (session.getAttribute("shoppingBasket") != null) {
shoppingBasket = (List<Item>) session.getAttribute("shoppingBasket");
}
session.setAttribute("id", form.getId());
Item item = itemRepository.findOne(Integer.parseInt(form.getId()));
for (int i = 0; i < shoppingBasket.size(); i++) {
Item chek = shoppingBasket.get(i);
if (Integer.parseInt(form.getId()) == chek.getId()) {
return "basket/shopping_basket";
}
}
shoppingBasket.add(item);
session.setAttribute("shoppingBasket", shoppingBasket);
return "basket/shopping_basket";
}
Html
<tr th:each="item: ${session.shoppingBasket}">
<td><a th:href="@{/item/detail/{id}(id=${item.id})}"
th:text="${item.name}"></a></td>
<td th:id="'a' + ${item.id}">1</td>
<td><input type="button" value="个数-1"
th:attr="onclick='subtractNum(' +'a'+ ${item.id} + ');'" /> <input
type="button" value="个数+1"
th:attr="onclick='addNum(' +'a'+ ${item.id} + ');'" /> <input
type="hidden" th:value="${item.stock}" th:id="'c' + ${item.id}" /></td>
<td>
<form th:action="@{/basket/shopping/Delete/} + ${item.id}"
method="post">
<input type="hidden" /> <input type="submit" value="削除"
class="delete" />
</form>
</td>
</tr> 展开
1个回答
展开全部
先看java部分
pom.xml 加入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!--支持
Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--模板引擎-->
pom.xml 加入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!--支持
Web 应用开发,包含 Tomcat 和 spring-mvc。 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--模板引擎-->
追问
我的是session读取的值,怎么把选中de值10个放到一个list里面,然后用html读取呢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询