如何防止Spring 4.0 MVC @ModelAttribute变量出现在URL中?

时间:2016-08-21 20:28:38

标签: java spring spring-mvc

我有一种方法可以让您对客户发表评论,并在添加评论后再次将您重定向到包含评论的网站。

@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}";
    }
}

一切正常,但在网址中显示模型可靠:

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

我已经知道解决方案,但我不知道如何实现它。解决方案是在ignoreDefaultModelOnRedirect上设置ignoreDefaultModelOnRedirect true。在这个主题enter link description here中,他们说只是把<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" />放到我们的xml文件中。但是如何在基于Java的配置中执行此操作?

我有一个这样的课程:

    @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());
    }

}

我想补充一点:

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

但它没有工作。

我可以添加到processAddCustomerComment mehtod:

model.asMap().clear();
return "redirect:" + news.getUrl();

但我对此解决方案不满意。因为假设我们有像我这样的20种方法,并且我不想将这两行代码放到这20种方法中。

如何解决问题?

1 个答案:

答案 0 :(得分:2)

您的实施可能无法正常工作,因为RequestMappingHandlerAdapter已经在上下文中。添加另一个并不会改变事实上使用的那个。 This article建议您自动装配现有RequestMappingHandlerAdapter并设置属性。

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

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

所有信用均转到http://www.logicbig.com/以获取引用的代码。

话虽如此,除非您有使用@ModelAttribute的具体原因,否则您应该切换到@RequestParam,这更简单,附加的字符串更少。这里详细讨论了this topic