微架构 springcloud-03. springboot-使用 jsp 和 servlet

使用 jsp 和 servlet

前面已经说到,springboot 默认推荐使用thymeleaf模板引擎,那么jsp就应抛弃;既然使用SpringMVC,servlet 就应避免使用,定义Controller来书写业务逻辑。若你对springboot 不是全心全意的真爱,springboot 也提供了对Servlet API 的使用支持!如此一来就破坏了springboot 的设计初衷和最大优势,并不建议使用!

使用jsp

01 新建一个maven web 项目,添加基础依赖,无需再添加模板引擎了:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.10.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

02 添加 jsp 支持依赖:

<!--jstl-->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
</dependency>

<!-- tomcat 的支持.-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

03 修改配置文件

# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/views/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp

04 再webapp 下新建views目录,并创建index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
    <head>
        <base href="<%=basePath%>">
    </head>
    <body>
        <h2>Hello World!</h2>
        <h1>${mesg}</h1>
    </body>
</html>

05 编写Controller:

@Controller
public class JspController {

    @RequestMapping("/index")
    public String index(Map map){
        map.put("mesg", "jsp mesage");
        return "index";
    }
}

06 编写启动类,必须说明的是启动类所在的包必须是所有类的父包,springboot 的基础扫描包是启动类所在的包,按这个包往下扫描,加载SpringIOC 对象!

package person.jack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

07 main 函数启动,访问 http://localhost:8080/index:

# 页面打印
Hello World!

jsp mesage

# jsp 成功使用!

使用Servlet

就springboot 提供的各个模块而言,在实际开发中无需调用servlet API,都能完美、有效的完成我们的需求!但是我们也不能排除对servlet API的使用,如: Servlet、Filter、Listener 等等!Spirngboot 通过将以上模块注入到IOC容器以控制servlet API。

01 编写: person.jack.servlet/TestServlet.java,对应建包、建类:

@WebServlet(urlPatterns = "/testServlet",description = "servlet测试!")
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter printWriter = resp.getWriter();
        printWriter.write("springboot Servlet 测试!");
    }
}

02 浏览器访问 http://localhost:8080/testServlet ,能否正常访问:

# 页面报错404,找不资源
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Feb 06 22:56:57 CST 2018
There was an unexpected error (type=Not Found, status=404).
No message available

# 所以,单纯的定义Servlet 是无法正常访问的,需将Servlet 注册

03 Servlet 注册,通过@Bean注解将TesrServlet() 加载到IOC容器,在App.java 中添加一个@Bean 方法即可:

//@SpringBootApplication 注解是一个组合注解,它包含多个注解,其中也包含:@Configuration注解,所以此处无需再写
@SpringBootApplication
public class App {
    /**将TesrServlet 注册到IOC 容器!*/
    @Bean
    public ServletRegistrationBean testServlet(){
        return new ServletRegistrationBean(new TestServlet());
    }
    
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

04 重启服务,访问:http://localhost:8080/testServlet

#页面相应:
springboot Servlet 测试!

#Servlet 成功访问!

再次说明,无论是jsp 还是Servlet,Springboot 只是提供了这样的支持,并不推荐使用!</h1>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,115评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,993评论 6 342
  • 0 系列目录# WEB请求处理 WEB请求处理一:浏览器请求发起处理 WEB请求处理二:Nginx请求反向代理 本...
    七寸知架构阅读 14,079评论 22 190
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,394评论 11 349
  • 社会在进步,在城市高速发展的背景下,乡村和城市的差距越拉越大,城市是工业文明的产物,乡村不可能去丢掉农业去大展工业...
    冯保中阅读 207评论 0 0