1.开发社区首页

开发社区首页

1.搭建开发环境

使用IntelliJ IDEA的Spring Initializr创建一个新的项目。


并勾选所需要的依赖,并创建项目。

图中红框中的文件暂时用不到,可以先删掉,也可以保留。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CSD16iZQ-1648453134423)(img\image-20220228142057293.png)]
进入启动类,并点击运行。



可以看到项目运行在8080端口,我们进入浏览器访问localhost:8080,会得到如下页面,因为这是一个新建的项目,什么也没有。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eN1ax01c-1648453134426)(img\image-20220228142706498.png)]

2.前置知识

基础测试

现在我们做一个简单的测试,给新建的项目提供一个简单的功能,并希望浏览器能够访问到。

  • 首先在com.nowcoder.community包下新建一个名为controller的包
  • 然后在controller包下新建一个HelloController类,并添加如下代码
package com.nowcoder.community.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "hello, springboot";
    }
}

最后启动项目,并访问http://localhost:8080/hello,得到如下结果。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nPKZfhZy-1648453134430)(img\image-20220228144045922.png)]
application.properties文件的功能是对项目进行一些配置,例如

server.port=8888
server.servlet.context-path=/community
spring.thymeleaf.cache=false #用于关闭模板引擎的缓存

上面两行代码表示项目启动在8888端口,并且项目的访问路径前都要加上/community,才能正确访问资源。此时,我们只有访问http://localhost:8888/community/hello才能得到hello, springboot。


CommunityApplication是一个主启动类也是一个配置类,如果我们想让CommunityApplicationTests也以CommunityApplication为配置类,可以将CommunityApplicationTests按照如下方式修改代码,并运行。

package com.nowcoder.community;

import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
class CommunityApplicationTests implements ApplicationContextAware {

    @Test
    void contextLoads() {
        System.out.println(applicationContext);
    }

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

会得到org.springframework.web.context.support.GenericWebApplicationContext@58326051,而输出的applicationContext就是spring容器。


继续做一个测试,让applicationContext去管理bean。

  • 首先在com.nowcoder.community包下新建一个名为dao的包
  • 然后在dao包下新建一个TestDao接口和TestDaoImpl类,并添加如下代码
package com.nowcoder.community.dao;

public interface TestDao {
    String test();
}
package com.nowcoder.community.dao;

import org.springframework.stereotype.Repository;

@Repository
public class TestDaoImpl implements TestDao{
    @Override
    public String test() {
        return "TestDaoImpl method";
    }
}
  • 最后在CommunityApplicationTests类的contextLoads方法中添加,如下两行代码并运行
TestDao dao = applicationContext.getBean(TestDao.class);
System.out.println(dao.test());

输出TestDaoImpl method

Spring的一些知识点

给Bean自定义名字:@Component(“名字”)

初始化方法@PostConstruct,在构造器之后调用. 销毁对象之前调用,@PreDestroy.

@Scope()指定单例或者多例

@Configuration配置类,用以装载使用第三方类.

自动注入:@Autowired

SpringMVC简单使用

在HelloController中添加以下代码,测试get请求

访问http://localhost:8888/community/http

@RequestMapping("/http")
public void http(HttpServletRequest request, HttpServletResponse response) {
    //获取请求数据
    System.out.println(request.getMethod()); //打印请求方法
    System.out.println(request.getServletPath()); //打印请求路径
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {//依次打印所有的请求头
        String name = headerNames.nextElement();
        String value = request.getHeader(name);
        System.out.println(name + "=" + value);
    }
    System.out.println(request.getParameter("message"));//获取请求携带的参数

    //返回响应数据
    response.setContentType("text/html;charset=utf-8"); //设置返回的响应数据类型
    try {
        PrintWriter writer = response.getWriter();
        writer.write("<h1>牛客网</h1>"); //设置浏览器显示的文本
    } catch (IOException e) {
        e.printStackTrace();
    }
}

访问http://localhost:8888/community/students?current=1&limit=20

//GET请求
//  /student?current=1&limit=20
@RequestMapping(path = "/students", method = RequestMethod.GET)
public String getStudents(
  @RequestParam(name = "current", required = false, defaultValue = "1") int current,
  @RequestParam(name = "limit", required = false, defaultValue = "10") int limit) {
    System.out.println(current);
    System.out.println(limit);
    return "students list";
}

访问http://localhost:8888/community/students2/current=1/limit=20

@RequestMapping(path = "/students2/current={current}/limit={limit}" , method = RequestMethod.GET)
    public String getStudents2(
            @PathVariable(name = "current", required = false) int current,
            @PathVariable(name = "limit", required = false) int limit) {
        System.out.println(current);
        System.out.println(limit);
        return "students2 list";
    }

测试post请求

在static下新建一个StudentInfo.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>StudentInfo</title>
</head>
<body>

    <form method="post" action="/community/studentInfo">
        <p>姓名:<input type="text" name="name"></p>
    
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值