@RequestMapping无法访问

时间:2014-12-13 01:55:29

标签: jsp spring-mvc spring-boot

我想我需要第二双眼睛,因为我对此感到难过。

我有一个促销页面(/促销),其链接指向(/ promotion / new)。此链接转到另一个页面,允许您创建新的促销。

我想要的是当我在 / promotion / new 时,我有一个"添加"按钮。 (与“添加”按钮的作用无关)。但是,该按钮找不到@RequestMapping。

PromoController.java

package com.mycompany.wpa.web;

// imports

@Controller
@RequestMapping(value="/promotion")
@SessionAttributes("promoBean")
public class PromotionController extends BaseApplicationController {

    private PromoBean promoBean;

    @RequestMapping(method = RequestMethod.GET)
    public String initPromotion(Model model) {
        System.out.println("initPromotion() method called!!!!");

        // initialize 
        promoBean = new PromoBean();
        promoBean.initialize();

        // send null because we do not want to be date specific.
        promoBean.setPromoMap( priceService.getActivePromotions(null) );

        model.addAttribute("promoBean", promoBean);

        return "promotionList";
    }

    @RequestMapping(value="/new", method = RequestMethod.GET)
    public String createPromotion(Model model) {
        System.out.println ("createPromotion() method called!!!!");

        // initialize 
        promoBean = new PromoBean();
        promoBean.initialize();

        model.addAttribute("promoBean", promoBean);
        return "promotion";
    }

    @RequestMapping(value="/new" params="addSku", method=RequestMethod.POST) 
    public String addSkuToPromo(@ModelAttribute PromoBean promoBean, Model model) {

        System.out.println ("addSku new method called");

        model.addAttribute("promoBean", promoBean);
        return "promotion"; 
    }
}

promotionList.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<t:wrapper>
    <jsp:attribute name="header">
        <%@ include file="../templates/header.jsp" %>
    </jsp:attribute>
    <jsp:attribute name="navigation">
        <%@ include file="../templates/navigation.jsp" %>
    </jsp:attribute>
    <jsp:attribute name="footer">
        <%@ include file="../templates/footer.jsp" %>       
    </jsp:attribute>

    <jsp:body>
        <h1>Promotions</h1>

        <div class="row">
            <div class="col-md-6">
                <c:url var="newUrl" value="/promotion/new" />
                <a class="btn btn-success" style="float: right;" href="${newUrl}">New</a>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>Promotion Type</th>
                            <th>Start Date</th>
                            <th>End Date</th>
                            <th>Edit</th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:forEach var="promo" items="${promoBean.promoMap}" varStatus="status">
                            <tr>
                                <td><span class="label label-primary">${promo.value.promoCategory}</span></td>
                                <td><fmt:formatDate value="${promo.value.promoStartDate}" pattern="yyyy-MM-dd" /></td>
                                <td><fmt:formatDate value="${promo.value.promoEndDate}" pattern="yyyy-MM-dd" /></td>
                                <td>
                                    <a href="promotion/edit/${promo.value.promoId}">
                                        <span class="glyphicon glyphicon glyphicon-edit"></span>
                                    </a>
                                </td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </table>
            </div>
        </div>
    </jsp:body>
</t:wrapper>

promotion.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<t:wrapper>
    <jsp:attribute name="header">
        <%@ include file="../templates/header.jsp" %>
    </jsp:attribute>
    <jsp:attribute name="navigation">
        <%@ include file="../templates/navigation.jsp" %>
    </jsp:attribute>
    <jsp:attribute name="footer">
        <%@ include file="../templates/footer.jsp" %>       
    </jsp:attribute>

    <jsp:body>
        <h1>Promotions</h1>

        <form:form commandName="promoBean" method="POST">
            <div class="row">
                <div class="col-md-3">
                    <div class="input-group">
                        <form:input path="promoSku" class="form-control" placeHolder="SKU" type="text"/>
                        <span class="input-group-addon">$</span>
                        <form:input path="promoValue" class="form-control" placeHolder="Value" type="text"/>
                        <span class="input-group-btn">
                            <button class="btn btn-success" name="addSku">
                                <span class="glyphicon glyphicon-plus"></span>
                            </button>
                        </span>
                    </div>
                </div>
            </div>
        </form:form>
    </jsp:body>
</t:wrapper>

当您进入 / promotion 时,您将进入促销列表。在此屏幕上,您点击链接&#34;新&#34;,然后它会转到 / promotion / new ,其中会显示您创建新促销的表单。

在此表单上,有一个名为&#34; addSku&#34;的按钮。目前什么也没做。我尝试将它连接到PromotionController.java,但除非我从addSkuToPromo()方法中删除 @ModelAttribute PromoBean promoBean ,否则它无法找到它。

我甚至通过从@RequestMapping中删除 value =&#34; / new&#34; 进行测试,然后将该按钮放在promotionList.jsp页面上,它就可以了!...所以我认为有一个映射问题,我只是不明白它是什么。

错误消息

出现意外错误(type = Bad Request,status = 400)。 object =&#39; promoBean&#39;验证失败。错误计数:2

0 个答案:

没有答案