1

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){

        model.addAttribute("comment",commentRepository.getAllComments(id));

        return "details";
    }

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

        commentRepository.
                addComment(commentContent, localDate.now().toString(), id);

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

everything works fine but in the url appears model veriable:

http://localhost:8080/customers/details/62?commentContent=some_text

I already know the solution but I don't know how to implement it. The solution is to set ignoreDefaultModelOnRedirect true on ignoreDefaultModelOnRedirect. 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:

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

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(10240000);
        return  multipartResolver;
    }

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

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor(){
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }

    @Bean
    public MessageSource messageSource(){
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return  messageSource;
    }


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("resources/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

}

and I thought to add to it:

    @Bean 
public  RequestMappingHandlerAdapter requestMappingHandlerAdapter(){
 RequestMappingHandlerAdapter  requestMappingHandlerAdapter = new  RequestMappingHandlerAdapter();
requestMappingHandlerAdapter.ignoreDefaultModelOnRedirect(true);
return requestMappingHandlerAdapter;
}

but it dosen't work.

I could add to processAddCustomerComment mehtod:

model.asMap().clear();
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?

Community
  • 1
  • 1
Przemek
  • 647
  • 2
  • 8
  • 25

1 Answers1

5

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.

@EnableWebMvc
@Configuration
public class MyWebConfig {
    @Autowired
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @PostConstruct
    public void init() {
       requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(true);
    }
    ......
}

All credits go to http://www.logicbig.com/ 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.

Community
  • 1
  • 1
Deroude
  • 1,052
  • 9
  • 24