spring与springMVC整合

修改web.xml

将spring和springMVC配置文件一同加载

<init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml,classpath:applicationContext.xml</param-value>
    </init-param>

修改spring配置文件

防止重复扫描controller包

  <!--扫描包注解 : 不需要手动的在xml中进行配置类 而通过注解即可 spring在扫描包的时候 忽略 controller层-->
    <context:component-scan base-package="com.imust">
        <!--exclude 不包含-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

修改UserController.java

给前端返回视图

package com.imust.controller;

import com.imust.domain.User;
import com.imust.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        List<User> userList = userService.findUserList();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList",userList);
        modelAndView.setViewName("success");
        return modelAndView;
    }
}

修改前端页面

用来接收数据

 ${userList}

运行结果

posted @ 2020-03-15 20:04  搞点薯条  阅读(231)  评论(0编辑  收藏  举报