我在两个页面中测试使用EL结果发现 Form表单action提交到另一页面和利用 <A> 标签时。。requestScope无效
不能在另一页面中使用 requestScope 获得值 ,,而 sessionScope 和 applicationScope 都可以用。当我利用 request.getRequestDispatcher("showEl.jsp").forward(request,response); 这个进行页面跳转时就有可以用了。。这是为什么 ??以下是代码 :
<body>
<%
request.setAttribute("request","在request中设置的值");
session.setAttribute("session","在session中设置的值 ");
application.setAttribute("application","在application中设置的值 ");
%>
<form action="Jsp/showEl.jsp">
<tr>
<td>用户名:</td>
<td><input type="text" name="txtName"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="txtPwd"/></td>
</tr>
<tr><td colspan="2"><input type="submit" value="测试 El——JSTL"/></td></tr>
</form>
<a href="Jsp/showEl.jsp?request=123">测试利用超连接传值</a>
<%
// request.getRequestDispatcher("showEl.jsp").forward(request,response); 不使用时不能传值 ,使用后可传值
%>
</body>
</html>
另一个页码的代码:<table>
<tr>
<td>在这里测试EL——JSTL,显示传过来的值</td>
</tr>
<tr>
<td>${param.txtName }</td>
</tr>
<tr>
<td>${param.txtPwd }</td>
</tr>
<tr>
<td>${requestScope.req}</td>
</tr>
<tr>
<td>${sessionScope.session}</td>
</tr>
<tr>
<td>${applicationScope.application}</td>
</tr>
</table>
页面显示图: 在这里测试EL——JSTL,显示传过来的值
在session中设置的值
在application中设置的值
问题: 为什么 表单的action 和 a 标签进行页面跳时无效??难道这两个是重定向的??请高手说明一下。
不能在另一页面中使用 requestScope 获得值 ,,而 sessionScope 和 applicationScope 都可以用。当我利用 request.getRequestDispatcher("showEl.jsp").forward(request,response); 这个进行页面跳转时就有可以用了。。这是为什么 ??以下是代码 :
<body>
<%
request.setAttribute("request","在request中设置的值");
session.setAttribute("session","在session中设置的值 ");
application.setAttribute("application","在application中设置的值 ");
%>
<form action="Jsp/showEl.jsp">
<tr>
<td>用户名:</td>
<td><input type="text" name="txtName"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="txtPwd"/></td>
</tr>
<tr><td colspan="2"><input type="submit" value="测试 El——JSTL"/></td></tr>
</form>
<a href="Jsp/showEl.jsp?request=123">测试利用超连接传值</a>
<%
// request.getRequestDispatcher("showEl.jsp").forward(request,response); 不使用时不能传值 ,使用后可传值
%>
</body>
</html>
另一个页码的代码:<table>
<tr>
<td>在这里测试EL——JSTL,显示传过来的值</td>
</tr>
<tr>
<td>${param.txtName }</td>
</tr>
<tr>
<td>${param.txtPwd }</td>
</tr>
<tr>
<td>${requestScope.req}</td>
</tr>
<tr>
<td>${sessionScope.session}</td>
</tr>
<tr>
<td>${applicationScope.application}</td>
</tr>
</table>
页面显示图: 在这里测试EL——JSTL,显示传过来的值
在session中设置的值
在application中设置的值
问题: 为什么 表单的action 和 a 标签进行页面跳时无效??难道这两个是重定向的??请高手说明一下。
解决方案 »
- <bean:define 标签使用问题,请问如果对应的bean是null,如何返回一个空的bean?
- ssh tomcat启动出错 nested exception is org.hibernate.Hibernat
- GXT treepanel初始化选中问题
- java发送Email如何指定发送内容呢?
- spring2 集成 hibernate3 getHibernateTemplate() 为空
- 求助:关于JSP处理ResultSet的解决办法
- servlet是什么模式的?
- resin怎样配置oracle的数据源和连接池?
- 急问一个问题!关于servlet的!急!急!
- linux下解密AES失败的问题
- 关于json的问题
- 我搭建的SSH框架分页连接池为什么会耗尽???
应该来说action是可以,如果你用了struts的话,action='*.do'跳到Action中,在mapping.forword("name");
那么在页面上是可以拿到的。
<a></a>标签就是重定向肯定拿不到
如果有错的话再一起讨论,楼主可以百度下
应该来说action是可以,如果你用了struts的话,action='*.do'跳到Action中,在mapping.forword("name");
那么在页面上是可以拿到的。
<a></a>标签就是重定向肯定拿不到
如果有错的话再一起讨论,楼主可以百度下
<form></form>应该是可以的啊
你的<td>${requestScope.req}</td>应该是<td>${requestScope.request}</td>吧?
form表单中的值是通过request.getParameter获取的,只接受java.lang.String,而EL表达式读取的应该是Attribute中的对象
${requestScope.req} 这个应该是拿不到的,以为你在JSP页面上 放进的那个请求 和你 想拿值的那个JSP 不是一个请求
也就是说 你第一个页面 上用这个表达式 是能取到的 而 到了第2个 是取不到的 2个request 不是同一个
办法就是 把这个 request.setAttribute("request","在request中设置的值"); 放进action中
或者 页面用转发
至于放在request中的数据。如果通过表单提交肯定可以,因为发起了一次请求。如果通过连接跳转过去便得不到。如果是<a href="Jsp/showEl.jsp?request=123">测试利用超连接传值</a>这种形式也通过${param.request}来获取值
而你用request.getRequestDispatcher("showEl.jsp").forward(request,response);把request和response都传递到了新的页面,因此获取。
request.setAttribute("request","request值");
request.getRequestDispatcher("showEl.jsp").forward(request,response);另外一边就能调用 el 表达式了${requestScope.request}
值得到值的原因,一般来说。。在先在 servlet中设置attribute再在通过request.getRequestDispatch()跳转到页面中通过 requestScope获得的。而我是在两个页面中。。所以有点误差 感谢大家的回答。。对我有用。。谢谢。。