Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8075663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:04:04+00:00 2026-06-05T15:04:04+00:00

How do you fill reusable partials (like the header) in a Spring MVC web

  • 0

How do you fill reusable partials (like the header) in a Spring MVC web app with dynamic data like username every time a certain group of controllers are called (page controllers) but not for others (form controllers, ajax, …)?

header:

<#import "../spring.ftl" as spring />
<!DOCTYPE html>
<html>
<head>
<title>DW-Client</title>
</head>
<body>
<h2>Welcome ${menu.userName}</h2>

index:

<#include "common/header.ftl">
<!-- stuff -->
<#include "common/footer.ftl" />

This is kind of a follow up to this SO post. The guy however seems to be no longer active and I wanted to hear some fresh opinions on our approach to filling partials in a spring mvc web app with data like dynamic menus…

The other guy proposed using an AbstractController that gets extended by all page controllers and fills a variable via an @ModelAttribute annotated method, which seems like an okay idea. but my initial thought was:

Why not use AOP with pointcuts to fill the ModelAndView.model One would just annotate a controller or method with say @MenuData. Now whenever the controlle gets called it will get intercepted by an Aspect that adds the required information to the ModelAndView:

@RequestMapping(value = "/")
@MenuData
public ModelAndView homePath() {
    ModelAndView mav = new ModelAndView();
    mav.setViewName("index");        
    return mav;
}


@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface MenuData {
}

@Aspect
@Component
public class MenuDataAspect {

    @Inject
    private MenuDataProvider menuDataProvider;


    @Pointcut("within(@MenuData *) && execution(public * * (..))")
    public void menuDataRequested() {            
    }

    @Around("menuDataRequested()")
    public Object provideMenuData(ProceedingJoinPoint pjp) throws Throwable {
        Object output = pjp.proceed();
        ModelAndView mav = (ModelAndView) output;
        mav.getModel().put("menu", menuDataProvider.getMenuData());            
        return mav;
    }

}

Now I should be able to say ${menu.userName} or whatever in my header partial… This way I can controll exactly which controller should have menuData.

This concept can be made even more flexible by telling the pointcut to intercept all controllers that begin Page (not sure about the syntax, but I know it’ll work, something like @Pointcut("within(*.Page*)") or all controllers in the pageController package or whatever.

Of course one could also define multiple annotations for different menu scenarios that are not depending on the session but rather on the pages.


So I’m wondering, does this approach look like a good idea or are there any pitfalls? What would you do differently / better? Which approach do you use?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:04:05+00:00Added an answer on June 5, 2026 at 3:04 pm

    AOP seems overkill for initializing data in a few controller classes.

    I question the complexity for something that (a) isn’t application-wide behavior, (b) is limited to a few user-page-level controllers, and (c) can be implemented using traditional inheritance.

    While it would work technically, what’s the compelling reason to not use inheritence?

    If I were providing oversite on this, you’d have to work harder to sell me on the tradeoff.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to fill jQuery variable by PHP??? I mean something like this:
I fill the data set twice, the table name never set correctly. I see
I am trying to fill up an NSmutableAray with data from sqlite database. ItemShow
I am trying to make a reusable header. Here is my XML. <?xml version=1.0
I fill my UITableView with data from a MySQL-DB . If my table does
I fill my tableView with random data. So i don't know how many row
I am building an app with a reusable numeric keyboard. The keyboard has been
How can I fill in the hidden fields in app/views/rounds/shot_fields.html.erb? app/models/player.rb class Player <
I fill a collection one single time when my J2EE webapp starts. Then, several
I fill unused elements in a matrix with NaNs, and I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.