栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

读 Spring Boot实战笔记--day004

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

读 Spring Boot实战笔记--day004

接上一节:读 Spring Boot实战笔记–day003
其他配置

viewController 配置:

package com.example.springboot.mvc;

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


@Controller
public class ControllerH {

    // 通过ModelAttribute 注解获取参数 msg
    @GetMapping("index")
    public void index(@ModelAttribute("msg") String msg){
    }

    // 跳转主页面 可以配置在是视图配置里
    @RequestMapping("/index")
    public String toIndex(){
        return "index";
    }
}

package com.example.springboot.mvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;


@Configuration
@EnableWebMvc // 开启 webmvc
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //addResourceLocations 指的是文件放置的目录,addResourceHandler指的是对外暴露的访问路径。
        registry.addResourceHandler("/assets
@Configuration
@EnableWebMvc // 开启 webmvc
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //addResourceLocations 指的是文件放置的目录,addResourceHandler指的是对外暴露的访问路径。
        registry.addResourceHandler("/assets
@Controller
public class ControllerH {

    @PostMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file){
        // 编写上传逻辑
        return "ok";
    }
}
package com.example.springboot.mvc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;


@Configuration
@EnableWebMvc // 开启 webmvc
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //addResourceLocations 指的是文件放置的目录,addResourceHandler指的是对外暴露的访问路径。
        registry.addResourceHandler("/assets/**").addResourceLocations("class:/assets/");
    }

    @Bean
    public MyInterceptor interceptor(){
        return new MyInterceptor();
    }

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("WEB-INF/class/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    //  拦截所有请求
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor());
    }

    // 直接配置在视图配置里
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("index").setViewName("index");
        // 调用 toUpload 会转到 upload 方法
        registry.addViewController("toUpload").setViewName("upload");
    }

    // 重写方法
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.setUseSuffixPatternMatch(false);
    }
    // 配置 bean 对象
    @Bean
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        // 文件最大为 10M
        resolver.setMaxUploadSize(10 * 1024 * 1024);
        return resolver;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571596.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号