Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:21:33+00:00 2026-06-14T09:21:33+00:00

I basically have the following jsp which retrieves the advertisement objects which came with

  • 0

I basically have the following jsp which retrieves the advertisement objects which came with the spring model advertisement list ${myAdverts}.

And I would like that when one of those is clicked, a post request is submitted back to my controller but with an instance of the advertisement object. Is that somehow possible?

here is my JSP code:

<xc:forEach var="advertisement" items="${myAdverts}" varStatus="stats">
    <li>

    <a class="furtherinfo-link" onclick="javascript:submitJoin(${stats.count})" >${advertisement.getName()}</a>
    </li>
</xc:forEach>
<form:form id="myform" method="POST" action="form_submit.html" commandName="myForm" name="MyForm">
 <form:input id="advertisementObj" path="advertisementObj" type="hidden"/>
 </form:form>

here is my attempt to send the post back with javascript inspired by the handling of autopopulating lists in spring MVC and javascript:

javascript code

 <script src="js/webmenu_nav.js"></script>

<script type="text/javascript">
      function submitJoin(position){
        $('#advertisementObj').val("myAdverts["+position+"]");

        document.MyForm.submit();
      }

</script>

The current behavior of that code is that I always get an empty advertisementObj on my post method in my Controller object.

The controller object is very simple, but just in case here it is part of its code:

@Controller
public class MyController {

@RequestMapping(value="/show_advertisements.html",method = RequestMethod.GET)
public ModelAndView showAdv(@RequestParam(value="response", required=false) String incomingResponse) {

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("response", incomingResponse);

    List<AdvertisementRecord> adverts = methodThatReturnsList();
    model.put("myAdverts", adverts);

    MyForm jform = new MyForm();
    model.put("myForm", jform);

    return new ModelAndView("show_advertisements", model) ;
}

@RequestMapping(value = "/form_submit.html", method = RequestMethod.POST)
public ModelAndView formSubmit(MyForm myForm,  BindingResult result, Map model){


    if(null != myForm.getAdvertisement())
        return showPage("adver " + myForm.getAdvertisement().getId());
    else
        return showPage("null advertisement on join");


}

 }

Solution!!

snippets of the solution code

JSP code:

<xc:forEach var="advertisement" items="${myAdverts}" varStatus="stats">
    <li>
    <a class="furtherinfo-link" onclick="javascript:submitForm(${stats.count})" >${advertisement.getName()}</a>
    </li>
</xc:forEach>
<form:form method="POST" id="theForm" action="form_submit.html" modelAttribute="myAdverts" name="MyForm">
</form:form>    

javascript:

<script src="js/webmenu_nav.js"></script>

    <script type="text/javascript">
          function submitForm(position){
           $('#theForm').attr("action","form_submit.html?position="+position);

            document.MyForm.submit();
          }

    </script>

</head> 

controller:

 @Controller
 @SessionAttributes("myAdverts")
public class MyController {

@RequestMapping(value="/show_advertisements.html",method = RequestMethod.GET)
public ModelAndView showAdv(@RequestParam(value="response", required=false) String incomingResponse) {

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("response", incomingResponse);

    List<AdvertisementRecord> adverts = methodThatReturnsList();
    model.put("myAdverts", adverts);

    //MyForm jform = new MyForm();
    //model.put("myForm", jform);

    return new ModelAndView("show_advertisements", model) ;
}

@RequestMapping(value = "/form_submit.html", method = RequestMethod.POST)
public ModelAndView formSubmit(@RequestParam("position") final int position, @ModelAttribute("adverts") @Valid List<AdvertisementRecord> adverts,  BindingResult result, Map model){


    if(null != adverts && null != adverts.get(position))
        return showPage("adver " + adverts.get(position).getId());
    else
        return showPage("null advertisement ");


}

 }

Be aware on the code above, that it is important to have the request param coming first in the signature as Im calling “form_submit.html?position=”+position”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:21:35+00:00Added an answer on June 14, 2026 at 9:21 am

    objects you put in your model are only there for the current request by default. It means your myAdverts list is not there any more in the second request (i.e. the POST request). However you can use @SessionAttribute annotation to tell spring mvc to store objects in the http-session, so you can access them in further requests.

    your controller could look like this:

    @Controller
    @SessionAttributes("myAdverts")
    public class MyController {
        @RequestMapping(value="...", method=RequestMethod.GET)
        public void get(ModelMap model){
            List myAdverts = // get your list of adverts.
            model.put("myAdverts", myAdverts)
        }
    
        @RequestMapping(value="...", method=RequestMethod.POST)
        public void post(@RequestParam("position") final int position, @ModelAttribute("myAdverts") List myAdverts,SessionStatus sessionStatus){
            myAdverts.get(position);
            // ...
    
            // tell spring to remove myAdverts from session
            sessionStatus.setComplete();
        }
    }
    

    for more information on @SessionAttribute take a look here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
i have the following code from my JSP which doesn't quite work: <%@ taglib
Wondering if anyone can help with the following. Basically I have a form which
I need some help with this. Basically I have the following code which I
I have the following jsfiddle setup: http://jsfiddle.net/5bFq7/8/ basically I have a ul list and
I basically have the following string in the format: A,B,C:D,E,F What I am trying
I basically have the following class: .sf-sub-indicator { background: url(/abcprod/images/arrows-ffffff.png) no-repeat -10px -100px; }
I am wondering if the following is possible in MVC 3 I basically have
Basically I have a new client that is after the following functionality from her
I have the following views in my application. Basically I want to call show_house()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.