JSP输入值到Java方法 -> HttpServletRequest返回NULL值

huangapple 未分类评论45阅读模式
英文:

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 = &quot;/storage/subamount/{id}&quot;)
	public String substractTheAmountValue(@PathVariable(&quot;id&quot;) int id, Model model, HttpServletRequest request) {
		
		String amount_req = request.getParameter(&quot;amount_sub_&quot; + id);
		System.out.println(amount_req);
		
		return null;
	}
}

JSP fragment :

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

答案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 = &quot;/storage/subamount/{id}&quot;, produces = {&quot;application/json&quot;})
    public String substractTheAmountValue(@PathVariable(&quot;id&quot;) int id, Model model) {

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

        return null;
    }
}

huangapple
  • 本文由 发表于 2020年4月3日 19:09:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/61010534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定