[关闭]
@muyanfeixiang 2017-01-12T17:27:23.000000Z 字数 2562 阅读 1327

Java Web学习笔记(5)

JavaWeb Spring SpringMVC


上一小节使用了servlet,本小节使用jsp,jsp类似asp.net mvc中cshtml文件。

添加jsp依赖

首先在pom.xml文件中添加jsp模块依赖,如下。

  1. <dependency>
  2. <groupId>javax.servlet.jsp</groupId>
  3. <artifactId>javax.servlet.jsp-api</artifactId>
  4. <version>2.3.1</version>
  5. <scope>provided</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>javax.servlet.jsp.jstl</groupId>
  9. <artifactId>javax.servlet.jsp.jstl-api</artifactId>
  10. <version>1.2.1</version>
  11. <scope>compile</scope>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.glassfish.web</groupId>
  15. <artifactId>javax.servlet.jsp.jstl</artifactId>
  16. <version>1.2.2</version>
  17. <scope>compile</scope>
  18. <exclusions>
  19. <exclusion>
  20. <groupId>javax.servlet</groupId>
  21. <artifactId>servlet-api</artifactId>
  22. </exclusion>
  23. <exclusion>
  24. <groupId>javax.servlet.jsp</groupId>
  25. <artifactId>jsp-api</artifactId>
  26. </exclusion>
  27. <exclusion>
  28. <groupId>javax.servlet.jsp.jstl</groupId>
  29. <artifactId>jstl-api</artifactId>
  30. </exclusion>
  31. </exclusions>
  32. </dependency>

添加jsp文件

在web中的WEB-INF下添加jsp文件夹,添加Greet.jsp文件
image_1b68t9llp182j1ndio84ip3f199.png-62.1kB
Greet.jsp文件内容如下

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: liuyb
  4. Date: 2017/1/12
  5. Time: 16:38
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  9. <html>
  10. <head>
  11. <title>From Greet jsp</title>
  12. </head>
  13. <body>
  14. <p>Hello From Greet.jsp 文件</p>
  15. </body>
  16. </html>

当然此时jsp还不能被使用,首先需要在web.xml中添加相应的配置。如下

  1. <jsp-config>
  2. <jsp-property-group>
  3. <url-pattern>*.jsp</url-pattern>
  4. <url-pattern>*.jspf</url-pattern>
  5. <page-encoding>UTF-8</page-encoding>
  6. <scripting-invalid>false</scripting-invalid>
  7. <trim-directive-whitespaces>true</trim-directive-whitespaces>
  8. <default-content-type>text/html</default-content-type>
  9. </jsp-property-group>
  10. </jsp-config>

再次需要修改GreetServlet,来指定返回的jsp文件,修改如下

  1. package com.liuyb;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.MultipartConfig;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. /**
  10. * Created by liuyb on 2017/1/12.
  11. */
  12. @WebServlet(
  13. name = "greetServlet",
  14. urlPatterns = {"/greet"},
  15. loadOnStartup = 1
  16. )
  17. @MultipartConfig(
  18. fileSizeThreshold = 5_242_880, //5MB
  19. maxFileSize = 20_971_520L, //20MB
  20. maxRequestSize = 41_943_040L //40MB
  21. )
  22. public class GreetServlet extends HttpServlet {
  23. @Override
  24. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  25. req.getRequestDispatcher("/WEB-INF/jsp/Greet.jsp")
  26. .forward(req, resp);
  27. }
  28. @Override
  29. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  30. resp.getWriter().println("Hello Post From GreetServlet");
  31. }
  32. }

然后运行程序,在浏览器输入http://localhost:8080/greet
可以看到如下
image_1b68vsue3f03183emhk9jrgtb9.png-10.8kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注