英文:
JSP Input value To Java Method -> HttpServletRequest gives NULL value
问题
*大家好。*
我有一个简单的存储页面,它从数据库中**显示所有产品**。
我为每个产品都设置了**<input type="text">**,并为其设置了**唯一名称**以更改**产品数量**。
当我想在Java方法中获取此值时,它**返回null**。
*你能帮我解决一下,正确地获取这些文本输入中的值吗?*
**控制器:**
@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 片段:**
<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>
英文:
Hello Guys.
I have simple Storage page whichs display all Products from DB.
I set <input type="text"> 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.
Can you help me what i need to do to corretly catching value's in this text inputs ?
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 :
<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>
答案1
得分: 0
你应该拥有像下面这样的 API 控制器,假设你的用户界面正在将数据发布到你的 API/控制器(假设你正在使用最新版本的 Spring Boot)。你有一个 @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;
}
}
英文:
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.
@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;
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论