【问题标题】:Why form submit opens new window/tab?为什么表单提交会打开新窗口/标签?
【发布时间】:2012-04-07 05:03:49
【问题描述】:

我发现了很多问题"How to open form result in new window",但我面临着相反的问题。

我有表格:

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

然后我有处理方法的控制器

@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";
}

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

我找到并正在工作的解决方案是添加目标属性:

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

_self 是默认的 AFAIK。

我想知道为什么会这样。

我检查了生成的 HTML,在我看来没问题:

<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

edit 2为 result.form 添加方法

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

我的控制器类开始于:

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

【问题讨论】:

  • 你在 siable Javascript 时有同样的行为吗?
  • 行为与禁用的 JS 相同。
  • 在不同的浏览器上呢?其他网站和链接可以吗?
  • 可能不相关,但我有一个错误,我的浏览器在所有表单上都这样做。重启解决了。
  • 在我所有的浏览器(IE9、FF11、Chrome17)中都可以使用。

标签: java html forms spring spring-mvc


【解决方案1】:

问题出在base tag。我故意在标签中添加了target="_blank",可能是因为我从 w3schools 复制了示例:-/

【讨论】:

  • 对于那些在&lt;form&gt; 标签中没有target="_blank" 的人,这肯定是解决方案。
猜你喜欢
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2012-10-12
  • 2018-05-19
  • 2014-08-27
  • 1970-01-01
  • 2014-11-05
  • 2012-11-27
相关资源
最近更新 更多