Java 使用 Request.setAttribute 传递值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21091635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass Value by Using Request.setAttribute
提问by newbieinjavaversion2
I am trying to get attribute of spanId and set Attribute by using request. Then I want to pass the output. Although the first input is has value, it still return me null.
我试图通过使用请求来获取 spanId 的属性并设置属性。然后我想传递输出。虽然第一个输入有值,但它仍然返回 null。
Below are my codes. Help will be appreciate! :)
下面是我的代码。帮助将不胜感激!:)
<input id="spanId" name="spanId">
<%
String spanId = request.getParameter("spanId");
request.setAttribute("spanId",spanId);
%>
<%= request.getParameter("spanId") %>">
回答by Naveen Ramawat
Use the getAttribute
method instead of getParameter()
like
使用getAttribute
方法而不是getParameter()
像
<%= request.getAttribute("spanId") %>">
回答by Swapnil Sawant
Just think if you are setting an attribute to request object how you can get it? by using getAttribute.... String spanId = request.getAttribute("spanId").
试想一下,如果您正在设置一个属性来请求对象,您如何获得它?通过使用 getAttribute.... String spanId = request.getAttribute("spanId")。
Also request attribute has request scope (scope lasts only till 1 request).
请求属性也具有请求范围(范围仅持续到 1 个请求)。
More on this over here http://docs.oracle.com/javaee/1.4/api/javax/servlet/jsp/JspContext.html
更多关于这里http://docs.oracle.com/javaee/1.4/api/javax/servlet/jsp/JspContext.html
Avoid using scripting elements in your JSP.. remember you are tightly coupling your presentation and business logic.
避免在 JSP 中使用脚本元素。请记住,您将表示和业务逻辑紧密耦合在一起。