@Controller public class DeptController { /** * 自动注入UserService * */ @Autowired @Qualifier("hrmService") private HrmService hrmService; /** * 处理/login请求 * */ @RequestMapping(value="/dept/selectDept") public String selectDept(Model model,Integer pageIndex, @ModelAttribute Dept dept){ System.out.println("selectDept -->>"); System.out.println("pageIndex = " + pageIndex); System.out.println("dept = " + dept); PageModel pageModel = new PageModel(); System.out.println("getPageIndex = " + pageModel.getPageIndex()); System.out.println("getPageSize = " + pageModel.getPageSize()); System.out.println("getRecordCount = " + pageModel.getRecordCount()); if(pageIndex != null){ pageModel.setPageIndex(pageIndex); } /** 查询用户信息 */ List<Dept> depts = hrmService.findDept(dept, pageModel); model.addAttribute("depts", depts); model.addAttribute("pageModel", pageModel); return "dept/dept"; }修改这段代码
时间: 2024-03-05 10:49:54 浏览: 151
这段代码中的问题可能是:
1. 在DeptController类中,没有引入HrmService类的包,需要在类头部添加import语句。
2. 在使用PageModel类时,需要先实例化一个PageModel对象,然后再设置pageIndex等属性值,否则会报空指针异常。
3. 在调用findDept()方法时,需要将pageModel作为参数传入,否则查询结果可能不正确。
修改后的代码如下:
```
import com.example.demo.service.HrmService;
import com.example.demo.entity.Dept;
import com.example.demo.util.page.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class DeptController {
@Autowired
@Qualifier("hrmService")
private HrmService hrmService;
@RequestMapping(value="/dept/selectDept")
public String selectDept(Model model,Integer pageIndex,@ModelAttribute Dept dept){
System.out.println("selectDept -->>");
System.out.println("pageIndex = " + pageIndex);
System.out.println("dept = " + dept);
PageModel pageModel = new PageModel();
if(pageIndex != null){
pageModel.setPageIndex(pageIndex);
}
System.out.println("getPageIndex = " + pageModel.getPageIndex());
System.out.println("getPageSize = " + pageModel.getPageSize());
System.out.println("getRecordCount = " + pageModel.getRecordCount());
/** 查询部门信息 */
List<Dept> depts = hrmService.findDept(dept, pageModel);
model.addAttribute("depts", depts);
model.addAttribute("pageModel", pageModel);
return "dept/dept";
}
}
```
请注意,这里只是对代码进行了简单修改,具体是否能够运行还需要根据实际情况进行调试。
阅读全文