Java 如何在请求对象中调用 setParameter?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9779670/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:34:28  来源:igfitidea点击:

How can I call setParameter in a request object?

javajspjstl

提问by Ender Wiggin

This is probably due to my misunderstanding and incomplete information of JSP and JSTL. I have a web page where I have input elements such as

这可能是由于我对JSP和JSTL的误解和信息不完整。我有一个网页,其中有输入元素,例如

<input name="elementID" value="${param.elementID}"/>

When I am trying to save the form, I check for that elementID and other elements to conform to certain constraints "numeric, less than XXX". I show an error message if they don't. All the parameters are saved and user does not need to type it again after fixing the error.

当我尝试保存表单时,我会检查该 elementID 和其他元素是否符合某些约束“数字,小于 XXX”。如果他们不这样做,我会显示一条错误消息。所有参数均已保存,修复错误后用户无需再次键入。

After saved, when I am redirecting to the same page for the object to be edited, I am looking a way to set the parameter like request.setParameter("elementID",..)Is there a way to do this ? However the only thing I can find is request.setAttribute.

保存后,当我重定向到要编辑的对象的同一页面时,我正在寻找一种设置参数request.setParameter("elementID",..)的方法,例如有没有办法做到这一点?然而,我唯一能找到的是request.setAttribute.

采纳答案by Aviram

HTTP responses does not support passing parameters.
JSP/Servelets allows you to either use request.setAttributeor session.setAttributefor that purpose. Both methods are available when processing the page you're redirecting to, So basically, you got it right...

HTTP 响应不支持传递参数。
JSP/Servelets 允许您使用request.setAttributesession.setAttribute用于该目的。在处理您重定向到的页面时,这两种方法都可用,所以基本上,您做对了...

Also, from what you describe, you may want to check client-side validation: don't submit the form until you're validating it using client-side scripting (javascript)

此外,根据您的描述,您可能需要检查客户端验证:在使用客户端脚本(javascript)对其进行验证之前,不要提交表单

回答by Carlos Jaime C. De Leon

After the servlet processes the form, (ie. saves the user input in the database), have the servlet forward (not redirect, because that would lose the request params) the request to the same jsp which contains the form. So there is no need to set the params since the servlet is just passing back the same request object.

在 servlet 处理表单后(即,将用户输入保存在数据库中),让 servlet 将请求转发(不是重定向,因为这会丢失请求参数)到包含表单的同一个 jsp。因此不需要设置参数,因为 servlet 只是传回相同的请求对象。

The jsp which contains the form should have inputs similar to this:

包含表单的 jsp 应该有类似这样的输入:

<form>
...
<input type="text" value="${elementid}"/>
...
</form>

The syntax ${varname} is EL. So if the elementid already has a value, it that textfield will contain that value. Alternatively if you have not used EL and/or JSTL, you use scriptlets (but that is highly unadvisable, EL and/or JSTL should be the way):

语法 ${varname} 是 EL。因此,如果 elementid 已经有一个值,那么该文本字段将包含该值。或者,如果您没有使用 EL 和/或 JSTL,则使用 scriptlet(但这是非常不可取的,应该使用 EL 和/或 JSTL):

<form>
...
<input type="text" value="<%= request.getParameter("elementid") %>"/>
...
</form>

回答by Colin D

I had to include <%@ page isELIgnored="false"%>to my jsp to allow code like ${elementid}to work

我必须包含<%@ page isELIgnored="false"%>到我的 jsp 中以允许代码${elementid}工作