0

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

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

2

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.

Community
  • 1
  • 1
sp00m
  • 47,968
  • 31
  • 142
  • 252
  • Ok tnx, in your opinion what it the best solution for this purpose? The request parameter or the path variable? โ€“ AndreaNobili Aug 31 '15 at 14:33
  • 1
    @AndreaNobili It's really up to you, I'm not sure there's a best solution. In this situation, I would probably opt for a request parameter since path variables are usually used with technical identifiers. โ€“ sp00m Aug 31 '15 at 14:41
  • 1
    Perfect, tnx so much :-) โ€“ AndreaNobili Aug 31 '15 at 14:41