将参数从jsp传递到Spring Controller方法 [英] Passing parameters from jsp to Spring Controller method

查看:69
本文介绍了将参数从jsp传递到Spring Controller方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate的Spring MVC应用程序中工作.

I am working in a Spring MVC application which uses Hibernate.

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

In the JSP page I have a function which lists the values stored in the database(currently all the value).

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

I have written a method, where the list is only limited to an ID passed in the JSP file. I got the HQL query working right, so I know it is retrieving data based upon the ID as a parameter.

现在,我想在控制器中使用此方法.为此,我必须将ID的参数传递给列表,因此在控制器端,将调用该函数,该函数将基于该ID检索列表.

Now, I would like to use this method in the controller. For that I have to pass a parameter of ID to the list, so in the controller side, the function is called which will retrieve list based upon that ID.

不幸的是,我不知道如何从JSP文件传递参数.

Unfortunately I don't know how to pass parameters from a JSP file.

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>

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

Controller file with list function :

@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";
    }

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

I tried looking up the net, but I don't know what it is called that I am looking for, so having a hard time. Any help would be good. Thank you.

推荐答案

您的控制器方法应如下所示:

Your controller method should be like this:

@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中以以下方式调用控制器方法:

Use the id in your code, call the controller method from your JSP as:

/{your mapping}/{your id}

更新:

将您的jsp代码更改为:

Change your jsp code to:

<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>

这篇关于将参数从jsp传递到Spring Controller方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆