SpringBoot学习之旅(一)---基础项目搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lupengfei1009/article/details/86543442

源码地址

点击下载

项目创建





运行



运行正常;超级爽!

测试请求响应

  • 创建测试类TestController

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TestController {
    
        @RequestMapping("/hello")
        public String hello(String a) {
            return "hello sb!!!==>" + a;
        }
    }
    

    重启项目!!!
    请求测试:http://127.0.0.1:8080/hello?a=1

  • 响应jsp测试

    • 添加web jsp目录



      创建以下目录
  • 配置pom.xml添加jsp的支持

            <!-- servlet依赖. -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
            </dependency>
    
            <!-- tomcat的支持.-->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
    
  • 创建测试jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>hello jsp</title>
    </head>
    <body>
    hello ${name} 我是jsp!!!
    </body>
    </html>
    
  • 配置application.properties

    spring.mvc.view.prefix=/WEB-INF/jsp/
    spring.mvc.view.suffix=.jsp
    

  • 创建TestJspController,jsp的前置入口

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class TestJspController {
    
        @RequestMapping("/hellojsp")
        public String hellojsp(String name,Model model) {
            //这里添加jsp中要获取的数据
            model.addAttribute("name",name);
            //返回jsp页面的名称
            return "hello";
        }
    }
    
  • 测试
    http://127.0.0.1:8080/hellojsp?name=张三

猜你喜欢

转载自blog.csdn.net/lupengfei1009/article/details/86543442