struts2 注解 返回json

来源:互联网 发布:2017一手收藏品数据 编辑:程序博客网 时间:2024/05/29 01:52

找了好久才找到。。。哎。。。记录一下吧!

首先在struts框架基础上引入jar包:struts2-json-plugin-2.3.15.1.jar,struts2-convention-plugin-2.3.15.1.jar,

然后看下面代码啦~返回的就是userList的json字符串了

@Namespace("/user")
@Results( { @Result(name = ActionSupport.SUCCESS, type = "json"),
@Result(name = ActionSupport.ERROR, type = "json") })
@SuppressWarnings("serial")
public class UserAction extends ActionSupport{

@Autowired    
    private UserService userSerivce; 

private List<UserBean> userList;

@Action(value="getUserByName", results={@Result(type="json", params={"root","userList"})})
public String getUsers(){
userList = userSerivce.getUsers(); 
return SUCCESS;
}

public List<UserBean> getUserList() {
return userList;
}


public void setUserList(List<UserBean> userList) {
this.userList = userList;
}

}

1 3
原创粉丝点击