1、SpringMVC中通过@ResponseBody、@RequestParam默认值,@PathVariable的用法

package com.kuman.cartoon.controller.friendsfuns;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.util.CollectionUtils;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.SessionAttributes;

import com.kuman.cartoon.common.Page;

import com.kuman.cartoon.common.controller.BaseController;

import com.kuman.cartoon.entity.Friend;

import com.kuman.cartoon.entity.User;

import com.kuman.cartoon.service.UserService;

/**

* "我的好友"和"我的粉丝"部分的代码

*

* 修改原来的好友和粉丝部分的代码

*

* @author to.to

*/

@Controller

@RequestMapping(value = "/friendsAndFuns", method = { RequestMethod.GET,

RequestMethod.POST })

@SessionAttributes(value = { "accountid", "account" })

public class FriendsAndFuns extends BaseController {

@SuppressWarnings("unused")

private static final Logger LOGGER = LoggerFactory

.getLogger(FriendsAndFuns.class);

@Autowired

UserService userService;

/**

* 跳转到"我的好友"和"我的关注页面"

*

* @param userId

*            用户登录用的Id

* @param friendId

*            登录用户的好友的Id

* @param pageNo

*            当前页码数

* @param pageRow

*            每页显示条数

* @return

*/

@RequestMapping(value = "/toFriendFuns/{userId}")

public String toFriendFuns(

Model model,

@PathVariable("userId") int userId,

@RequestParam(value = "pageNo", required = false, defaultValue = "1") int pageNo,

@RequestParam(value = "pageRow", required = false, defaultValue = "12") int pageRow,

@RequestParam(value = "friendId", required = false, defaultValue = "-1") int friendId) {

// 1、判断用户是否登陆过了,如果登录过了直接进入到后续过程

User user = isLogin(model);

if (null == user) {

return "redirect:/user/login";

} else {

// 如果有了直接调转到登陆页面

// LOGGER.info("friendId = " + friendId);

// LOGGER.info("pageNo = " + pageNo);

// LOGGER.info("pageRow = " + pageRow);

model.addAttribute("user", user);

model.addAttribute("friendId", friendId);

model.addAttribute("pageNo", pageNo);

model.addAttribute("pageRow", pageRow);

return "/web/friendsfuns/friendfuns";

}

}

/**

* 显示关注的用户

*

* @param map

* @param userId

* @param pageNo

* @param pageRow

* @return

*/

@SuppressWarnings({ "unchecked", "rawtypes" })

@RequestMapping(value = "/follow/{userId}", produces = { "text/html" })

public String showFollow(

Map<String, Object> map,

Model model,

@PathVariable("userId") int userId,

@RequestParam(value = "friendId", required = false, defaultValue = "-1") int friendId,

@RequestParam(value = "pageNo", required = false, defaultValue = "1") int pageNo,

@RequestParam(value = "pageRow", required = false, defaultValue = "12") int pageRow) {

// 1、判断用户是否登录了,如果登录了直接查询,如果没有要跳转到登录页面

User loginedUser = isLogin(model);

if (null == loginedUser) {

return "redirect:/user/login";

} else {

User user = userService.queryUser(userId, false);

Page page = userService.queryUserByFansId(pageNo, pageRow,

user.getUserId());

// 关注的好友

List<Object> fUserList = new ArrayList<Object>();

for (Object o : page.getRecordList()) {

Friend friend = (Friend) o;

// 关注的好友

User fUser = friend.getUser();

// 关注的好友所关注的好友

List<Friend> friendList = userService.queryUserByFansId(fUser

.getUserId());

if (!CollectionUtils.isEmpty(friendList)) {

fUser.setFollowCount(friendList.size());

}

fUserList.add(friend);

}

page.setRecordList(fUserList);

// 用户信息

map.put("user", user);

map.put("page", page);

return "/web/friendsfuns/follow";

}

}

/**

* 用于模拟跳转到"动态管理"页面中去

*

* @param model

* @return

*/

@RequestMapping(value = "/toUserDy/{userId}")

public String toUserDy(Model model, @PathVariable("userId") int userId) {

return "/web/friendsfuns/userdy";

}

/**

* 获取粉丝信息

*

* @param map

* @param userId

* @param pageNo

* @param pageRow

* @return

*/

@SuppressWarnings({ "rawtypes", "unchecked" })

@RequestMapping(value = "/funs/{userId}", produces = { "text/html" })

public String showFuns(

Map<String, Object> map,

Model model,

@PathVariable("userId") int userId,

@RequestParam(value = "friendId", required = false, defaultValue = "-1") int friendId,

@RequestParam(value = "pageNo", required = false, defaultValue = "1") int pageNo,

@RequestParam(value = "pageRow", required = false, defaultValue = "12") int pageRow) {

// 1、判断用户是否已经登录进去了,如果登录进去了,则可以查询自己的粉丝

User loginedUser = isLogin(model);

if (null == loginedUser) {

return "redirect:/user/login";

} else {

User user = userService.queryUser(userId, false);

Page page = userService.queryFansByUserId(pageNo, pageRow, userId);

// 粉丝

List<Object> fUserList = new ArrayList<Object>();

for (Object o : page.getRecordList()) {

Friend friend = (Friend) o;

// 粉丝

User fUser = friend.getFans();

// 粉丝所关注的好友

List<Friend> friendList = userService.queryUserByFansId(fUser

.getUserId());

if (!CollectionUtils.isEmpty(friendList)) {

fUser.setFollowCount(friendList.size());

}

fUserList.add(friend);

}

page.setRecordList(fUserList);

map.put("user", user);

map.put("page", page);

return "/web/friendsfuns/funs";

}

}

}

SpringMVC中通过@ResponseBody返回对象,Js中调用@ResponseBody返回值,统计剩余评论字数的js,@RequestParam默认值,@PathVariable的用法的更多相关文章

  1. hibernate 返回对象指定属性,需要返回的列,可以直接返回 对象属性

    // hibernate 返回对象指定属性,需要返回的列,可以直接返回 对象属性 @Override public TeamPlan getTeamPlanByBaoMingId(String bao ...

  2. [Effective C++ --021]必须返回对象时,别妄想返回其reference

    引言 在条目20中,我们知道了值传递和引用传递的效率问题,因此在设计程序时,我们可能就尽可能来返回引用而不是值. 可是,可能会犯下面的一些错误:传递一些引用指向其实并不存在的对象. 第一节:返回临时变 ...

  3. 条款21: 必须返回对象时,不要强行返回对象的reference

    总结: 绝不要返回一个local栈对象的指针或引用:绝不要返回一个被分配的堆对象的引用:绝不要返回一个静态局部对象(为了它,有可能同时需要多个这样的对象的指针或引用). 条款4中给出了“在单线程环境中 ...

  4. 条款21:必须返回对象object时,不要返回其引用reference

    如下为一个有理数类,其中包含一个用来计算乘积的函数: #include <iostream> using namespace std; class Rational { public: R ...

  5. 【21】必须返回对象时,别妄想返回器reference

    1.考虑有理数Rational,有个友元操作符*,返回Rational对象.返回对象,导致临时对象的构造,析构.效率低,因此会想返回方法内局部对象的引用,这种方法不可行.为什么? 2.调用方法是在st ...

  6. 切记切记:Spring配置文件中,Component-scan无法扫描到的类中的自动装配对象无法被调用,报空指针错误。

    Spring单例注入,单例对象可设置成Spring元件. 只有Spring的元件中@Autowired才有用,在普通类中@Autowired虽然不会编译报错,但运行时会报空指针错误.

  7. C#中获取多个对象list中对象共有的属性项

    场景 有一组数据list<TestDataList> 每一个TestDataList是一个对象,此对象可能有温度数据,也可能没有温度数据. 有温度数据的情况下,温度数据属性又是一个list ...

  8. objective-c中自己创建的对象为什么不能调用release

    dealloc方法,本就不应该手动调用. 你自己创建的对象,看你代码怎么写的了.例子:NSString *str1 = [NSString stringWithString:@"abc&qu ...

  9. Effective C++ -----条款21:必须返回对象时,别妄想返回其reference

    绝不要返回pointer或reference指向一个local stack对象,或返回reference指向一个heap-allocated对象,或返回pointer或reference指向一个loc ...

随机推荐

  1. Webpack 4 Tutorial: from 0 Conf to Production Mode

    webpack 4 is out! The popular module bundler gets a massive update. webpack 4, what's new? A massive ...

  2. Linux(十七)动态监控进程

    17.1 介绍 top与ps命令很相似.它们都用来显示正在执行的进程.top与ps最大的不同之处,在于top在执行一段时间可以更新正在运行的进程 17.2 语法 top    [选项] 常用选项   ...

  3. Java内存泄漏分析系列之三:jstat命令的使用及VM Thread分析

    原文地址:http://www.javatang.com 使用jstat命令 当服务器CPU100%的时候,通过定位占用资源最大的线程定位到 VM Thread: "VM Thread&qu ...

  4. Mybatis 批量插入、批量更新

            合理的使用批量插入.更新对性能优化有很大的作用,速度明显快了N倍.         要注意数据库连接串后面要新增:&allowMultiQueries=true,表示一个sql ...

  5. 拾遗与填坑《深度探索C++对象模型》3.3节

    <深度探索C++对象模型>是一本好书,该书作者也是<C++ Primer>的作者,一位绝对的C++大师.诚然该书中也有多多少少的错误一直为人所诟病,但这仍然不妨碍称其为一本好书 ...

  6. pthon核心编程-读书笔记:知识点摘录与总结(方便理解和快速记忆)

    Python 中的列表(大小可变的数组)和字典(哈希表)就是内建于语言本身的.在核心语言中提供这些重要的构建单元,可以鼓励人们使用它们, 缩短开发时间与代码量,产生出可读性更好的代码.C不提供, c+ ...

  7. Redis 学习笔记3:Jedis 连接虚拟机下的Redis 服务

    Jedis 是 Redis 官方首选的 Java 客户端开发包. 虚拟机的IP地址是192.168.8.88. Jedis代码是放在windows上的,启动虚拟机上的Redis服务之后,用Jedis连 ...

  8. 我的第一个RootKit,支持XP、Vista、Win7、Win8 RTM 32位

    只有写过一个BootKit,才能比较深刻的理解其整个过程与机制,也能加深对Windows系统引导各个过程的熟悉和理解. 我写的这个bootkit,暂时还没想到一个比较好的名字,它 1.  支持xp到w ...

  9. [Flask]学习杂记一 Hello程序

    这几天买了本  <Flask Web开发:基于Python的Web应用开发实战>,之前也用过flask 但是不怎么系统,有时候需要搭建一些临时的测试服务,用falsk比较方面,一个文件就可 ...

  10. 协议系列之TCP/IP协议

    根据前面介绍的几种协议,将IP协议.TCP协议.UDP协议组合起来,于是便有了TCP/IP协议.现在很多的应用的通信都是建立在TCP/IP协议的基础上,运用非常广泛,很有必要对其学习一下. 打个不太恰 ...