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

  • SEARCH
  • Home
  • 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 6577345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:37:25+00:00 2026-05-25T15:37:25+00:00

We allow users to set some preferences in our web application. When they login

  • 0

We allow users to set some preferences in our web application. When they login to certain sites, they are allowed to do/see different screens vs. when they are in another site. One of our requirements is that we hide preferences on the page which they can’t access in the currently logged in site. Currently we are using Spring MVC to handle these requests.

Here is a simplified example of our POJO:

public class Preferences {
  boolean prefA; //Valid when can do A
  String prefB;  //Valid when can do B
  Integer prefC; //Valid when can do C
  ....
  Long prefZ;    //Valid when can do Z
}

Here is the controller code:

@RequestMapping(method = RequestMethod.POST, value = "preferences.xhtml")
public ModelAndView updateRequestPreferences(@ModelAttribute(USER_PREFERENCES) final Preference preferences, final BindingResult results)
{
  return updatePreferences(preferences, results);
}

Currently updatePreferences does reflection, and ensures that the value is not null before it persists the input preferences — This is due to Spring MVC creating a new instance of Preferences and then populating the values with what was on the UI.

We could do the following in the setter of the preferences:

public void setPreferences(Preferences preferences) {
  if (preferences.getPrefA() != null) {
    this.preferences.setPrefA(preferences.getPrefA());
  }
  if (preferences.getPrefB() != null) {
    this.preferences.setPrefB(preferences.getPrefB());
  } 
  ...
  if (preferences.getPrefZ() != null) {
    this.preferences.setPrefZ(preferences.getPrefZ());
  }
}

It would get unwieldy even with a helper function for all the checks in the setter method (and seems like an easy step to forget when a new preference is created); at the same time reflection seems like a cop out. Is there a better way to refactor this?

  • 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-05-25T15:37:26+00:00Added an answer on May 25, 2026 at 3:37 pm

    You can configure Spring MVC to populate fields of a preexisting object rather than to create a new one.

    For example, you can use @SessionAttributes(USER_PREFERENCES) in order to store an instance of Preferences in a session between rendering a form and processing its submit.

    Another approach is to load new instance of Preferences from the database as an implicit model attribute:

    @ModelAttribute(USER_PREFERENCES)
    public Preferences loadPreferences(@RequestParam("key") String key) {
        return loadPreferencesByKey(key);
    }
    

    Note that in all cases (and your original approach needs it as well) you need to specify a set of allowed fields in @InitBinder method, to prevent malicious user from modifying fields that was not rendered in a form:

    @InitBinder(USER_PREFERENCES)
    public void initBinder(WebDataBinder b) {
        b.setAllowedFields("prefA", "prefB");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to allow users of my application to add sub-users and set privileges
I'm creating a set of search panes that allow users to tweak their results
I'm creating eBay style search panes that allow users to narrow their results set
We have a content management system to allow our users to store files uploaded
I am currently hoping to use a PropertyGrid to allow users to edit some
For part of a web application the user needs to import some data from
Hello All I am writing some software that will allow users to create their
I have an ASP.NET application that uses a custom MembershipProvider to allow users to
I'm creating an application that allow users to create group profiles. A group profile
I'm using a PropertyGrid in an application I am writing to allow users 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.