products.jsp
<a href=" <spring:url value="/product/${product.id}" /> "
class="btn btn-primary"> <span
class="glyphicon-info-sign glyphicon" /></span>Details</a>
productcontroller.java
@Controller
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/productlist")
public String productList(Map<String, Object> map, Principal principal){
map.put("productlist",productService.listProduct());
if(principal != null){
String name = principal.getName();
map.put("username", name);
}
return "products";
}
@RequestMapping("/product/{id}")
public String product(Map<String,Object> map,@PathVariable int productID){
map.put("product",productService.getProduct(productID));
return "product";
}
}
product.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="taglib.jsp" %>
<section class="container" >
<div class="row">
<div class="col-md-5">
<img src="<c:url value="/resource/productimages/${product.id}.png"></c:url>" alt="image" style = "width:100%"/>
</div>
<div class="col-md-5">
<h3>${product.name}</h3>
<p>${product.description}</p>
<p>
<strong>Item Code : </strong><span class="label label-warning">${product.id}</span>
</p>
</div>
</div>
</section>
genral.xml -apache tiles
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
"http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
<definition name="common" template="/WEB-INF/layout/classic.jsp">
<put-attribute name="footer" value="/WEB-INF/layout/footer.jsp" />
</definition>
<definition name="product" extends="common">
<put-attribute name="title" value="product details" />
<put-attribute name="body" value="/WEB-INF/views/jsp/product.jsp" />
<put-attribute name="current" value="product" />
</definition>
<definition name="checkout" extends="common">
<put-attribute name="title" value="checkout details" />
<put-attribute name="body" value="/WEB-INF/views/jsp/Checkout.jsp" />
<put-attribute name="current" value="checkout" />
</definition>
</tiles-definitions>
当我尝试通过{。1}}通过products.jsp访问任何产品时,即使我的jsp文件存在,即使我能访问的所有其他jsps,我也会收到此400错误,但仅在此网址中收到错误
答案 0 :(得分:1)
@PathVariable int productID
是问题所在。您的模板变量,例如id
,不等于您的变量名称,例如productID
。你应该调和这两者:
@PathVariable("id") int productID
或:
@RequestMapping("/product/{productID}")
要处理@PathVariable
注释,Spring MVC需要按名称查找匹配的URI模板变量。您可以在注释中指定它:
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
// implementation omitted
}
或者,如果URI模板变量名称与方法参数名称匹配,则可以省略该详细信息。只要没有调试信息编译代码,Spring MVC就会将方法参数名称与URI模板变量名称匹配:
@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
// implementation omitted
}