Spring整合SpringMVC框架

Spring整合SpringMVC框架

springmvc.xml配置注解扫描

基本配置参照点此链接

  • 细节
<!--开启注解扫描,只扫描Controller注解-->
    <context:component-scan base-package="com.wl">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
<a href="account/findAll">测试</a>
package com.wl.controller;

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

/**
 * 账户web
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    
    

    @RequestMapping("/findAll")
    public String finaAll() {
    
    
        System.out.println("表现层,查询所有的账户信息...");
        return "list";
    }
}

运行结果
在这里插入图片描述
开始整合

配置监听器

<!--配置Spring的监听器 默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--设置配置文件的路径-->
    <!--但是由于SpringConfig.xml配置文件不在WEB-INF目录下,所以需要指定路径-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
package com.wl.controller;

import com.wl.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 账户web
 */
@Controller
@RequestMapping("/account")
public class AccountController {
    
    

    @Autowired
    private AccountService accountService;

    @RequestMapping("/findAll")
    public String finaAll() {
    
    
        System.out.println("表现层,查询所有的账户信息...");
        //调用service的方法
        accountService.findAll();
        return "list";
    }
}

运行结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_51600120/article/details/113762363