在JSP中提交表单后,自动过渡到下一页

时间:2018-12-11 17:41:52

标签: java spring jsp servlets java-ee

我有一个JSP文件。我必须给控制器2个参数。一个来自输入表单,另一个来自下拉列表(当前所选客户的ID)。

当我在'form:form'标记中具有'action'属性时-URL看起来不错(http://localhost:8080/myapp/customer/nextPage?parFromDropdownList=1&parFromInput=34),但是控制器没有从下拉列表中获取参数,并且应用返回了消息“ Required int参数'paramFromDropdownList'不存在”。我怀疑首先调用“ nextPage”页面,然后将参数传递给URL。

当我没有'action'属性时,一切都可以正常工作,但是我需要单击两次:首先是在Submit按钮上,然后是在带有重定向的'anchor'上。一键就能做到吗?

JSP文件

    <select name="parFromDropdownList">
        <option value="">Change customer</option>
        <c:forEach var="tempCustomer" items="${customers}">
            <option value="${tempCustomer.getId()}">${tempCustomer.getSurname()}</option>
        </c:forEach>
    </select>

    <input name="parFromInput" />
    <input type="submit" value="Submit" />

</form:form>

<c:url var="nextPage" value="/customer/nextPage">
    <c:param name="paramFromDropdownList"
        value='<%=request.getParameter("parFromDropdownList")%>'></c:param>
    <c:param name="paramFromInput"
        value='<%=request.getParameter("parFromInput")%>'></c:param>
</c:url>

<a href="${nextPage}">Next page</a>

控制器

@RequestMapping("/nextPage")
public String dane(@RequestParam("paramFromDropdownList") int theId, @RequestParam("paramFromInput") int tegos, Model theModel, Model theModel2) {
    Customer theCustomer = customerService.getCustomer(theId);

    theModel.addAttribute("customer2", theCustomer);

    theModel2.addAttribute("mass", tegos);

    return "resPage";
}

0 个答案:

没有答案