Java Spring MVC & Angular

508 views
Skip to first unread message

Jaymes Bearden

unread,
Jan 7, 2013, 10:52:47 PM1/7/13
to ang...@googlegroups.com
I'm working with Spring MVC & Angular to create a few different dynamic sites and I found a situation that I need to handle and I'm not sure what the best method is...

Basically, I have a Spring controller where I setup a model with some properties and attach a view which is rendered using Velocity. I need some information from the model present in the view that I would like to be passed further into Angular --a couple cases to demonstrate my points:

1: On a few pages, I have some user authentication information that I want available in an Angular service. I really don't want to resort to an async GET on page load since the information is already available in the Spring model on the initial page request and I would have to write an additional Spring controller method to get the information/additional testing/etc etc.

2: I need application context path information available throughout Angular when I do async GET/POST. In this specific situation, I have used Velocity to write this information into a script tag like so:

<script type="text/javascript">
  var contextPath = '$!{request.contextPath}'; // Velocity notation for requesting a model attribute
</script>

and later retrieved this global variable in Angular. I hate doing it this way as it feels very un-Angular-like, but the problem is that I have other situations where I need to do exactly this. There are a bunch of other 'expensive' operations that are easier/cheaper to embed into the page by a Spring model attribute than to acquire by a GET/POST later on.

What I would love is if I could somehow get various Spring model attributes in to an Angular module .config() object that I could inject throughout Angular. I thought about writing a Spring controller that returns a text/javascript script where I could embed this information but that feels a little hacky too.

Any thoughts on best/suggested practices when dealing with these situations or dealing with dynamic server pages and Angular (aside from avoiding them) ?
Reply all
Reply to author
Forward
0 new messages