为什么表单提交会打开新窗口/标签? [英] Why form submit opens new window/tab?

查看:139
本文介绍了为什么表单提交会打开新窗口/标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多问题如何在新窗口中打开表单结果,但是我面临着相反的问题。

I found a lot of questions "How to open form result in new window", but I'm facing opposite problem.

我有表格:

<form:form method="post" commandName="search">
    ...
    <input type="submit" value="Search" />
</form>

然后我有控制器处理方法

Then I have controller with handling method

@RequestMapping(value = "/search.form", method = RequestMethod.POST)
public String submit( @Valid final SearchObject searchObject, final BindingResult bindingResult ) {
    if ( bindingResult.hasErrors() ) return "forms/search";
    return "redirect:/A/result.form";
}

一切正常,除了在新窗口/标签中打开结果的事实。

Everything works fine, except the fact that result is opened in new window/tab.

我发现并且正在工作的解决方案是添加目标属性:

Solution that I found and is working is to add target attribute:

<form:form method="post" commandName="search" target="_self">

但是 _self 是默认的AFAIK。

But _self is default AFAIK.

我想知道为什么会发生这种情况。

I'm wondering why is that happening.

我检查生成的HTML并且在我看来好了:

I checked generated HTML and it seems to me ok:

<form id="search" action="/myApp/A/search.form" method="post">

编辑添加标题

Content-Language    en-US
Content-Length  0
Date    Thu, 22 Mar 2012 16:29:13 GMT
Location    http://localhost:8080/myApp/A/result.form
Server  Apache-Coyote/1.1
Request Headersview source
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/ *;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Cookie  JSESSIONID=E9ACB44C4ED3814814ABE7D96C90135C; GUEST_LANGUAGE_ID=cs_CZ; COOKIE_SUPPORT=true
Host    localhost:8080
Referer http://localhost:8080/myApp/A/search.form
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0
Response Headers From Cache
Content-Language    en-US
Content-Length  0
Date    Thu, 22 Mar 2012 16:29:13 GMT
Location    http://localhost:8080/myApp/A/result.form
Server  Apache-Coyote/1.1
Request Headers From Upload Stream
Content-Length  64
Content-Type    application/x-www-form-urlencoded


GET sessions.form

200 OK

localhost:8080

309 B

::1:8080


6ms
HeadersResponseCacheHTML
Content-Language    en-US
Content-Length  309
Content-Type    text/html;charset=utf-8
Date    Thu, 22 Mar 2012 16:29:13 GMT
Server  Apache-Coyote/1.1
Request Headersview source
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/ *;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Cookie  JSESSIONID=E9ACB44C4ED3814814ABE7D96C90135C; GUEST_LANGUAGE_ID=cs_CZ; COOKIE_SUPPORT=true
Host    localhost:8080
Referer http://localhost:8080/myApp/A/search.form
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0

编辑2 为结果添加方法.form

edit 2 added method for result.form

@RequestMapping(value = "/result.form", method = RequestMethod.GET)
public String result() {
    return "forms/result";
}

我的控制器类以:

@Controller
@RequestMapping(value = "/A")
public class AController {


推荐答案

问题出在基本标记。我故意在标签中 target =_ blank,可能是因为我从w3schools复制了这个例子: - /

Problem was in base tag. I had intentionally target="_blank" in tag, probably because I copied the example from w3schools :-/

这篇关于为什么表单提交会打开新窗口/标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆