0

I am pretty new in Spring MVC and I have the following problem.

Into an home() controller method defined into a class named **HomeController I retrieve an object using a service. This object have to be put as Session Attribute so it can be used from other methods in other controller classes.

So I have done in this way:

@Controller
@SessionAttributes({"progettoSelezionato"})
@PropertySource("classpath:messages.properties")
public class HomeController {

    private static final Logger logger = Logger.getLogger(LoginController.class);
    private @Autowired HomeService homeService;

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(Model model) {

        List<Tid023Intervento> listaInterventi = homeService.getListaInterventiRUP(18);

        model.addAttribute("progettoSelezionato", listaInterventi.get(0));

        model.addAttribute("listaProgettiRUP", listaInterventi);//TODO

        return "home";
    }
}

As you can see I have used this annotation on the class level:

@SessionAttributes({"progettoSelezionato"})

and then I put this object by:

model.addAttribute("progettoSelezionato", listaInterventi.get(0));

I am not sure that this is correct because, from what I know, this put the retrieved listaInterventi.get(0) object into the model with the voice progettoSelezionato. So I am absolutly not sure that it is putted as SessionAttributes.

Then, after that this object is putted as SessionAttriibute I have to retrieve and use if from a gestioneDatiContabiliEnte() method defined into another controller class, so I am doing in this way:

@Controller
@SessionAttributes({"progettoSelezionato"})
@PropertySource("classpath:messages.properties")
public class GestioneDatiContabiliEnteController {

    private static final Logger logger = Logger.getLogger(LoginController.class);

    @RequestMapping(value = "/gestioneDatiContabiliEnte", method = RequestMethod.GET)
    public String gestioneDatiContabiliEnte(Model model) {

        System.out.println("INTO gestioneDatiContabiliEnte()");
        System.out.println("PROGETTO SELEZIONATO: " + progettoSelezionato);
        return "gestioneDatiContabiliEnte/gestioneDatiContabiliEnte";
    }

}

But it seems can't work because Eclipse sign me error on this line:

System.out.println("PROGETTO SELEZIONATO: " + progettoSelezionato);

The error is: progettoSelezionato cannot be resolved to a variable.

How can I correctly put the listaInterventi.get(0) as SessionAttribute into my HomeController? And how can I retrieve and use it into the gestioneDatiContabiliEnte() method defined into my GestioneDatiContabiliEnteController class?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Although you could abuse `@SessionAttributes` for that, that isn't what it is intended for. Its intended use is for the same controller store an object in the session. If you want to set it in the session just add `HttpSession` to your method signature and put it in yourself. And where you need it, inject the session and obtain it. Note in Spring 4.3 there is a `@SessionAttribute` annotation coming next to the `@RequestParam` annotation to make things easier. – M. Deinum Feb 01 '16 at 19:36

1 Answers1

0

Soled by myself, I can access to this object by:

model.asMap().get("progettoSelezionato");
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596