从@Pathvariable获取日期值而不是long

时间:2015-08-14 13:13:50

标签: jsp spring-mvc

我正在尝试使用商品ID从商品列表中点击特定商品,然后使用@pathVAriable将其传递给我的控制器方法,然后在数据库中查询该商品并返回包含商品文字集的新视图,sot用户可以编辑报价,但是当我记录@path变量的值时,它返回的是日期值,而不是实际的offerId。

JSP:

bool validate(char minimum, char maximum, char testValue)
{
    return validate(toupper(minimum), toupper(maximum), toupper(testValue));
}

控制器:

 %@include file="includes/header.jsp"%>
<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">List of current offers</h3>
    </div>
    <div class="panel-body">
        <c:forEach var="offer" items="${offers}">


            <dl class="dl-horizontal">

                <dt>Name</dt>
                <dd>
                    <c:out value="${offer.user.name}" />
                </dd>
                <dt>email</dt>
                <dd>
                    <c:out value="${offer.user.email}" />
                </dd>
                <dt>Text</dt>
                <dd>
                    <c:out value="${offer.text}" />
                </dd>
            </dl>
            <a class="btn btn-link"
                href="<c:url value ='/offers/${offer.id}/send-email'/>">Send an
                email</a>
            <a class="btn btn-link"
                href="<c:url value ='/offers/${offer.id}/edit-offer'/>">Edit
                offer</a>
            <a class="btn btn-link"
                href="<c:url value ='/offers/${offer.id}/remove-offer'/>">Remove
                offer</a>

        </c:forEach>

    </div>
    <%@include file="includes/footer.jsp"%>

控制台:

@RequestMapping(value = "/{id}/edit-offer", method = RequestMethod.GET)
    public String editOffer(@PathVariable("id") long id,Model model) {
        System.out.print("Path variable is ###############################" + id);

        Offer offer = offerService.findByOne(id);
        EditOfferForm form = new EditOfferForm();
        form.setText(offer.getText());

        model.addAttribute("offer",form);
        return "edit-offer";

    }

2 个答案:

答案 0 :(得分:0)

实际上,您的路径ID已正确映射:2

您的控制台将您的ID 2与错误的时间戳联系起来:2015-08-12 19:33:06.577

我的猜测是你的问题来自于offerService.findByOne(id)返回null的事实,这就是你有NullPointerException的原因。请注意堆栈跟踪第81行:

java.lang.NullPointerException: null
    at com.niass.offer.controller.OfferController.editOffer(OfferController.java:81)

Java是一种强类型语言,如果传递了Date而不是long,它会抛出类型不匹配的异常。

答案 1 :(得分:0)

First of all, i would like to thank both, @Alin & @ Sanjay, the problem was, when i created my OfferRepoImpl method, it initially returned null, because the method was auto generated and i forgot to make it return the offer it found, after writing the query method.

Before correction was made :

    @Override
    public Offer findByOne(long id) {
        Criteria cr = session().createCriteria(Offer.class);
        cr.createAlias("user", "u");
        cr.add(Restrictions.eq("u.id", id));
        cr.add(Restrictions.eq("u.isEnable", true));
        return null;
    }

After making correction:

    @Override
    public Offer findByOne(long id) {
        Criteria cr = session().createCriteria(Offer.class);
        cr.createAlias("user", "u");
        cr.add(Restrictions.eq("u.id", id));
        cr.add(Restrictions.eq("u.isEnable", true));
        return (Offer) cr.uniqueResult();
    }