如何防止Spring 3.0 MVC @ModelAttribute变量出现在URL中? [英] How do I prevent Spring 3.0 MVC @ModelAttribute variables from appearing in URL?

查看:153
本文介绍了如何防止Spring 3.0 MVC @ModelAttribute变量出现在URL中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring MVC 3.0.0.RELEASE,我有以下控制器:

Using Spring MVC 3.0.0.RELEASE, I have the following Controller:

@Controller
@RequestMapping("/addIntake.htm")
public class AddIntakeController{

  private final Collection<String> users;

  public AddIntakeController(){
    users = new ArrayList<String>();
    users.add("user1");
    users.add("user2");
    // ...
    users.add("userN");
  }

  @ModelAttribute("users")
  public Collection<String> getUsers(){
    return this.users;
  }

  @RequestMapping(method=RequestMethod.GET)
  public String setupForm(ModelMap model){

    // Set up command object
    Intake intake = new Intake();
    intake.setIntakeDate(new Date());
    model.addAttribute("intake", intake);

    return "addIntake";
  }

  @RequestMapping(method=RequestMethod.POST)
  public String addIntake(@ModelAttribute("intake")Intake intake, BindingResult result){

    // Validate Intake command object and persist to database
    // ...

    String caseNumber = assignIntakeACaseNumber();

    return "redirect:intakeDetails.htm?caseNumber=" + caseNumber;

  }

}

控制器读取从HTML表单填充的命令对象中获取信息,验证命令对象,将信息保留到数据库,并返回案例编号。

The Controller reads Intake information from a command object populated from an HTML form, validates the command object, persists the information to the database, and returns a case number.

一切都很好,除了当我重定向到intakeDetails.htm页面时,我得到一个如下所示的URL:

Everything works great, except for when I redirect to the intakeDetails.htm page, I get a URL that looks like this:

http:// localhost:8080 / project /intakeDetails.htm?caseNumber=1&users=user1&users=user2&users=user3&users=user4 ...

如何防止用户集合显示在URL中?

How do I prevent the user Collection from showing up in the URL?

推荐答案

从春季3.1开始, RequestMappingHandlerAdapter 提供了一个名为<$ c的标志$ c> ignoreDefaultModelOnRedirect 可用于防止在控制器重定向时使用defautl模型的内容。

Since spring 3.1 the RequestMappingHandlerAdapter provides a flag called ignoreDefaultModelOnRedirectthat you can use to prevent using the content of the defautl model if the controller redirects.

这篇关于如何防止Spring 3.0 MVC @ModelAttribute变量出现在URL中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆