java - 在 spring mvc 中获取 400 错误

标签 java spring jsp spring-mvc

产品.jsp

    <a href=" <spring:url value="/product/${product.id}" /> "
    class="btn btn-primary"> <span
  class="glyphicon-info-sign glyphicon" /></span>Details</a>

产品 Controller .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";
    }

}

产品.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>

当我尝试通过 "<spring:url value="/product/${product.id}" />" 访问任何产品时通过 products.jsp 我收到这个 400 错误,即使我的 jsp 文件存在,甚至我可以访问所有其他 jsp,但只在这个 url 中收到错误。

最佳答案

@PathVariable int productID 是问题所在。您的模板变量,例如id,不等于您的变量名,例如产品编号。您应该调和这两者:

@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
}

关于java - 在 spring mvc 中获取 400 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34692721/

相关文章:

java - 为什么我的 java lambda 表达式不能工作,而它的命令式风格可以正常工作?

java - Kerberos 协议(protocol)中的第 7 条消息和票证验证

java - Java 中 Google App Engine 中的 SQL 查询?

java - 如何从源代码中找出具体的jsp技术?

java - 文件正在下载但里面没有任何内容

java - 这种情况的抽象语法树?

java - 避免 Hibernate LazyInitializationExceptions 的架构

java - Spring 配置的 Hibernate 方言问题

java - 从 Spring jdbc 获取 java.sql.connection : getJdbcTemplate()

java - JPA ManyToMany 映射问题(无法将同一实体映射到另一个实体)