【问题标题】:s:form overflowing to next jsp after form submits:表单提交后表单溢出到下一个jsp
【发布时间】:2013-07-29 11:50:52
【问题描述】:

我在第一个 jsp 中有一个<s:form> 标签,当我通过 struts 操作提交表单并显示另一个 jsp 时,表单标签似乎也扩展到了第二个 jsp。 (如在萤火虫中观察到的)。我在这些 jsps 中有一些具有相同 id 的元素,并且以这种方式将它们组合在一起给我带来了很多问题。此外,第二个 jsp 的 <head> 部分将被忽略。

这是page1.jsp的基本结构:

<html>
<head>
    <%@ taglib uri="/struts-tags" prefix="s" %> 
    <script type="text/javascript" src="../js/page1.js"></script>
</head>
<body>
    <s:form name="form1" id="form1" action="" method="post" namespace="">
        <div data-role="page" id="firstPage">
            <div id="xyz" class = "xyz"></div>
            .....//other elements
        </div>
    </s:form>
    <script type="text/javascript">
        .... // do stuff and submit "form1" to process.action
    </script>
</body>
</html>

page2.jsp的基本结构:

<html>
<head>
    <script type="text/javascript" src="../js/page2.js"></script>
</head>
<body>
   <div data-role="page" id="secondPage">
       <div id="xyz" class = "xyz"></div>
       .....//other elements
   </div>
</body>
</html>

在 firebug 中看到的 HTML:

<html>
<head>
    <%@ taglib uri="/struts-tags" prefix="s" %> 
    <script type="text/javascript" src="../js/page1.js"></script>
</head>
<body>
  <form name="form1" id="form1" method="post" action="process.action">
      <div data-role="page" id="firstPage">
          <div id="xyz" class = "xyz"></div>
          .....//other elements
      </div>
      <div data-role="page" data-external-page="true" id="secondPage">
          <div id="xyz" class = "xyz"></div>
          .....//other elements
      </div>
  <form>
  <script type="text/javascript">
      .... // do stuff and submit "form1" to process.action
  </script>
</body>
</html>

因此,这导致相同的 html 表单具有 2 个 id 为“xyz”的元素,以及在两个 jsps 中具有相同值的其他 id。 我做错了什么,或者我该如何预防?

编辑:我看到 data-external-page="true" 添加到呈现的 html 中的 div "secondPage" 中。我试图在 page2.jsp 中将其显式设置为 false,但这也有效。

此外,令人耳目一新的作品。虽然重新加载时不存在填充页面所需的数据,但这个特殊问题在重新加载时消失了。

编辑:struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="location" namespace="/location">
        <action name="start" class="StartAction">
            <result name="success">/WEB-INF/templates/location/page1.jsp</result>
        </action>

        <action name="process" class="ProcessAction">
            <result name="success">/WEB-INF/templates/location/page2.jsp</result>
        </action>

    </package>
</struts>

ProcessAction.java:

public class ProcessAction{
    private String xyz;

    //other elements
    //getters and setters

    @Override
    public String process() {
       //get data from DB and set it to xyz and other elements, 
       //whose corresponding elements are in page2.jsp

       return SUCCESS;
    }
}

【问题讨论】:

  • 您需要在描述中更加明确:如果您已提交表单并且操作正在触发,则这是一个新请求,并且第一页已消失。您要么没有告诉我们有关 JavaScript 或您的主题的信息,要么...?如果您使用默认主题,您的 HTML 将无效,当然,您不能在表格行周围放置任意 div。

标签: jsp jquery-mobile struts2


【解决方案1】:

不确定这是否与您的错误有关,但肯定是错误:&lt;%@ taglib uri="/struts-tags" prefix="s" %&gt; 必须放在任何 HTML 语句之前,甚至在 DTD 之前,而不是在 &lt;head/&gt; 标记内;

您也应该使用 &lt;s:form/&gt; 元素的 action 属性,而不是将其设置为空白,然后(我猜)通过 javascript 设置它

【讨论】:

  • 我这样做了...我仍然有同样的问题。
  • 你能发布你的 Struts.xml 和你的 Action 的相关部分吗?这很奇怪
  • 添加了我的 struts.xml 和操作。此外,令人耳目一新的作品。尽管重新加载时不存在填充页面所需的数据,但重新加载时这个特殊问题就会消失。
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 2014-07-01
  • 2020-01-23
  • 2014-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多