简体   繁体   English

输入转发时如何在URL中附加查询参数?

[英]How to append query params in URL in case of input forward?

I am having trouble in maintaining the value of the query params which was sent in URL and lost during input forward due to validation error. 我在维护查询参数的值时遇到麻烦,该查询参数在URL中发送并且由于验证错误而在输入转发期间丢失。

I have a link link this 我有一个链接链接

<a href="/somelink/testpage?param1=value1&param2=value2" id="test">Test</a>

Clicking this link opens a new page which has a form with two hidden fields (which takes the value from these params). 单击此链接将打开一个新页面,该页面的表单带有两个隐藏字段(从这些参数中获取值)。 This page has two buttons save and cancel . 此页面有两个按钮, 保存取消 My problem is 我的问题是

  1. I click the link, the input page corresponding to this action is test.jsp . 我单击链接,此操作对应的输入页面是test.jsp This renders the page.(ex the URL is https:host/somelink/testpage?param1=value1&param2=value2 ). 这将呈现页面。(例如URL是https:host/somelink/testpage?param1=value1&param2=value2 )。

  2. I fill the form and click Save button which submits the form. 我填写表单,然后单击“保存”按钮以提交表单。 In the Action class I get the params and set in request and do validation . 在Action类中,获取参数并在request中进行设置并进行验证。 Due to validation error, control goes to input page (but without query params in url). 由于验证错误,控制权转到输入页面(但url中没有查询参数)。 Now the input page has the error message and the URL is https:host/somelink/testpage 现在输入页面显示错误消息,URL为https:host/somelink/testpage

  3. Now in the test.jsp , from the request the hidden fields in form are filled. 现在在test.jsp ,从请求中填充表单中的隐藏字段。

  4. The error is shown in the input page. 错误显示在输入页面中。 Now if I click the save button again control goes to the Action class with form being submitted. 现在,如果我再次单击“保存”按钮,则控件将转到要提交表单的Action类。 In this case I can access those params (as form is submitted) 在这种情况下,我可以访问这些参数(提交表单时)

  5. But there is a case where in the input page with error, if the user presses Enter from address bar the request will go to action class (Remember when forward was done to the input page, the query params are not set in url, so now if request is sent there is no way to get the value of the param as its not in url and the form is not submitted.) 但是在某些情况下,如果输入页面有错误,如果用户按地址栏中的Enter键 ,则请求将进入操作类(请记住,当转发到输入页面时,查询参数未在url中设置,因此现在如果发送了请求,则无法获取参数的值,因为它不在url中,并且未提交表单。)

Is there any way I can maintain the value of the params? 有什么方法可以保持参数的价值? I am not able to append it to URL as its a input forward. 我无法将其附加到URL作为向前的输入。 Only way I found to retain the values of params is add in session and get from session. 我发现保留params值的唯一方法是在session中添加并从session中获取。 But I am not sure if its advisable. 但是我不确定它是否合适。

When you submit the form you didn't pass parameters in the url. 提交表单时,您没有在url中传递参数。 To make it working you should include parameters in the action attribute of the form tag. 为了使其正常工作,您应该在form标记的action属性中包括参数。

Like 喜欢

<html:form action="/somelink/testpage?param1=value1&param2=value2">

or submit the form without parameters in the URL, but to pass parameters you need to add html:hidden fields to the form, that will hold the values. 或提交网址中不含参数的表单,但要传递参数,您需要向表单添加html:hidden字段,以保存值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM