简体   繁体   English

Spring @ModelAttribute和翻译请求参数绑定名称

[英]Spring @ModelAttribute and translating request parameter binding names

I'm working on converting a legacy project to Spring (trying to adjust little as possible for now) and I'm running into a small issue with mapping/translating legacy parameters to a model attribute object. 我正在努力将遗留项目转换为Spring(现在尝试尽可能少地调整)并且我遇到了将遗留参数映射/转换为模型属性对象的小问题。 I may be completely wrong in thinking about this problem but it appears to me that to translate a parameter to a specific model attribute setter is to pass in the request parameter through a method for creating a model attribute and manually call the correct setter: 我在思考这个问题时可能完全错了,但在我看来,将参数转换为特定的模型属性setter是通过创建模型属性的方法传递request参数并手动调用正确的setter:

@ModelAttribute("form")
public MyForm createMyForm(@RequestParameter("legacy-param") legacy) {
    MyForm myForm = new MyForm();
    myForm.setNewParam(legacy);
    return myForm;
}

I don't necessarily want to change the request parameter name yet since some javascript and JSPs are depending on it being named that way but is there any way to do something like this? 我不一定要更改请求参数名称,因为某些javascript和JSP依赖于这种命名方式,但是有什么办法可以做这样的事情吗? Or is there a different way to map/translate request parameters to model attributes? 或者是否有不同的方法将请求参数映射/转换为模型属性?

public class MyForm {
    @ParameterName("legacy-param")
    private String newParam;

    public void setNewParam(String value) { ... }

    public String getNewParam() { ... }
}

@Controller
public class MyController {

    @RequestMapping("/a/url")
    public String myMethod(@ModelAttribute("form") MyForm myForm, BindingResult result) { ... }
}

The way you've written that model attribute method is indeed odd. 您编写模型属性方法的方式确实很奇怪。 I'm not entirely clear what you're actually trying to do.Assuming there are many parameters, you're going to end up with an awful lot of instances of MyForm in your ModelMap. 我并不完全清楚你实际上要做什么。假设有很多参数,你最终会在ModelMap中得到大量的MyForm实例。 A more 'normal' way to create model attribute would be like this: 创建模型属性的一种更“正常”的方式是这样的:

@ModelAttribute("legacyParamNotCamel")
public MyForm createMyForm(@RequestParameter("legacy-param-not-camel") String legacy) {
    return legacy;
}

Then in the JSP you can refer to it directly in expression language. 然后,在JSP中,您可以直接在表达式语言中引用它。 eg, 例如,

<c:out value="${legacyParamNotCamel}"/>

If you want to put them onto a form backing object, you need to do it all in a single method that creates the object, not make new copies of it in each method. 如果要将它们放在表单支持对象上,则需要在创建对象的单个方法中完成所有操作,而不是在每个方法中创建它的新副本。 (assuming your form has more than a single parameter associated with it.) (假设您的表单具有多个与之关联的参数。)

-- It seems like what you're really trying to do though is translate the parameter names in the request before the web data binder gets ahold of it, so that you can bind oddly named parameters onto a java bean? - 看起来你真正想要做的就是在Web数据绑定器获取它之前转换请求中的参数名称,这样你就可以将奇怪命名的参数绑定到java bean上了? For that you'll need to use an interceptor that translates the names before the binding process begins, or make your own subclass of the databinder than can take a property name translation map. 为此,您需要使用在绑定过程开始之前转换名称的拦截器,或者创建自己的数据库子类,而不是使用属性名称转换映射。

You placed the @ModelAttribute at the Method Level but the intention seems to be more of a formBackingObject hence we should be dealing at the Method Parameter Level 你把@ModelAttribute放在了Method Level,但是意图似乎更像是一个formBackingObject,因此我们应该处理Method Parameter Level

There's a difference. 有区别。

I put up an explanation here on my blog along examples at Spring 3 MVC: Using @ModelAttribute in Your JSPs at http://krams915.blogspot.com/2010/12/spring-3-mvc-using-modelattribute-in.html 我在Spring 3 MVC上的示例中给出了一个解释:在JSP使用@ModelAttribute网址http://krams915.blogspot.com/2010/12/spring-3-mvc-using-modelattribute-in.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM