如何在 Spring MVC 中向 JSP 发送参数并获取更新的参数

发布于 2024-12-05 12:46:41 字数 2804 浏览 2 评论 0原文

我是 Spring MVC 和 JSP 开发的新手,所以请温柔地告诉我我正在做一些愚蠢的事情:P 说到这里,问题是:

我研究了一些 Spring MVC,并得到了一段很好的工作代码。现在我陷入了一个明显愚蠢的问题:似乎我无法将传递给 JSP 的对象返回到控制器中。 我需要做的是:用户填写两个文本框,然后单击按钮,系统应该查询数据库,找到用户搜索的元素,将找到的元素添加到数组“capitolospesas”中并重新渲染包含数组中所有对象的 jsp(前一个 + 刚刚添加的对象)。

我创建的循环是:controller ---> jsp --->控制器---> jsp。 映射“newProposte”使用空白值初始化页面 newEditProposte.jsp,jsp 正确获取要显示的值,用户编辑/填写所需内容并单击提交按钮。控件传递到方法“selectCapitoloSpesaFromProposte”,系统应在该方法中执行查询并将结果添加到数组“capitolospesas”中,但当我添加新对象时它始终为空,因此在页面上它仅显示找到的最后一个元素。

@RequestMapping("/newProposte")
public ModelAndView newProposte()
{
    System.out.println("ProposteController.newProposte()");
    ModelAndView mav = new ModelAndView();

    mav.addObject("proposte", new ProposteWeb());
    mav.addObject("newFlag", true);
    mav.addObject("capitolospesas", new HashSet<Capitolospesa>());

    mav.setViewName("proposte/newEditProposte.jsp");

    return mav;
}

@RequestMapping("/selectCapitoloSpesaFromProposte")
public ModelAndView selectCapitoloSpesaFromProposte(@ModelAttribute ProposteWeb proposte, @ModelAttribute HashSet<Capitolospesa> capitolospesas)
{
    ModelAndView mav = new ModelAndView();

    mav.addObject("proposte", proposte);
    mav.addObject("newFlag", true);        
    capitolospesas.add(capitolospesaDAO.findCapitolospesaByCapitoloSpesaCapitoloAndCapitoloSpesaArticolo(proposte.getCapitoloSpesaCapitoloProposte(), proposte.getCapitoloSpesaArticoloProposte()));
    mav.addObject("capitolospesas", capitolospesas);

    mav.setViewName("proposte/newEditProposte.jsp");

    return mav;
}

jsp:

<script language="Javascript">       
function selectCapitoloSpesa()
{
    alert("selectCapitoloSpesaFromProposte");
    document.getElementById("tabProposteForm").action = "${pageContext.request.contextPath}/selectCapitoloSpesaFromProposte";
    submitForm();
    return true;
}
function submitForm()
{
    alert("submit");
    document.getElementById("tabProposteForm").submit();
    return true;
}
</script>
....
....
the form
....
....
the foreach iterating on the array "capitolospesas" showing it in a table
....
....
<input type="button" value="selectCapitoloSpesaFromProposte"    name=selectCapitoloSpesaFromProposte    onclick="return selectCapitoloSpesa();">

我以这种方式发布帖子,因为根据单击的按钮(有各种按钮),我需要表单表现不同。

我阅读了各种教程,要将参数传递到 jsp 页面,我必须将它们添加到 ModelAndView 对象,并且我正确地执行了此操作。在 jsp 中,我可以访问传递给它的每个对象,在本例中为“newFlag”、“capitolospesas”和“proposte”。我不明白的是,当这些对象从 jsp 返回时,如何获取它们? 到目前为止,我在保存、编辑或删除页面上唯一实体所需的唯一参数之前使用了 @ModelAttribute 注释,但现在我需要从页面获取多个对象。在上面粘贴的代码中,我尝试多次使用 @ModelAttribute 注释,但没有成功;对象“proposte”正确地填充了用户键入的内容,但我调试了代码,发现每次方法“selectCapitoloSpesaFromProposte”启动一个新数组“capitolospesas”时都会实例化,而不是从我之前传递的数组中获取值到jsp。

如果有人有任何建议,请发布。

I'm new to Spring MVC and JSP developing, so please be gentle telling me I'm doing something stupid :P
Told that, the question:

I studied a bit Spring MVC and got a nice piece of working code. Now I'm stuck on an apparently stupid problem: it seems I cannot get the objects I pass to the JSP back in the controller.
What I need to do is: the user fills in two textbox, then he clicks on a button and the system should query the db, find the element the user searched for, add the element found to the array "capitolospesas" and re-render the jsp with all the objects in the array (previous + the one just added).

The cycle I created is: controller ---> jsp ---> controller ---> jsp.
The mapping "newProposte" initialises the page newEditProposte.jsp with blank values, the jsp correctly gets the values to display, the user edits/fills in what is needed and clicks the submit button. The control passes to the method "selectCapitoloSpesaFromProposte" where the system should perform the query and add the result to the array "capitolospesas", but it's always empty when I add the new object, so on the page it displays only the last element found.

@RequestMapping("/newProposte")
public ModelAndView newProposte()
{
    System.out.println("ProposteController.newProposte()");
    ModelAndView mav = new ModelAndView();

    mav.addObject("proposte", new ProposteWeb());
    mav.addObject("newFlag", true);
    mav.addObject("capitolospesas", new HashSet<Capitolospesa>());

    mav.setViewName("proposte/newEditProposte.jsp");

    return mav;
}

@RequestMapping("/selectCapitoloSpesaFromProposte")
public ModelAndView selectCapitoloSpesaFromProposte(@ModelAttribute ProposteWeb proposte, @ModelAttribute HashSet<Capitolospesa> capitolospesas)
{
    ModelAndView mav = new ModelAndView();

    mav.addObject("proposte", proposte);
    mav.addObject("newFlag", true);        
    capitolospesas.add(capitolospesaDAO.findCapitolospesaByCapitoloSpesaCapitoloAndCapitoloSpesaArticolo(proposte.getCapitoloSpesaCapitoloProposte(), proposte.getCapitoloSpesaArticoloProposte()));
    mav.addObject("capitolospesas", capitolospesas);

    mav.setViewName("proposte/newEditProposte.jsp");

    return mav;
}

The jsp:

<script language="Javascript">       
function selectCapitoloSpesa()
{
    alert("selectCapitoloSpesaFromProposte");
    document.getElementById("tabProposteForm").action = "${pageContext.request.contextPath}/selectCapitoloSpesaFromProposte";
    submitForm();
    return true;
}
function submitForm()
{
    alert("submit");
    document.getElementById("tabProposteForm").submit();
    return true;
}
</script>
....
....
the form
....
....
the foreach iterating on the array "capitolospesas" showing it in a table
....
....
<input type="button" value="selectCapitoloSpesaFromProposte"    name=selectCapitoloSpesaFromProposte    onclick="return selectCapitoloSpesa();">

I made the post in this way because depending on the button clicked (there are various buttons) I need the form to behave differently.

I read all over the various tutorials that to pass parameters to the jsp page I have to add them to the ModelAndView object, and I correctly do this. In the jsp I have access to every object I pass to it, in this case "newFlag", "capitolospesas" and "proposte". What I don't understand is how can I get those objects when they come back FROM the jsp?
Until now I used @ModelAttribute annotation before the only parameter I needed to save, edit or delete the only entity I had on a page, but now I need to get multiple objects from the page. In the code pasted above I tried using multiple times the @ModelAttribute annotation, but without success; the object "proposte" is correctly filled with the things typed by the user, but I debugged the code to find that everytime the method "selectCapitoloSpesaFromProposte" starts a new array "capitolospesas" is instantiated instead of getting the values from the array I passed earlier to the jsp.

If anyone has any suggestions please please post them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魔法唧唧 2024-12-12 12:46:41

当 JSP 发送到客户端(即呈现为 HTML)时,唯一保留的数据是生成的 HTML 文档中的数据。不存在 Java 对象,只有文本 - 可以作为表单提交返回到服务器。因此JSP可以访问java对象,但是一旦它生成了网页,它就被忘记了。请记住:网页只是一个文本文档,Web 浏览器和服务器之间交换的所有内容都是文本。 Java 对象不会被忽略。

要保留 java 对象(可以在提交表单时修改),必须将其存储在其他地方。具体来说,您将使用会话对象直接存储该对象,或存储指向它的其他指针(例如,可用于引用数据库条目的登录名,或用于读取的文件名等)。

要在普通 servlet 中使用会话值,请执行以下操作:

HttpSession session = request.getSession(true);
session.setAttribute( "keyForObject", myObject );

存储对象。


然后

HttpSession session = request.getSession(true);
myObject = (ObjectType)session.getAttribute( "keyForObject" );

检索它。

在 Spring RequestMapping 中,只需添加 HttpSession 参数即可获取会话对象:

@RequestMapping
public void myMethod(... HttpSession session)

When the JSP is sent to the client (ie. rendered in to HTML), the ONLY data that is preserved is that which is in the resulting HTML document. No Java objects exist, only text - which can be returned to the server as a form submission. So the JSP may access java objects, but once it has generated the web page, it is forgotten. Remember: a web page is just a text document, and all that is exchanged between web browser and server is text. Java objects don't get passed over.

To preserve a java object (which you can amend when the form is submitted), you must store it elsewhere. Specifically you would use the session object to store the object directly, or to store some other pointer to it (eg. a login name that you can use to reference a database entry, or a file name to read from etc.).

To use the session value in a normal servlet do something like:

HttpSession session = request.getSession(true);
session.setAttribute( "keyForObject", myObject );

to store the object.
.
.
then

HttpSession session = request.getSession(true);
myObject = (ObjectType)session.getAttribute( "keyForObject" );

to retrieve it.

In a Spring RequestMapping, you just need to add a HttpSession parameter to get the session object:

@RequestMapping
public void myMethod(... HttpSession session)
我恋#小黄人 2024-12-12 12:46:41

您必须将对象保存在控制器中,无论是在会话变量还是数据库(或其他持久存储)中。您无法从 ModelAndView 对象中取回它。该对象被发送到 JSP,然后被垃圾收集。您处于新请求中,除非将模型对象保存在某个地方,否则您无法访问先前请求中的模型对象。

You have to save the object in your controller, either in a session variable or a database (or other persistent store). There's no way you'll get it back from the ModelAndView object. That object was sent to the JSP and was garbage collected afterwards. You're in a new request, there's no way you can access model objects from a previous request unless you save them somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文