Spring mvc 3 一般Controller架构问题

发布于 2024-11-03 00:09:49 字数 841 浏览 1 评论 0原文

抱歉新手问题, 场景很简单,我进入Jsp页面,可以填写Driver信息, 我有驱动程序对象的设置方法

@RequestMapping(method = RequestMethod.GET)
public ModelAndView setUpForm(){
ModelAndView modelAndView =  new ModelAndView("/driverForm");
Driver = myService.getDriver();
modelAndView.addObject("driver",driver);
return modelAndView;
}

和更新方法来获取驱动程序更新的数据

@RequestMapping(params = "update", method = RequestMethod.POST) 
  public String update(Driver driver, BindingResult result, SessionStatus status) {
myService.saveDriver(driver);
return "driversList";
}

然后我向 jsp 层“提供”所需的驱动程序信息,例如驱动程序名称:

<form:input path="name" size="20" maxlength="50"  />

我的问题是如何填充不同的模型属性并连接驱动程序这些属性的信息 例如: 将各种许可证类型显示为复选框,供用户选择(自行车、公共汽车、出租车等),并将它们在我的控制器中映射到驱动程序对象上的单个属性, 从此字段编辑现有驱动程序时还要映射选择 什么是正确的控制器架构?

Sorry for the newbie question,
The scenario is simple, I go Jsp page where one can fill Driver information,
I have setup method for the driver object

@RequestMapping(method = RequestMethod.GET)
public ModelAndView setUpForm(){
ModelAndView modelAndView =  new ModelAndView("/driverForm");
Driver = myService.getDriver();
modelAndView.addObject("driver",driver);
return modelAndView;
}

And update method to get the driver updated data

@RequestMapping(params = "update", method = RequestMethod.POST) 
  public String update(Driver driver, BindingResult result, SessionStatus status) {
myService.saveDriver(driver);
return "driversList";
}

Then I am "feeding" the jsp layer with the desired driver information, for example driver name:

<form:input path="name" size="20" maxlength="50"  />

My question is how to populate different model attributes and connect the driver information to these attribute
For example:
Display various license types as checkboxes for the user to select from (bike,bus,cab, etc) and map them in my controller to a single attribute on the driver object,
Also map the selection when editing existing driver from this field
What is the correct controller architecture?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2024-11-10 00:09:49

在您的控制器中,将一个集合添加到包含您想要提供的所有选项的模型中,然后使用 from:radioButtons 标记来呈现它,这样代码将如下所示:

    @RequestMapping(method = RequestMethod.GET)
public ModelAndView setUpForm(){
ModelAndView modelAndView =  new ModelAndView("/driverForm");
Driver = myService.getDriver();
modelAndView.addObject("driver",driver);
Collection transportType = ....
modelAndView.addObject("transportType", transportType);
return modelAndView;
}

在 JSP 中,您将执行类似这样的操作

<form:radioBoxes items="${transportType}" path="..."/>

您可以找到更多有关单选按钮标签的信息:

In your controller add a collection to the model containing all the options you want to offer and then use the from:radioButtons tag to render it so the code would look like this :

    @RequestMapping(method = RequestMethod.GET)
public ModelAndView setUpForm(){
ModelAndView modelAndView =  new ModelAndView("/driverForm");
Driver = myService.getDriver();
modelAndView.addObject("driver",driver);
Collection transportType = ....
modelAndView.addObject("transportType", transportType);
return modelAndView;
}

In the JSP you would then do something like this

<form:radioBoxes items="${transportType}" path="..."/>

You can find more information on the radio buttons tag here : http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-jsp-formtaglib-radiobuttonstag

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文