【问题标题】:Struts2 action without resultStruts2 动作没有结果
【发布时间】:2015-10-10 00:03:19
【问题描述】:

我正在编写一个简单的购物车,但我不知道该怎么做:

这是jsp的一部分:

<s:iterator value="#session.listaLibros" var="libro">
                <tr>
                    <td><s:property  value="titulo"/></td>
                    <td><s:property value="autor"/></td>
                    <td><s:property value="precio"/></td>
                    <s:hidden name="isbn" value="%{idLibro}"/>
                    <td><s:url var="url" action="comprar">
                            <s:param name="idLibro" value="isbn"/>
                        </s:url>
                        <s:a href="%{url}">Comprar</s:a></td>
                </tr>
            </s:iterator>

它有一个表格,您可以在其中查看图书详细信息。在每一行中,都有一个购买该书的链接。我已经用 and 对其进行了编码,并在 . 内传递了参数。

问题是我不想转到另一个 jsp 页面。当您单击该链接时,它会触发一个操作(将图书添加到列表中)并在同一 jsp 页面底部的另一个表中显示该列表。 我该怎么做?

如果我在 xml 文件上编写一个没有结果的新操作,我会收到错误消息。 我编写了一个新类,它不扩展 ActionSupport,但也不起作用。我得到一个空白页。

//ActionClass
public class Comprar {
.
.
//Action method
public void comprarLibro(){
....
}

struts.xml

<!--Action for the ActionSupport action class-->

<action name="comprarLibroAction" class="controller.ComprarLibroAction"  method="comprarLibro">

    </action>

    <!--Simple action class-->
    <action name="comprar" class="controller.Comprar" method="comprarLibro"></action>

【问题讨论】:

    标签: struts2 actionresult


    【解决方案1】:

    Ajax 是解决方案。这里解释如何创建前端部分和后端部分。前端部分很简单...只是将元素添加到购物车中,不插入ajax调用,不考虑数量。

    前端

    $(document).ready(function() {
      $('.buy').click(function() {
        // retrieve book description
        var bookId = $(this).attr('id');
        var description = $(this).parent().parent().find('.description').html();
        
        // HERE ADD THE AJAX CALL
        // On success function use this code to add values to the cart
        var tr = $('<tr />');
        var td = $('<td />', {id: bookId, html: description});
        
        $(tr).append(td);
        
        $('#cart tbody').append(tr);
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <table>
      <thead>
        <tr>
          <th>Id</th>
          <th>Title</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td class="description">Asd Asd by Asd</td>
          <td><button id="1" class="buy">Buy!</button></td>
        </tr>
        <tr>
          <td>2</td>
          <td class="description">Lol by Lol</td>
          <td><button type="button" id="2" class="buy">Buy!</button></td>
        </tr>
        <tr>
          <td>3</td>
          <td class="description">Struts by Apache</td>
          <td><button type="button" id="3" class="buy">Buy!</button></td>
        </tr>
      </tbody>
    </table>
    
    <table id="cart">
      <thead>
        <tr>
          <th>Cart</th>
        </tr>
      </thead>
      <tbody>
      </tbody>
    </table>

    后端

    对于后端部分,您必须使用 Struts2 JSON 插件来设置可由 Ajax 调用并返回所需 JSON 结果的操作(例如,如果书仍然可用,或更新会话购物车.. 。有关更多信息,请查看official documentation 和谷歌的一些示例...

    这是一个例子:

    struts.xml

    <struts>
        <package name="example"  extends="json-default">
            <action name="JSONExample" class="example.JSONExample">
                <result type="json"/>
            </action>
        </package>
    </struts>
    

    SUCCESS 上的 CartAction 会自动将您的所有操作属性(具有 getter 和 setter 的属性)映射到 JSON 对象中,该对象可以在 success ajax 函数中详细说明

    // Ajax call
    success: function(response) {
        // ...
        var tr = $('<tr />');
        var td = $('<td />', {id: bookId, html: description});
    }
    

    【讨论】:

    • 非常感谢您,@IIGala,但我正在尝试不使用 javascript。还有其他方法吗?
    • 唯一的就是在action成功时刷新同一个页面....但是可能非常昂贵....用户购买的所有东西都必须刷新页面? JQuery 是一个更好的解决方案
    猜你喜欢
    • 2014-01-13
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多