springMVC(十一)

springmvc整合jsp

1.webapp 目录下创建 WEB-INF/jsp 存放jsp

2.控制类

package com.painter.controller;

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


/**
 * @Author: Painter
 * @project_name: SpringMVC
 * @system_login: sunshine
 * @time: 2022/10/1117:00
 */


@Controller
@RequestMapping("/jsp")
public class JspController {

    /**
     *  没有添加@ResponseBody 注解时
     */

    @RequestMapping("/jsp")
    public String painterJsp(){
        return "test";  // 自动到配置的目录下指定的文件名test.jsp
    }
}

3.配置类

package com.painter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * @Author: Painter
 * @project_name: SpringMVC
 * @system_login: sunshine
 * @time: 2022/10/916:00
 */


/**
 * @Configuration  等同于创建以个xml配置文件
 * @ComponentScan("com.painter.controller")  将com.painter.controller包下的所有类注入到ioc容器中
 *
 * 在springmvc原理 所有请求过来先达到我们的 DispatcherServlet 分发具体控制类 方法执行
 */
@Configuration
@ComponentScan("com.painter.controller")
@EnableWebMvc
public class SpringMVCConfig implements WebMvcConfigurer {  // 配置类
    /**
     * 1.@Configuration  定义SpringMVCConfig.xml配置文件
     * 2.需要将我们的控制类注入到ioc容器 @ComponentScan("com.mayikt.controller")
     * *@ComponentScan("com.mayikt.controller")将该包下所有的类 注入到IOC容器种
     * 3.在springmvc原理 所有请求过来先达到我们的 DispatcherServlet 分发具体控制类 方法执行
     *
     *
     *    @Configuration
     *     SpringMVCConfig.java   @Configuration
     *     springmvc.xml=== SpringMVCConfig.java
     *     -->
     */

    //WebMvcConfigurer
    @Bean
    public InternalResourceViewResolver resourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        //请求视图文件的前缀地址
        internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
        //请求视图文件的后缀
        internalResourceViewResolver.setSuffix(".jsp");

        internalResourceViewResolver.setExposeContextBeansAsAttributes(true);
        return internalResourceViewResolver;
    }

    /**
     * 视图配置
     *
     * @param registry
     */
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(resourceViewResolver());
    }
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

4.注册配置配置类

package com.painter.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

/**
 * @Author: Painter
 * @project_name: SpringMVC
 * @system_login: sunshine
 * @time: 2022/10/916:05
 */


public class ServletInitializer extends AbstractDispatcherServletInitializer {  // 注册配置类
    @Override
    protected WebApplicationContext createServletApplicationContext() {

        AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
        // 注册我们的 springmvc config 配置类
        annotationConfigWebApplicationContext.register(SpringMVCConfig.class);
        return annotationConfigWebApplicationContext;
    }

    @Override
    protected String[] getServletMappings() {

        return new String[]{"/"};
    }

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

访问 http://127.0.0.1:8080/jsp/jsp

会自动在 WEB-INF/jsp找指定的test.jsp文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_painter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值