搜索
java - Spring MVC参数传递问题
天蓬老师
天蓬老师 2017-04-17 14:57:16
[Java讨论组]

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

@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为什么没有把值传递进去呀?以及怎样才是正确的写法?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
ringa_lee

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)};
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号