Przemek Przemek - 1 year ago 83
Java Question

How do I prevent Spring 4.0 MVC @ModelAttribute variables from appearing in URL?

I have a method that let you comment about customer and after adding a comment redirect you again to site with comments.

@RequestMapping(value="customers/details/{id}", method = RequestMethod.GET)
public String showCustomerComments(@ModelAttribute("commentContent") String commentContent, @PathVariable int id, Model model){


return "details";

@RequestMapping(value ="customers/details/{id}", method = RequestMethod.POST)
public String processAddCustomerComment(@ModelAttribute("commentContent") String commentContent, @PathVariable int id){

addComment(commentContent,, id);

return "redirect:/customers/details/{id}";

everything works fine but in the url appears model veriable:


I already know the solution but I don't know how to implement it. The solution is to set
true on
. In this topic enter link description here they say to just put sth like
<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" />
to our xml file. But How to do this in Java Based configuration?

I have a such class:

@ComponentScan(basePackages = "com.packt.webstore")
public class WebConfiguration extends WebMvcConfigurerAdapter {

public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
return viewResolver;

public CommonsMultipartResolver multipartResolver(){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;

public LocaleResolver localeResolver(){
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(new Locale("en"));
return sessionLocaleResolver;

public LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
return localeChangeInterceptor;

public MessageSource messageSource(){
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
return messageSource;

public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

public void addResourceHandlers(ResourceHandlerRegistry registry) {

public void addInterceptors(InterceptorRegistry registry) {


and I thought to add to it:

public RequestMappingHandlerAdapter requestMappingHandlerAdapter(){
RequestMappingHandlerAdapter requestMappingHandlerAdapter = new RequestMappingHandlerAdapter();
return requestMappingHandlerAdapter;

but it dosen't work.

I could add to

return "redirect:" + news.getUrl();

but I am not happy with this solution. Because suppose that we have 20 methods like mine and I don't want to put those two lines of code to every of those 20 methods.

How do I solve the problem?

Answer Source

It is possible that your implementation is not working because a RequestMappingHandlerAdapter is already on the context. Adding another one doesn't change the one used de facto. This article suggests that you autowire the existing RequestMappingHandlerAdapter and set the property instead.

public class MyWebConfig {
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    public void init() {

All credits go to for the quoted code.

That being said, unless you have a specific reason for using @ModelAttribute, you should perhaps switch to @RequestParam, which is simpler, less strings attached. Here's this topic discussed at length.