2013-05-13 44 views
1

我对网络应用程序和播放框架都很陌生,我提出的问题可能很幼稚。但是,我搜索了一会儿,找不到一个好的答案,请耐心等待。查询字符串从提交按钮中丢失

首先,我的平台是play-2.1.1 + java 1.6 + OS X 10.8.3。

问题的一个简短版本:我有一个action =“hello?id = 100”的提交按钮。但是,当我单击按钮时,发送的请求似乎是hello?而不是hello?id=100。此请求的操作需要参数id,所以我得到一个错误hello?

以下是完整的设置。

的conf /路线:

GET /       controllers.Application.index() 
GET  /hello      controllers.Application.hello(id: Long) 

的app/controllers/Application.java:

package controllers; 

import play.*; 
import play.mvc.*; 

import views.html.*; 

public class Application extends Controller { 

    public static Result index() { 
     return ok(index.render()); 
    } 

    public static Result hello(Long id) { 
     return ok("Hello, No. " + id); 
    } 

} 

应用程序/视图/ index.scala.html

This is an index page. 

<form name="helloButton" action="hello?id=100" method="get"> 
    <input type="submit" value="Hello"> 
</form> 

根据播放文档,id应该从查询字符串?id=100中提取。然而,当我点击提交按钮,请求成为hello?而不是hello?id=100,因此我得到这样的错误:“GET /你好”

对于请求[缺少参数:id]

有人可以告诉我为什么会发生这种情况吗?先谢谢你。

回答

1

问题是与形式:

<form name="helloButton" action="hello?id=100" method="get"> 
    <input type="submit" value="Hello"> 
</form> 

由于形式方法设置得到的,它改变了查询字符串。 method="get"告诉浏览器将表单的内容添加到查询字符串中,这意味着当前查询字符串被删除。

你可以在这样的形式添加的ID作为隐藏字段:

<form name="helloButton" action="hello" method="get"> 
    <input type="hidden" name="id" value="100"> 
    <input type="submit" value="Hello"> 
</form> 

这将告诉浏览器的隐藏字段添加到导致hello?id=100查询字符串。或者,您可以将表单方法更改为POST。

+0

谢谢,山姆。问题解决了。我去了解决方案。 – JBT 2013-05-13 17:49:09