saurabh kumar
saurabh kumar

Reputation: 164

Getting 400 error in spring mvc

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>

when i am trying to access any product through "<spring:url value="/product/${product.id}" />" through products.jsp i am getting this 400 error even though my jsp file exist and even all other jsps i am able access ,but getting error only in this url.

Upvotes: 2

Views: 470

Answers (1)

Ali Dehghani
Ali Dehghani

Reputation: 48193

@PathVariable int productID is the problem. Your template variable, e.g. id, is not equal to your variable name, e.g. productID. You should reconcile those two, either:

@PathVariable("id") int productID

or:

@RequestMapping("/product/{productID}")

To process the @PathVariable annotation, Spring MVC needs to find the matching URI template variable by name. You can specify it in the annotation:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable("ownerId") String theOwner, Model model) {
    // implementation omitted
}

Or if the URI template variable name matches the method argument name you can omit that detail. As long as your code is not compiled without debugging information, Spring MVC will match the method argument name to the URI template variable name:

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
public String findOwner(@PathVariable String ownerId, Model model) {
    // implementation omitted
}

Upvotes: 1

Related Questions