简体   繁体   English

如何在JSP页面中添加请求参数?

[英]How to add parameters to request in JSP page?

I have to add to GET method two dates selected by users.我必须将用户选择的两个日期添加到GET方法中。
How can I do this, with the opportunity to extract it at servlet later?我怎样才能做到这一点,以后有机会在 servlet 中提取它?

Code snippet from jsp page:来自jsp页面的代码片段:

<p class="text-left">
  <strong><fmt:message key="apartments.arrival"/>:</strong> <input type="text" id="arrivaldate" name="arrivaldate"/> <br/>
  <strong><fmt:message key="apartments.depart"/>:</strong> <input type="text" id="departdate" name="departdate"/> <br/>
  <strong><fmt:message key="apartments.price"/>:</strong> ${apartment.price} <br/>
</p>
<a class="btn btn-primary btn-large btn-block"
                       href="<c:url value="/purchase?apartment=${apartment.id}"/>"><fmt:message key="button.book"/>
</a>

I have to take arrivaldate + departdate .我必须采取arrivaldate + departdate

The date is chosen with the JQuery date picker.日期是使用 JQuery 日期选择器选择的。

I want to do the following:我想做以下事情:

  • take two data values inputted by the user at JSP page取用户在 JSP 页面输入的两个数据值
  • set as a parameter to URL设置为 URL 的参数
  • at servlet side into doGet() extract these two values.在 servlet 端进入doGet()提取这两个值。 Better to avoid using the form, I want to put this request into doGet() .最好避免使用表单,我想将此请求放入doGet()

UPDATE:更新:

Here is the content of a full page - apartments.jsp这是一个完整页面的内容——apartments.jsp

How to solve this?如何解决这个问题?

Probably what you want is to overwrite the method doGet of your Servlet (if you have not done that already) and retrieve the GET parameters as可能你想要的是覆盖你的Servlet的方法doGet (如果你还没有这样做)并检索GET参数作为

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Stirng arrivaldate = request.getParameter("arrivaldate");
    Stirng departdate = request.getParameter("departdate");
    Stirng id = request.getParameter("apartment");
}

UPDATE: Looking at your apartments.jsp , I figured out the problem.更新:查看您的apartments.jsp ,我发现了问题所在。 You are not using any form tag and your button button.book has the GET parameters hardcoded.您没有使用任何表单标签,并且您的按钮button.book具有硬编码的GET参数。 That is why parameters other than apartment are not submitted.这就是为什么不提交apartment以外的参数的原因。

To fix it, you are going to have to wrap the content of lines 74 to 81 with a form tag, like this:要修复它,您将不得不使用form标记包装第74 to 81行的内容,如下所示:

<form action="/purchase" method="GET">
    <p class="text-left">
        <strong><fmt:message key="apartments.arrival"/>:</strong>
        <input type="text" id="arrivaldate" name="arrivaldate"/> <br/>
        <strong><fmt:message key="apartments.depart"/>:</strong>
        <input type="text" id="departdate" name="departdate"/> <br/>
        <strong><fmt:message key="apartments.price"/>:</strong> ${apartment.price} <br/>
    </p>
    <input type="hidden" id="apartment" name="apartment" value="${apartment.id}"/>
    <input class="btn btn-primary btn-large btn-block" type="submit" value="Submit" />
</form>

I think maybe your example is from a JavaScript page?我想也许你的例子来自一个 JavaScript 页面?

If you had the following url:如果您有以下网址:

blah.com/home?name=blah

You can use various languages(PHP, JavaScript, Java) to then do a getParam("name") which would return blah .您可以使用各种语言(PHP、JavaScript、Java)然后执行getParam("name")返回blah

You can also look into a rest service where you can do URL params using the following URL.您还可以查看休息服务,您可以在其中使用以下 URL 执行 URL 参数。

blah.com/home/blah

where you have the java code something like this你有这样的java代码

@GET
@Path("/home/{name}")
@Produces("text/html")
public String Test(@URLParam String name) {
   return name;
}

Answered in Java cause question has java tag.在 Java 中回答的原因问题有 java 标签。

just try this method to get your arrivaldate + departdate.只需尝试此方法即可获取您的到达日期 + 离开日期。

$("#from_date").datepicker({
    minDate   : 0,
    onSelect: function() {
        var minDate = $(this).datepicker('getDate');
        minDate.setDate(minDate.getDate());
        $("#to_date").datepicker( "option", "minDate", minDate);
    }
});

$("#to_date").datepicker({
    minDate: 0
});

fiddle demo to click here.小提琴演示点击这里。

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

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