2

I have a simple controller with scope annotation.

@Controller
@Scope("session")
@RequestMapping("/")
public class HelloController {

    private User user = new User("Bob");

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome() {
        return "hello";
    }
}

and simple jsp page

<html>
<body>
    <h1>Y=${sessionScope.user}</h1>
    <h1>Z=${user}</h1>
</body>
</html>

But when I'm trying to run this code, I'm getting nothing. The result is empty. I don't want to use single beans of User, also I don't like to pass object of HttpSession to method in controller or HttpServletRequest object for retrieving session from it. Is there any solution for using some annotation (except @SessionAttributes) to pass object to jsp from my controller?

user3279337
  • 451
  • 1
  • 6
  • 14

1 Answers1

1

I don't see why you are surprised. The only thing in your code that is in the HttpSession is the @Controller bean.

Just because that object has a field of type User doesn't mean there will be a User attribute in the HttpSession.

The result is empty. I don't want to use single beans of User, also I don't like to pass object of HttpSession to method in controller or HttpServletRequest object for retrieving session from it. Is there any solution for using some annotation (except @SessionAttributes) to pass object to jsp from my controller?

You've basically listed all your options. Use any of those.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724