weixin_45137110 2021-05-19 22:22 采纳率: 50%
浏览 56

SSM框架无法获得数据库连接

写了测试类,后台可以查询到记录,但是通过访问浏览器,经过controller层时就报如上错。

public class testController {
    @Test
    public void test1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        RoleService roleService = applicationContext.getBean(RoleService.class);
        List<Role> list = roleService.list();
        for (Role l:list
             ) {
            System.out.println(l);
        }
    }
}

后台可以查询到记录:

Controller:

@RequestMapping("/role")
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView();
        List<Role> roleList = roleService.list();
        //设置模型
        modelAndView.addObject("roleList",roleList);
        //设置视图
        modelAndView.setViewName("role-list");

        return modelAndView;
    }

请求其他的controller可以正常访问,说明MVC是通的,但是访问这个就不行。

报错:

 

  • 写回答

2条回答 默认 最新

  • java通俗易懵 2021-05-20 09:58
    关注

    你可以试一下用Model model视图和@ResController搭配,返回roleList的json

    评论

报告相同问题?