春季@RequestParam与类 [英] Spring @RequestParam with class

查看:137
本文介绍了春季@RequestParam与类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SpringHiberate创建简单的Web服务.在Controller中,我有很长的表格形式的args.

I use Spring and Hiberate to create simply web service. In Controller i have long list of args from form.

我的控制器

 @RequestMapping( value="/addAccount", method=RequestMethod.POST )
public String addAccoundPOST( @RequestParam( value = "username" )   String username,
                              @RequestParam( value = "password" )   String password,
                              @RequestParam( value = "email" )      String email,
                              @RequestParam( value = "name" )       String name, 
                              @RequestParam( value = "surname" )    String surname,
                              @RequestParam( value = "birth_date" ) String birth_date, 
                              @RequestParam( value = "place" )      String place,
                              @RequestParam( value = "province" )   String province,
                              @RequestParam( value = "motorcycle" ) String motorcycle,
                              @RequestParam( value = "tel" )        String tel ) 
{
    User user = new User();

    if( username != null && username.length() >= 4 )
        user.setUsername( username );

    if( password != null && password.length() >= 4 )
        user.setPassword( password );

    // and more, and more, and mode....

    return "addAccount";
}

User类的一部分:

Part of User class:

@Entity
@Table( name = "users", catalog = "dbjadenazlot" )
public class User implements Serializable {
private int uid;

private String ...;
/* --------------------------------- GET ------------------------------ */

@Id
@GeneratedValue(strategy = IDENTITY)
@Column( name = "uid", unique = true, nullable = false )
public int getUID()             
    { return uid;}

@Column( name = "name", length = 45 )
public String getName()         
    { return name; }
// setters and rest code.. 
}

是否可以将我的参数从POST请求转换为类?如果可能,需要什么?

Is it possible to convert my arguments from POST request to class? If it's possible, what is required?

 @RequestMapping( value="/addAccount", method=RequestMethod.POST )
public String addAccoundPOST( @RequestParam( value = "user") User user ) 
{

    return "addAccount";
}

我如何编写简短的代码?

How can i write shorten code?

推荐答案

首先,在您的第一个示例中,

First of all, in your first example,

if( username != null && username.length() >= 4 )

不需要检查null.如果@RequestParam无法解析,Spring将返回400状态代码.

checking for null is not necessary. Spring will return a 400 status code, if a @RequestParam cannot be resolved.

是否可以将我的参数从POST请求转换为类?如果可能,需要什么?

it is possible to convert my arguments from POST request to class? If it's possible, what is required?

是的,创建一个类,可能是您的User类,该类具有与请求参数共享相同名称的字段.让您的处理程序方法接受该类的参数.

Yes, create a class, possibly your User class, which has fields that share the same name as the request parameters. Make your handler method accept a parameter of that class.

public String addAccoundPOST( @ModelAttribute User user ) 

如果要验证它,只需添加@Valid.

If you want to validate it, just add @Valid.

public String addAccoundPOST( @Valid @ModelAttribute User user ) 

这篇关于春季@RequestParam与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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