springboot静态资源和常用参数


今天解决了许多在学习过程中应用知识点的问题,解决方法见上图中浏览器搜索的方法。有许多版本不兼容的问题。
主界面是一个外部依赖jquery的应用。还有后端的可视化检测视图,中间要把端点全部都暴露出来。但是我的可视化显示没有在线运行的状态,有些苦恼。
还有junit5的单元测试的使用方法,比如对于注解的使用。
还有springboot整合maybatis的练习,有三种方法,最简单的就是引入mybatid-plus依赖(要注意版本!!),写一个mapper包下的接口就可以使用,不用做其他的配置和service层接口和实现类的书写。也有较简单的一种,用注解来实现mybatis的整合。最麻烦的要有mybatis的核心配置文件和映射文件,并且一个mapper接口对应一个映射文件。service层的实现类调用mapper包下的接口(用@Autowaried注解),controller层再调用service的接口实现操作,同理。controller层的控制类方法用@RestMapper注解开头,return的字符串会显示到界面上,打印的东西会输出到控制台上。
一、页面动态资源和静态资源的先后顺序:

是返回a.html中的内容还是返回hello?
是返回动态资源hello,优先级大于静态资源。
如果想返回静态资源a.html中的内容,需要额外配置路径。在application.properties中加入如下东西:

#自定义静态资源的映射规则(配置前缀)
spring.mvc.static-path-pattern=/hello/**

访问:localhost:8080/hello/a.html
二、在resources下访问除了static的其他包的html文件,在application.properties中加入如下配置:

#自定义静态资源的存放目录
spring.web.resources.static-locations=classpath:/hello/

一定要格式正确,我因为对classpath:/hello/加了中括号导致配置不能实现功能
==写多少个classpath就会有多少个classpath下的文件能生效,static包也是如此
三、程序的主页面和页面左上角的图标
位置:static包下

意思:
index.html是主页面,访问localhost:8080会显示
图标文件命名为:favicon.ico,这样就会自动启用
效果:
四、注解
1.@RestControlle
等价于@Controller和@ResponseBody的组合

参数的常用注解
2.@PathVariable

控制台打印输出localhost:8080/aa/123/mm/456中的值
注解写在形参的前面,并且小括号还要写东西
可以通过localhost请求来制定对应的值


    /**
     * 测试PathVariable注解
     * @param userId
     * @param num
     * @return
     */
    //localhost:8080/aa/123/mm/456
    @RequestMapping ("aa/{userId}/mm/{num}")
    public String testPathVariable(@PathVariable("userId") String userId,@PathVariable("num") Integer num) {
        System.out.println(userId);
        System.out.println(num);
        return "hello";
    }


3.@RequestParam

控制台打印输出localhost:8080/testRequestParam?username=张三&age=18&address=北京的值
形参接受浏览器传递过来的数据

/**
     * 测试RequestParam注解
     * @param name
     * @param age
     * @param address
     * @return
     */
    //localhost:8080/testRequestParam?username=张三&age=18&address=北京
    @RequestMapping("testRequestParam")
    public String testRequestParam(@RequestParam("username") String name,@RequestParam("age") Integer age,
                                   @RequestParam("address") String address){
        System.out.println(name);
        System.out.println(age);
        System.out.println(address);
        return "hello";
    }

4.@RequestBody

浏览器访问Demo1.html文件,点击提交:

以字符串形式显示表单传过来的数据
Demo1.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--提交实体内容-->
<form action="getData" method="post">
    <input type="text" name="username"> <br>
    <input type="password" name="password"><br>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
@RequestMapping("getData")
    public String getData(@RequestBody String data){
        return data;
    }

表单提交getData请求到控制器中去
5.@CookieValue
处理前端的cookie,接收cookie数据

@RequestMapping("testCookieValue")
    public String testCookieValue(@CookieValue("Idea-aafc98fb") String cookie){
        System.out.println(cookie);
        return "hello";
    }

6.@RequestHeader
同5,接收并存储请求头

 @RequestMapping("testRequestHeader")
    public String testRequestHeader(@RequestHeader("Accept") String userAgent){
        System.out.println(userAgent);
        return "success";
    }

7.@RequestAttribute
//转发并实现数据的共享

    @RequestMapping("test1")
    public String test1(HttpServletRequest request){
        request.setAttribute("username","张三");
        return "forward:/test2";
    }

    /**
     * @RequestAttribute注解,用来获取Request里面的值,修饰控制器方法的参数
     * @param username
     * @return
     */
    @RequestMapping("test2")
    public String test2(@RequestAttribute("username") String username){
        System.out.println(username);
        return "username";
    }
posted @ 2025-04-28 21:55  f-52Hertz  阅读(21)  评论(0)    收藏  举报