【问题标题】:Getting 400 error in spring mvc在spring mvc中出现400错误
【发布时间】:2016-01-09 11:10:26
【问题描述】:

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 瓦片

<?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>

当我尝试通过 products.jsp 通过"&lt;spring:url value="/product/${product.id}" /&gt;" 访问任何产品时,即使我的 jsp 文件存在,甚至我能够访问的所有其他 jsp,我都会收到此 400 错误,但仅在此 url 中出现错误。

【问题讨论】:

    标签: java spring jsp spring-mvc


    【解决方案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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      相关资源
      最近更新 更多