【问题标题】:Passing parameters from jsp to Spring Controller method从jsp传递参数到Spring Controller方法
【发布时间】:2014-10-30 12:11:18
【问题描述】:

我正在使用 Hibernate 的 Spring MVC 应用程序。

在 JSP 页面中,我有一个函数列出了存储在数据库中的值(当前是所有值)。

我写了一个方法,其中列表仅限于在 JSP 文件中传递的一个 ID。我的 HQL 查询工作正常,所以我知道它正在根据 ID 作为参数检索数据。

现在,我想在控制器中使用这个方法。为此,我必须将 ID 参数传递给列表,因此在控制器端,调用该函数将根据该 ID 检索列表。

很遗憾,我不知道如何从 JSP 文件中传递参数。

JSP 文件:

  <c:url var="addAction" value="/note/add" ></c:url>
<form:form action="${addAction}" commandName="notices">
    <table>
        <c:if test="${!empty notices.notetext}">
            <tr>
                <td>
                    <form:label path="noticesid">
                        <spring:message text="noticesid"/>
                    </form:label>
                </td>
                <td>
                    <form:input path="noticesid" readonly="true" size="8"  disabled="true" />
                    <form:hidden path="noticesid" />
                </td>
            </tr>
        </c:if>
        <tr>
            <td>
                <form:label path="notetext">
                    <spring:message text="notetext"/>
                </form:label>
            </td>
            <td>
                <form:input path="notetext"  />
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="notetag" >
                    <spring:message text="notetag"/>
                </form:label>
            </td>
            <td>
                <form:input path="notetag"/>
            </td>
        </tr>
        <tr>
            <td>
                <form:label path="notecolor">
                    <spring:message text="notecolor"/>
                </form:label>
            </td>
            <td>
                <form:input path="notecolor" />
            </td>
        </tr>

        <tr>
            <td>
                <form:label path="canvasid">
                    <spring:message text="canvasid"/>
                </form:label>
            </td>
            <td>
                <form:input path="canvasid" />
            </td>
        </tr>

        <tr>
            <td>
                <form:label path="sectionid">
                    <spring:message text="sectionid"/>
                </form:label>
            </td>
            <td>
                <form:input path="sectionid"  />
            </td>
        </tr>

        <tr>
            <td>
                <form:label path="canvasnName">
                    <spring:message text="canvasnName"/>
                </form:label>
            </td>
            <td>
                <form:input path="canvasnName"  />
            </td>
        </tr>


        <tr>
            <td colspan="2">
                <c:if test="${!empty notices.noticesid}">
                    <input type="submit"
                           value="<spring:message text="Edit note"/>" />
                </c:if>
                <c:if test="${empty notices.notetext}">
                    <input type="submit"
                           value="<spring:message text="Add note"/>" />
                </c:if>
            </td>
        </tr>
    </table>
</form:form>
<br>
<h3>Notes List</h3>

<c:url var="listAction" value="/note/list/2323" ></c:url>
<c:if test="${!empty notices.noticesid}">
    <table class="tg">
        <tr>
            <th width="80">Notes ID</th>
            <th width="120">Notes text</th>
            <th width="120">Note Tag</th>
            <th width="120">Note color</th>
            <th width="120">Note section</th>
            <th width="120">Canvas id</th>
            <th width="120">Canvas name</th>
            <th width="120">Other id</th>
            <th width="60">Edit</th>
            <th width="60">Delete</th>
        </tr>
        <c:forEach items="${listNotes}" var="notices">
            <tr>
                <td>${notices.noticesid}</td>
                <td>${notices.notetext}</td>
                <td>${notices.notetag}</td>
                <td>${notices.notecolor}</td>
                <td>${notices.sectionid}</td>
                <td>${notices.canvasid}</td>
                <td>${notices.canvasnName}</td>
                <td>${notices.personid}</td>
                <td><a href="<c:url value='/editnote/${notices.noticesid}' />" >Edit</a></td>
                <td><a href="<c:url value='/removenote/${notices.noticesid}' />" >Delete</a></td>
            </tr>
        </c:forEach>
    </table>
</c:if>

带有列表功能的控制器文件:

@RequestMapping(value = "/note/list/{id}", method=RequestMethod.GET)
    public String listNotes(@PathVariable int id,Model model) {
        Person person = personService.getCurrentlyAuthenticatedUser();
        this.setSectionid(id);
        model.addAttribute("person", new Person());
        model.addAttribute("listPersons", this.personService.listPersons());
       model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person));
        return "note";
    }

@RequestMapping(value= "/note/add")
    public String addNote(@ModelAttribute("notices") Notes p,Model model) {
        Person person = personService.getCurrentlyAuthenticatedUser();
        model.addAttribute("listNotes",this.notesService.listNotes());

        int id = getSectionid();
        System.out.println("Section id is"+id);
         model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person));
        this.notesService.addNote(p, person);
        return "note";
    }

我尝试查找网络,但我不知道我要查找的名称是什么,所以很难。任何帮助都会很好。谢谢你。

【问题讨论】:

  • 请用清晰的方式解释你想要做什么
  • @SanKrish :我已经修改了问题,请看一下。
  • 您是否尝试将 id 从 jsp 传递给控制器​​?
  • @SanKrish 是的。所以我可以使用我写的函数。

标签: spring jsp spring-mvc


【解决方案1】:

使用@RequestParam 将参数传递给控制器​​处理程序方法。 在 jsp 中,您的表单应该有一个带有 name = "id" 的输入字段,如下所示:

<input type="text" name="id" />
<input type="submit" />

然后在您的控制器中,您的处理程序方法应如下所示:

@RequestMapping("listNotes")
public String listNotes(@RequestParam("id") int id) {
    Person person = personService.getCurrentlyAuthenticatedUser();
    model.addAttribute("person", new Person());
    model.addAttribute("listPersons", this.personService.listPersons());
    model.addAttribute("listNotes", this.notesService.listNotesBySectionId(id, person));
    return "note";
}

请同时参考这些答案和教程:

【讨论】:

  • 你能看一下修改后的JSP和控制器吗,不知何故,控制器中的id总是保持为零。
  • 你的意思是在使用@requestParam 之后?
  • 是的。在 JSP 中,我现在尝试使用静态值,例如 /note/list/2323。它仍然保持在零。
  • 我想你误会了。 id 保持静态,由开发人员硬编码。我不希望用户点击提交按钮。你能检查更新的 JSP 页面吗?
  • 你到底想要什么?
【解决方案2】:

你的控制器方法应该是这样的:

@RequestMapping(value = " /<your mapping>/{id}", method=RequestMethod.GET)
public String listNotes(@PathVariable("id")int id,Model model) {
    Person person = personService.getCurrentlyAuthenticatedUser();
    int id = 2323;  // Currently passing static values for testing
    model.addAttribute("person", new Person());
    model.addAttribute("listPersons", this.personService.listPersons());
    model.addAttribute("listNotes",this.notesService.listNotesBySectionId(id,person));
    return "note";
}

在您的代码中使用id,从您的JSP 调用控制器方法:

/{your mapping}/{your id}

更新:

把你的jsp代码改成:

<c:forEach items="${listNotes}" var="notices" varStatus="status">
    <tr>
        <td>${notices.noticesid}</td>
        <td>${notices.notetext}</td>
        <td>${notices.notetag}</td>
        <td>${notices.notecolor}</td>
        <td>${notices.sectionid}</td>
        <td>${notices.canvasid}</td>
        <td>${notices.canvasnName}</td>
        <td>${notices.personid}</td>
        <td><a href="<c:url value='/editnote/${listNotes[status.index].noticesid}' />" >Edit</a></td>
        <td><a href="<c:url value='/removenote/${listNotes[status.index].noticesid}' />" >Delete</a></td>
    </tr>
</c:forEach>

【讨论】:

  • 看来你的方法会奏效。就一件事。如何提取 @PathVariable 值并分配给 int id(当前设置为 2323)。
  • 将id的数据类型改为int。修改了我的答案
  • 谢谢。能不能看一下添加的JSP和Controller,id的值一直都是零。
  • 当你为一个不被接受的答案付出这么多努力时,这很烦人。 +1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多