使用 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 成功访问!