How to pass a parameter (that identify the primery key of an object) from the view to a controller using Spring MVC?

AndreaNobili

I am pretty new in Spring MVC and I have the following doubt about how correctly create a link into a view page that have to be handled by a method defined into a controller class.

So I have the following situation:

Into a view I use a model object name

<c:forEach items="${scuola.twp1007Progettos}" var="progetto" varStatus="itemProgetto">

    <!-- Visualizza il progetto solo se è un progetto PNSD: -->
    <c:if test="${progetto.flgTipPrg == 'P'}">
        <div class="group-item">
            <a href="visualizzaProspetto">
                <img src="<c:url value="resources/img/icons/projects/PNSD.png" />">
            </a>

            <div>
                <h4><a href="visualizzaProspetto">${progetto.codPro}</a></h4>
                <p>${progetto.twp1009Tipostaprogetto.desTipSta}</p>

            </div>
        </div>
    </c:if>

</c:forEach>

So, as you can see in the previous code snippet, I have a progetto variable that is an instance of a model class named Twp1007Progetto. This model class contain some field that I correctly used in the previous code snippet (for example ${progetto.codPro}).

Ok, untill now I have no problem and I correctly access to the previous property of the progetto variable.

In the previous code snippet I also have this link:

<a href="visualizzaProspetto">

where visualizzaProspetto is a resource handled by this method defined into a controller class:

@RequestMapping(value = "/visualizzaProspetto", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
    .....................................................
    .....................................................
    DO SOMETHING
    .....................................................
    .....................................................

    return "prospettoRendicontazione";
}

Ok, this is correctly called.

Now my problem is that, when the link is clicked by the user, I have not to handle the simple visualizzaProspetto but I have also to pass a parameter representing the value of the prgPro field defined into my Twp1007Progetto model object instance (the ${progetto.prgPro} value) because it is the primary key on the clicked object on the database.

So, what is the best way to do this thing? I know that using Spring I can pass parameter or path variable but I really don't know how correctly handle this situation.

Tnx

sp00m

Either with a request parameter:

<a href="visualizzaProspetto?codPro=${progetto.codPro}">
@RequestMapping(value = "/visualizzaProspetto", method = RequestMethod.GET)
public String home(@RequestParam String codPro, Locale locale, Model model) {
    ...
}

Or with a path variable:

<a href="visualizzaProspetto/${progetto.codPro}">
@RequestMapping(value = "/visualizzaProspetto/{codPro}", method = RequestMethod.GET)
public String home(@PathVariable String codPro, Locale locale, Model model) {
    ...
}

You may need to URL encode the codPro value, depending on whether it can contain reserved chars.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ASP.NET MVC How to pass JSON object from View to Controller as Parameter

How to pass a object parameter to the controller Spring mvc by an onclick

ASP-MVC: How to take an object property from a controller and pass it into view using custom html helper?

Pass object from view to controller MVC 5

How identify a primary key parameter from an ASP NET MVC Model object based on annotation [key] tag?

How can I send an object from a controller to a view Spring MVC

How to pass multiple values from the form to a controller using Spring MVC?

How to pass correctly parameter from view to controller?

How could I pass multiple collections to View as parameter from Controller in ASP.NET MVC?

Pass parameter from a view to a controller

Pass table Object from view to controller using asp.net mvc

How to pass object from Spring Controller to AngularJS

Pass Collection of Object from Controller to View Asp.Net MVC

how to pass Html.HiddenFor from view to view and controller mvc

How to pass custom model object from razor view to controller action in ASP.NET MVC 5

How to pass a null value for a URL parameter to Spring MVC controller?

pass JSON object from javascript function to a java controller class using spring MVC

C# MVC pass parameters from View to Controller using dotnetfiddle

MVC 5 pass model from view to controller using actionlink

How to pass an object from Spring 3.0 controller to JSP view and test with JSTL

Spring MVC, how to pass a parameter to thymeleaf view when redirecting

How to pass the list of objects from view to MVC controller using json.stringify

ASP.Net Mvc How to pass data from View into Controller with a Button using Ajax

Pass parameter from MVC view to an action using jQuery

How to pass FormData and JSON object in ajax to Spring MVC Controller?

Pass Servlet Context Parameter to a Spring MVC Controller

How to pass data from datepicker in view to controller in mvc

How to pass a variables data from a controller to a view (MVC)

ASP.Net MVC How to pass data from view to controller