0

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

这是 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;
    }
}
4

1 回答 1

0

不确定这是否与您的错误有关,但这绝对是一个错误:<%@ taglib uri="/struts-tags" prefix="s" %>必须放在任何 HTML 语句之前,甚至在 DTD 之前,而不是在<head/>标签内;

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

于 2013-07-29T12:19:08.853 回答