同一个Controller里的同一个Service实例,在当前的Controller里的不同方法中状态不一致
来源:互联网 发布:福建顶点软件 垃圾 编辑:程序博客网 时间:2024/06/05 08:32
直接上代码如下:
@Controller
@RequestMapping("/views/information")
public class PubContentController extends BaseController{
@Autowired
private ContentCategoryService contentCategoryService;
/**
* 新增资讯
*
* @param pubContent
* @param request
* @param response
* @return
* @throws Exception
*/
@RequestMapping(value = "/pubContent_add", method = RequestMethod.POST)
@AuthAnnotation(authStr = "func_news_add")
@LogAnnotation(module = "资讯管理", act = "新增")
@ResponseBody
public Object add(@ModelAttribute("pubContent") PubContent pubContent, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String currUserId = currUserId(request);
String title = pubContent.getTitle().trim();
if (StringHelper.isEmpty(title, true))
throw new ParameterException(MsgConstant.EXCEPTION_NAME_NULL);
//根据分类ID获取资讯分类,再判断是一级或二级分类
String categoryId = pubContent.getSubid();
Map<String,Object> params = Maps.newConcurrentMap();
if (!StringHelper.isEmpty(categoryId, true)){
params.put("id", categoryId);
}else{
throw new ParameterException(MsgConstant.EXCEPTION_CATEGORYID_NULL);
}
ContentCategory category = contentCategoryService.get("get", params);
String id = category.getId();
//父id
String pid = category.getPid();
if(StringHelper.isEmpty(pid, true) || pid.equals("0") ){
//没有父id,是一级分类
//设置一级分类id
pubContent.setCid(id);
//设置二级分类id
pubContent.setSubid("");
}else{
//有父id,是二级分类
//设置一级分类id
pubContent.setCid(pid);
//设置二级分类id
pubContent.setSubid(id);
}
pubContent.setCreateBy(currUserId);
pubContent.setUpdateBy(currUserId);
int addid= pubContentService.insert(pubContent);
if (addid <= 0) {
return new Result(CodeConstant.RETCODE_500, null, null, MsgConstant.ERROR);
}
//更新新上传的附件
uploadPubAttachment(request, response, pubContent.getId()+"");
CheckLog checkLog=new CheckLog();
checkLog.setPubid(addid);
checkLog.setCreateBy(currUserId);
checkLog.setNote("");
checkLog.setStatus(0);
checkLog.setUserName(currUser(request).getUserName());
checkLogService.insert(checkLog);
return new Result(CodeConstant.RETCODE_200, null, null, MsgConstant.SUCCESS);
}
/**
* 获取
*
* @param id
* @return category
*/
@RequestMapping(value = "/get_Category", method = RequestMethod.POST)
@ResponseBody
private ContentCategory get_Category(@RequestParam(value = "id", required = true) String id,
HttpServletRequest request,
HttpServletResponse response, Model model)throws Exception {
Map<String,Object> params = Maps.newConcurrentMap();
if (!StringHelper.isEmpty(id, true)){
params.put("id", id);
}
ContentCategory category = contentCategoryService.get("get", params);
return category;
}
}
带下划线的service是同一个service,绿色代码处的是没有问题,在红色代码处出现java.lang.NullPointerException异常问题,
后面查看对象,id是有值的, params的Map也是有值的,最后发现 contentCategoryService 对象是空的,找了好久都没有发现
是什么问题导致了contentCategoryService是空的,而调用另一个方法的contentCategoryService是有实体对象的没有任何问题,
最后请教大神指点,经过多次的测试发现原来是 访问权限的问题,后一个contentCategoryService为空的方法是访问权限修饰符是private,
不为空的方法是访问权限修饰符是public,所以导致了contentCategoryService为空,一直空指针异常。(自己排除错误的时候一直没有
注意到访问权限修饰符)修改了修饰符就解决了问题。
/**
* 获取
*
* @param id
* @return category
*/
@RequestMapping(value = "/get_Category", method = RequestMethod.POST)
@ResponseBody
public ContentCategory get_Category(@RequestParam(value = "id", required = true) String id,
HttpServletRequest request,
HttpServletResponse response, Model model)throws Exception {
Map<String,Object> params = Maps.newConcurrentMap();
if (!StringHelper.isEmpty(id, true)){
params.put("id", id);
}
ContentCategory category = contentCategoryService.get("get", params);
return category;
}
复制粘贴,排除错误的时候记得要检查权限修饰的
- 同一个Controller里的同一个Service实例,在当前的Controller里的不同方法中状态不一致
- Springmvc 在同一个controller和不同的controller中进行重定向redirect和转发forward:
- Springmvc 在同一个controller和不同的controller中进行重定向redirect和转发forward
- 普元 EOS Platform 7.6 集群,同一个流程实例,不同节点在workspace中显示的状态不一致
- SpringMVC从Controller跳转到另一个Controller 同一个 Controller 跳转到不同的方法 新增 到显示用户信息列表
- 关于STRUCTS 中同一个ATION里定位不同方法的问题
- verilog里同一个寄存器问什么不能在不同的always里分别操作?
- 关于在多个controller中使用同一个函数的问题
- spring mvc 在同一个controller 中同时返回多种格式的数据 (xml json atom)
- 在控制台里打印controller的层级
- Controller里视图的出现,消失方法
- angular中controller在不同场景下的状态
- 同一个工程在两台电脑里用eclipse生成的apk的签名不同
- SpringAOP拦截Controller,Service实现日志管理(自定义注解的方式)里面的坑之 同一个controller部分方法可以拦截,部分方法拦截不了
- Yii里获取当前controller和action的id
- Yii里获取当前module controller action的id
- controller或者service层调用配置文件里的属性
- Swift 4 和 Objective-C 在同一个工程里的混搭编程的方法
- Ant工具与MySQL数据库
- java中String的“==”和equals判断相等性
- file(内存)----输入流---->【程序】----输出流---->file(内存)
- zurmo(八)-数据库表分析及某些功能的页面实现
- java基础之Java变量命名规范 (转载)
- 同一个Controller里的同一个Service实例,在当前的Controller里的不同方法中状态不一致
- 第一篇博客哦
- kali linux上为GPU安装显卡驱动
- Linux下安装DB2数据库步骤
- [读书笔记]30 天自制操作系统 day8 鼠标控制与32位模式切换
- lombok的安装以及使用
- weex sdk集成到Android工程二. weex sdk集成到Android工程
- opencv中Mat介绍
- zookeeper系列之通信模型