简体   繁体   English

JSP 输入值到 Java 方法 -> HttpServletRequest 给出 NULL 值

[英]JSP Input value To Java Method -> HttpServletRequest gives NULL value

Hello Guys.大家好。 I have simple Storage page whichs display all Products from DB.我有一个简单的存储页面,它显示数据库中的所有产品 I set我设置to each of them with Unique name to change the Amount of product .给他们每个人用唯一的名字来改变产品的数量 When i want to catch this value in Java method its returns me null .当我想在 Java 方法中捕获这个值时,它返回给我 null Can you help me what i need to do to corretly catching value's in this text inputs?你能帮我在这个文本输入中正确地捕捉价值需要做些什么吗?

Controller: Controller:

@Controller
public class StoragePageController extends HttpServlet {

    @GET
    @RequestMapping(value = "/storage/subamount/{id}")
    public String substractTheAmountValue(@PathVariable("id") int id, Model model, HttpServletRequest request) {

        String amount_req = request.getParameter("amount_sub_" + id);
        System.out.println(amount_req);

        return null;
    }
}

JSP fragment: JSP 片段:

<c:set var="licznik" value="${recordStartCounter }" />
<div align="center">
    <table width="1000" border="0" cellpadding="6" cellspacing="2">
        <c:forEach var="u" items="${productList }">
            <c:set var="licznik" value="${licznik+1}" />
            <tr onmouseover="changeTrBg(this)" onmouseout="defaultTrBg(this)">
                <td align="right"><c:out value="${licznik }" /></td>
                <td align="left"><c:out value="${u.description }" /></td>
                <td align="left"><c:out value="${u.amount }" /></td>
                <td align="center"><input type="text" name="amount_sub_${licznik}" id="amount_sub_${licznik}"></td>
                <td align="center"><input type="button" value="Substract the value" onclick="window.location.href='${pageContext.request.contextPath}/storage/subamount/${licznik}'"/></td>
            </tr>
        </c:forEach>
    </table>

You should be having your API controller like the one given below given that your UI is posting the data to your API / Controller (assuming you are using the latest version of Spring Boot). You should be having your API controller like the one given below given that your UI is posting the data to your API / Controller (assuming you are using the latest version of Spring Boot). You have a @Get mapping which does not accept request payload in the body.您有一个@Get映射,它不接受正文中的请求有效负载。

@RestController
public class StoragePageController  {

@PostMapping(value = "/storage/subamount/{id}", produces = {"application/json"})
    public String substractTheAmountValue(@PathVariable("id") int id, Model model) {

        String amount_req = id;
        System.out.println(amount_req);

        return null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM