問題描述
內容類型是“應用程序/x‑www‑form‑urlencoded”。預期的“文本/x‑gwt‑rpc” (Content‑Type was 'application/x‑www‑form‑urlencoded'. Expected 'text/x‑gwt‑rpc')
我正在嘗試將 Spring Security 與 GWT 一起使用,但我在一個又一個問題中遇到了問題,因為似乎沒有什麼能像它實際應該那樣工作。目前我正在嘗試將所有內容連接到我的自定義登錄頁面。為此,我有一個 FormPanel
,它的初始化如下:
public LoginViewImpl() {
initWidget(uiBinder.createAndBindUi(this));
this.frmLogin.setMethod(FormPanel.METHOD_POST);
this.frmLogin.setAction("/app/login");
this.frmLogin.setEncoding("text/x‑gwt‑rpc");
this.txtUserID.setName("username");
this.txtPassword.setName("password");
}
但是當我點擊登錄按鈕時,我得到:
javax.servlet.ServletException: Content‑Type was 'application/x‑www‑form‑urlencoded'. Expected 'text/x‑gwt‑rpc'.
at com.google.gwt.user.server.rpc.RPCServletUtils.checkContentTypeIgnoreCase(RPCServletUtils.java:477)
at com.google.gwt.user.server.rpc.RPCServletUtils.readContent(RPCServletUtils.java:209)
at com.google.gwt.user.server.rpc.RPCServletUtils.readContentAsGwtRpc(RPCServletUtils.java:252)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.readContent(AbstractRemoteServiceServlet.java:182)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:364)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
...
我沒有不知道如何避免這種情況。任何幫助表示讚賞。
我還注意到請求的 Content‑Type
標頭是 application/x‑www‑form‑urlencoded
而不是 text/ x‑gwt‑rpc
就像我設置的一樣。
這部分的HTML說:
<form target="FormPanel_app_2" method="post" action="/app/login" enctype="text/x‑gwt‑rpc">
<!‑‑ ... ‑‑>
</form>
參考解法
方法 1:
In GWT, text/x‑gwt‑rpc
is used when GWT RPC is used as the server communication mode. In your example, you are submitting a normal HTML form (which is unaware of text/x‑gwt‑rpc as it is not a standard content type).
There are 2 options:
- If you intend to use HTML form, you should be exposing a separate servlet to handle application/x‑www‑form‑urlencoded form submission.
- If you want to stick to GWT RPC, you can define some LoginService which captures username & password from fields and send the data to RPC servlet using GWT RPC semantics. (http://www.gwtproject.org/doc/latest/tutorial/RPC.html)
(by Stefan Falk、Mohit)