Freemarker-选择选项未保存 [英] Freemarker - select option not saved

查看:85
本文介绍了Freemarker-选择选项未保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ftl文件中有一个表单,其中一部分是此选择:

I have a form in my ftl file and one part of this is this select:

 <select id="${spring.status.expression}" value="${spring.status.expression}">
    <$assign value=(spring.status.value!"") />
    <option value="A" >A</option>
    <option value="B" >B</option>
    <option value="C" >C</option>
</select>

默认情况下将其分配给A很好,但是当我单击选项B然后单击提交"时,B不会保持选中状态,因为它将被分配默认值A.我该如何解决?我在stackoverflow上看到了一些答案,但是在不使用的php中.

It is assigned to A by default what is fine, but when I click option B and then click submit, B won't stay selected, because it will be assigned default value A. How can I fix it ? I see some answers on stackoverflow but in php which I don't use.

推荐答案

让我们建立共同点:

首先,创建一个POJO来保存表单信息

First, create a POJO to hold the information of your form

public class Foo {
   private String bar;
   // public getter and setter ommitted
} 

现在,在您的Spring MVC控制器中,将要绑定的实例放入Spring MVC的模型中.这可以通过使用@ModelAttribute注释方法来完成,该方法返回所需的实例.通常,您将从数据库中读取表单的当前值-为了简洁起见,我只是在这里创建一个新的模型实例:

Now, in your Spring MVC controller, put the instance you want to bind to into Spring MVC's model. This is done by annotating a method using @ModelAttribute which returns the required instance. Typically, you'll read current values for your form from a database - For the sake of brevity I'm just creating a new model instance here:

@Controller
public class MyController {

   @ModelAttribute
   public Foo readFooFromDB() {
     // read the current option from the DB or simply create a default option here
     Foo f = new Foo();
     f.setBar("B");
     return f;
   }

}

Spring现在将使用名称fooFoo实例放入其模型中.您可以通过为@ModelAttribute注释分配另一个值来更改该名称.

Spring will now put the Foo instance into it's model using the name foo. You could change that name by assigning another value to your @ModelAttribute annotation.

下一步,将@RequestMapping方法添加到您的控制器,该方法将转发到您的Freemarker视图:

Next add a @RequestMapping method to your controller, which will forward to your Freemarker View:

@Controller
public class MyController {

   ....

   @RequestMapping("/request/path/")
   public String process() {
     return "mytemplate";
   }

}

最后,在您的模板中,使用Spring的FreeMarker宏来呈现包括您选择的表单:

Finally, in your template use Spring's FreeMarker macros to render a form including your select:

<#import "spring.ftl" as spring />
<form action="/request/path/">

  <#assign options = { "A": "Option A", "B": "Option B", "C": "Option C" } />
  <@spring.formSingleSelect path="foo.bar" options />

  <input type="submit" value="Send" />
</form>

现在,表单应将您的Foo实例的当前值(在本例中为"B")呈现为选定的选项.

The form should now render the current value of the your Foo instance ("B" in our case) as the selected option.

到目前为止,太好了.最后一步(您最初要求的步骤):如果提交表单,Spring应该将提交的bar值绑定到您的foo实例.为此,只需将实例作为参数添加到您的请求方法中,然后使用@ModelAttribute再次对其进行注释.请注意,参数的名称在这里很重要,并且必须与Spring模型中实例的名称相匹配-在我们的示例中为foo:

So far, so good. Last step (the one you were originally asking for): If you submit your form, Spring should bind the submitted bar value to your foo instance. To achieve this, simply add the instance as a parameter to your request method and annotate it again with @ModelAttribute. Note that the name of the parameter is important here and must match the name of the instance in Spring's model - foo in our case:

@Controller
public class MyController {

   ....

   @RequestMapping("/request/path/")
   public String process(
     @ModelAttribute Foo foo) {  // Binds all submitted request params to matching properties of your foo instance
     // validate your foo instance and probably save it in a DB
     return "mytemplate";
   }

}

Voilá

这篇关于Freemarker-选择选项未保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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