【问题标题】:using struts tag prefix with ajax使用带有 ajax 的 struts 标签前缀
【发布时间】:2013-06-05 23:41:26
【问题描述】:

我几乎不知道如何使用 AJAX 和 jQuery,或者它是如何工作的,但我想使用 Struts 2 框架制作一个简单的登录 web 应用程序。

我从网上得到了一个登录页面模板,如下:

<html>
<head>
  <link href="css/style.css" rel="stylesheet" type="text/css" />

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $(".username").focus(function() {
          $(".user-icon").css("left","-48px");
      });
      $(".username").blur(function() {
          $(".user-icon").css("left","0px");
      });

      $(".password").focus(function() {
          $(".pass-icon").css("left","-48px");
      });
      $(".password").blur(function() {
          $(".pass-icon").css("left","0px");
      });
    });
  </script>
</head>

<body>
  <div id="wrapper">
    <div class="user-icon"></div>
    <div class="pass-icon"></div>

    <form name="login-form" class="login-form" action="" method="post">
      <div class="header">
        <h1>Login Form</h1>
        <span>
          Fill out the form below to login to my super awesome imaginary control panel.
        </span>
      </div>

      <div class="content">
        <input name="username" type="text" class="input username" value="Username" onfocus="this.value=''" />
        <input name="password" type="password" class="input password" value="Password" onfocus="this.value=''" />
      </div>

      <div class="footer">
        <input type="submit" name="submit" value="Login" class="button" />
        <input type="submit" name="submit" value="Register" class="register" />
      </div>
    </form>
  </div>

  <div class="gradient"></div>
</body>
</html>

为了使用框架,我包含了 tag-libs 指令并将文件重命名为 .jsp 类型,然后将 &lt;form&gt; 标签更改为 &lt;s:form&gt;&lt;/s:form&gt;

这样做之后,它会丢失模板的格式。我下载了 struts2-jquery 插件 jar 并尝试使用 &lt;sj: form&gt; 但导致编译错误。

如何在不丢失模板格式和设计的情况下使用struts标签?

编辑:

http://imgur.com/GKV4b0P,51RmXhP

在这里您可以看到当我将&lt;form&gt;&lt;/form&gt; 编辑为&lt;s:form&gt;&lt;/s:form&gt; 并添加标签库页面指令时会发生什么:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>

我还将扩展名更改为 .jsp 并没有编辑其他内容。

【问题讨论】:

  • 能否请您转义嵌入在您的文本中的标签,以便我们看到它们?您需要使用简单的主题,不知道还有什么,因为所有不相关的内容和大量评论,我无法阅读您的 JSP。
  • +1 大量评论 :D:D
  • 而且您甚至没有包含您尝试调试的 JSP——为什么这是个好主意?

标签: java jquery ajax struts2


【解决方案1】:

Struts2 使用Themes to generate the HTML

对于每个主题,相同的Struts2 Tag(如&lt;s:select/&gt;)将生成不同的 HTML。

默认主题(未指定时应用的主题)是 XHTML 主题

有了它,标签

<s:select key="personBean.sport" list="sports" />

将生成以下 HTML:

<tr>
  <td class="tdLabel">
    <label for="save_personBean_sport" class="label">Favorite sport:</label>
  </td>
  <td>
    <select name="personBean.sport" id="save_personBean_sport">
      <option value="football">football</option>
      <option value="baseball">baseball</option>
      <option value="basketball" selected="selected">basketball</option>
    </select>
  </td>
</tr>

Here you can find the list of the four out-of-the-box themes.

如果有必要,你甚至可以自己卷起来。

我个人更喜欢 Simple 主题 ,它会生成尽可能少的 HTML,让您在标签周围生成 HTML。

要在标签中使用它,只需像这样添加它:

<s:select ... theme="simple" />

要将其设置为项目中所有 JSP 的默认主题,请将以下行放入 struts.xml 文件中:

<constant name="struts.ui.theme" value="simple" />

要将CSS 类 添加到Struts2 标记,请始终使用cssClass 而不是class

要将内联样式添加到Struts2 标记,请始终使用cssStyle 而不是style


现在您已经拥有了绘制组件所需的一切:生成的 HTML 和 CSS 由您决定。

【讨论】:

  • 对于遇到同样问题的其他人,修复具体是:“要向 Struts2 标签添加 CSS 类,请始终使用 cssClass 而不是类;要向 Struts2 标签添加内联样式,在使用简单主题的基础上,始终使用 cssStyle 而不是 style”。
  • 很好,我认为向您提供所有主要功能的概述会比调试您的代码更好,并且它有效:)
  • @VinayBangaloreNagaraj 我猜就像标签文档中所说的那样。
【解决方案2】:

使用

<s:form name="login-form"  theme="simple" class="login-for...

【讨论】:

  • imgur.com/GKV4b0P,51RmXhP,在这里您可以看到当我将
    编辑为
    并添加 tag-lib 时会发生什么页面指​​令。我没有编辑其他内容。顺便说一句,感谢您的快速回复。
  • 对齐方式不正确?
  • 对齐方式不正确,灰框折叠,文本字段无法写入
  • 尝试使用所有带 s: 的字段并设置 theme="simple"
  • 当我尝试更改字段时,出现编译错误 No tag "input" defined in tag library imported with prefix "s"
猜你喜欢
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
相关资源
最近更新 更多