绑定商品id

在首页的商品列表中,我们可以将商品 id 绑定到 a 标签中:

1
2

<d<a href="goods/${product.id}"><img src="xxxx"/></a></div>

提供 rest 服务

Service

1
2
3
4
5
6
7
8
9
10

@Override
public ResponseJsonResult getProductDetailById(Integer id) {

Product product = productMapper.selectByPrimaryKey(id);
ResponseJsonResult responseJsonResult = new ResponseJsonResult();
responseJsonResult.setObj(product);
return responseJsonResult;

}

Controller

1
2
3
4
5
6
7

@RequestMapping("product/detail/{productId}")
@ResponseBody
public ResponseJsonResult getProductDetail(@PathVariable("productId") Integer productId){
ResponseJsonResult responseJsonResult = productService.getProductDetailById(productId);
return responseJsonResult;
}
rest服务
rest服务

portal

Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

@Override
public Product getDetailById(Integer id) {

try {
String jsonResult = HttpClientUtil.httpGet(REST_URL + REST_PRODUCT_DETAIL + id);
ResponseJsonResult responseJsonResult = GsonUtils.fromJson(ResponseJsonResult.class, jsonResult);
Product product = (Product) responseJsonResult.getObj();
return product;
} catch (IOException e) {
e.printStackTrace();
}

return null;
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


@Controller
public class ProductController {

@Autowired
ProductService productService;

@RequestMapping("goods/{productId}")
public String index(ModelMap modelMap,@PathVariable("productId") Integer productId){
Product product = productService.getDetailById(productId);
modelMap.addAttribute("product",product);
return "detail";
}


}

Template