Spring MVC获取onclick输入星级的值

Spring MVC get value of onclick input star rating

本文关键字:输入 MVC 获取 onclick Spring      更新时间:2023-09-26

我正在编写一个javaspringmvc web应用程序,在客户端,我使用引导程序和星级评定插件来表示对我向用户显示的歌曲列表的赞赏。

我正在做的是传递我在会话中生成的播放列表,并在jsp中迭代该列表以创建一个合适的表。在歌曲名称''标题的一侧,我想显示一个星级系统;我做了,但它被设置为一个输入数字。我想做的是,点击一次星号,重定向到另一个页面,在控制器中检索速率,制定一些逻辑,然后返回没有那首歌的播放列表页面。

我遇到了一些困难,因为在控制器上,要想了解哪首歌被评级了,我显然必须将歌曲的id作为输入参数,所以输入的名称有点像"input2838",但在控制器侧,当我必须请求具有RequestParameter值的参数时,我需要准确地指定我想要的参数的名称,所以我认为这不是正确的做法。

我向你征求如何做这类事情的建议,最重要的是,当我点击输入时,如何重定向到另一个页面,传递参数的值。。。

我将向您展示jsp代码片段,其中显示了歌曲表。

<script>$("#input-id").rating();</script> <-- initialize the stars script
<c:choose>    <-- when the playlist is not empty show it
  <c:when test="${not empty playlist}">
   <div class="container col-lg-10 col-lg-offset-1" >
    <table class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>#</th>
                <th>Cover</th>
                <th>Artista</th>
                <th>Titolo</th>
                <th>Rate</th>
            </tr>
        </thead>
        <tbody>
<c:forEach items="${playlist}" var="canzone" varStatus="count">
<tr style="font-weight: normal">
  <td style="font-weight: normal">${count.index +1}</td>
  <td style="font-weight: normal"><img src="${canzone.albumImageUrl}" /></td>
  <td style="font-weight: normal">${canzone.artistName}</td>
  <td class="col-lg-4" style="font-weight: normal">${canzone.title}</td>
<td class="col-lg-4" style="font-weight: normal"><div><input id="input-id" type="number" class="rating" min=0 max=5 step=1 data-size="xs" data-rtl="false"></div></td>
       <tr>
    </c:forEach>
            </tbody>
    </table>
    </div>
  </c:when>
  <c:otherwise>

现在,为了完整起见,我将星级js发布为pastebin链接:

粘贴在此处

请随时询问其他信息。

您的问题是,如果您获得表中指定为id="sondId"name="sondId"的所有歌曲id,并且使用request.getParameter("songId"),它将不会选择正确的值,因为所有行中都包含相同的id和名称。

所以你需要做的是在<c:forEach>中输入a,就像在下面一样

<c:forEach items="${playlist}" var="canzone" varStatus="count">
    <form action="yourActionPath" method="post">
        <tr style="font-weight: normal">
            <input type="hidden" id="songId" name="songId" value="${canzone.id}"/>
            <td style="font-weight: normal">${count.index +1}</td>
            <td style="font-weight: normal"><img src="${canzone.albumImageUrl}" /></td>
            <td style="font-weight: normal">${canzone.artistName}</td>
            <td class="col-lg-4" style="font-weight: normal">${canzone.title}</td>
            <td class="col-lg-4" style="font-weight: normal"><div><input id="input-id" type="number" class="rating" min=0 max=5 step=1 data-size="xs" data-rtl="false"></div></td>
        </tr>
    </form>
</c:forEach>

你必须在指定了星号之后决定如何提交表单,比如onchange()或其他什么。这不是一个很好的选择,但据我所知。。