首页 > Spring MVC参数传递问题

Spring MVC参数传递问题

现在的大部分控制层代码是

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public ModelMap listXXX(
//     @RequestParam(defaultValue = "id") String sort,
//     @RequestParam(defaultValue = "desc") String order,
//     @RequestParam(defaultValue = "0") int begin,
//     @RequestParam(defaultValue = "50") int pageNum
//     ...
){
    return xxx.(sort,order,begin,pageNum,...);
}

想问可不可以把传递的参数替换成一个pojo对象?譬如以下

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public ModelMap listXXX(
//     SearchForm searchForm
){
    return xxx.(searchForm);
}

主要问题在于几个defaultValue的值,用了@Value(${xxx:yyy})完全不起作用……请问Spring为什么没有把值传递进去呀?以及怎样才是正确的写法?


Spring注解@value通常用来获取*.porperties文件的内容。使用@value方式必须在当前类使用@Component,xml文件内配置通过pakage扫描方式例如:<context:component-scan base-package="pakage_name" />,
这样@value就可以取到值赋值给所标注的变量。
Spring允许把传递的参数替换成一个pojo对象。我们项目目前的分页参数也是和楼主最上边的写法一致,并没有使用分页的POJO类去接收,就是因为默认值设置的原因。你的@Value(${xxx:yyy})不起作用,我猜很可能对应的类没有使用@Component注解。还是建议上面那种写法


我看微博的api都是封装了一个wapper对象,有Lisg count start limit等属性。


可以

@RequestMapping(value = "/loginDo", method = RequestMethod.POST)
    @ResponseBody
    public errorEntity loginDo(@RequestBody UserInfoEntity user,HttpServletRequest request,HttpServletResponse response) {

POJO类

public class UserInfoEntity {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

ajax 传递的参数

{"username":username,"password":hex_sha1(password)};
【热门文章】
【热门文章】