pringBoot
springboot的目的是为了简化spring应用的开发搭建以及开发过程。内部使用了特殊的处理,使得开发人员不需要进行额外繁锁的xml文件配置的编写,其内部包含很多模块的配置只需要添加maven依赖即可使用,这项功能可谓对开发人员提供了大大的好处。使用springboot只需要简单配置一下就可以完成之前复杂的配置过程。可以到https://start.spring.io/此网站上,下载一个最简单的springboot应用,然后一步一步实现自已的应用。
可以看出当前的稳定版本为2.1.0,点击Generate Project 按钮,即可下载一个可用的springboot应用。
这个是我下载下来后,双击后出来的。可以看出以工程是一个基于maven的项目。你可以将其解压到任何一个目录下,通过eclipse或其他IDE进行导入后运行,eclipse导入流程为File->import->maven->existing maven projects,查找到自己的项目目录。也可以基于此工程来建立自已的maven项目。
下面以建立自己的maven项目
建立自己的springboot项目
在建立项目时,可以创建一个多模块聚合项目,即在创建项目时选中
选择为pom。
创建后的工程结构为
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties>
将此段代码复制到 spring-boot-study工程中的pom文件中
将下面的依赖复制到spring-boot-web工程中的pom文件中
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
eclipse自动完成项目工程的配置。完成后项目中所有需要依赖的jar包自动配置完成。
@SpringBootApplication @RestController public class WebApplication { @RequestMapping("/hello") public String helloWorld() { return "Hello World"; } public static void main(String[] args) { SpringApplication.run(WebApplication.class, args); } }
HelloWold就已经完成后。可以在浏览器中输入localhost:8080/hello即可看到效果
springboot默认启动后的端口为8080,但可以在application.properties文件中进行修改。
server.port=9001
将端口修改为9001,重新启动项目后,在浏览器中输入入localhost:9001/hello同样可以看到相同的结果。
<!-- 加入thymeleaf的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
但在Spring Boot项目中,一般src/main/resources/static目录用于存放各类静态资源文件,例如css、js和image等。src/main/resources/templates用于存放页面文件,例如html,jsp等。所以在spring-boot-web中的resources目录下创建static目录与templates目录,并将相应的资源文件放置在各自的目录下。
配置thymeleaf
#thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.cache=false
html文件修改,增加xmlns:th="http://www.thymeleaf.org" 属性,资源文件的引入要修改。
<link href="../static/css/style.css" th:href="@{/css/style.css}" rel="stylesheet" /> <link href="../static/css/login.css" th:href="@{/css/login.css}" rel="stylesheet" />
然后编写 java代码
@Controller public class IndexController { @RequestMapping("/") public String index() { return "login"; } }
重新启动程序,访问localhost:9001/就可成功跳转至login.html登陆界面上。
注:thymeleaf对html标签要求很严格,每一个标签都需要成对出现。
调试过程中遇到下面异常信息
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) [thymeleaf-spring5-。。。。。。。。。。。
因为错将templates写成templatse导致。
至此实现从后端服务访问到登陆界面的搭建,还没有具体登陆逻辑实现。
ervlet 可以与 HTML form 标签一起使用,来允许用户上传文件到服务器。上传的文件可以是文本文件或图像文件或任何文档。
创建一个文件上传表单
下面的 HTML 代码创建了一个文件上传表单。以下几点需要注意:
<html> <head> <title>文件上传表单</title> </head> <body> <h3>文件上传:</h3> 请选择要上传的文件:<br /> <form action="UploadServlet" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="50" /> <br /> <input type="submit" value="上传文件" /> </form> </body> </html>
这将显示下面的结果,允许用户从本地计算机选择一个文件,当用户点击"上传文件"时,表单会连同从本地计算机选择的文件一起提交:
<b>文件上传:</b> 请选择要上传的文件:<br /> <input type="file" name="file" size="50" /> <br /> <input type="button" value="上传文件" /> <br /> 注:这只是虚拟的表单,不会正常工作。
编写后台 Servlet
以下是 Servlet UploadServlet,会接受上传的文件,并把它储存在目录 <Tomcat-installation-directory>/webapps/data 中。这个目录名也可以使用外部配置来添加,比如 web.xml 中的 context-param 元素,如下所示:
<web-app> .... <context-param> <description>Location to store uploaded file</description> <param-name>file-upload</param-name> <param-value> c:\apache-tomcat-5.5.29\webapps\data\ </param-value> </context-param> .... </web-app>
以下是 UploadServlet 的源代码,可以一次处理多个文件的上传。在继续操作之前,请确认下列各项:
// 导入必需的 java 库 import java.io.*; import java.util.*; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.io.output.*; public class UploadServlet extends HttpServlet { private boolean isMultipart; private String filePath; private int maxFileSize = 50 * 1024; private int maxMemSize = 4 * 1024; private File file ; public void init( ){ // 获取文件将被存储的位置 filePath = getServletContext().getInitParameter("file-upload"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // 检查我们有一个文件上传请求 isMultipart = ServletFileUpload.isMultipartContent(request); response.setContentType("text/html"); java.io.PrintWriter out = response.getWriter( ); if( !isMultipart ){ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); return; } DiskFileItemFactory factory = new DiskFileItemFactory(); // 文件大小的最大值将被存储在内存中 factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File("c:\\temp")); // 创建一个新的文件上传处理程序 ServletFileUpload upload = new ServletFileUpload(factory); // 允许上传的文件大小的最大值 upload.setSizeMax( maxFileSize ); try{ // 解析请求,获取文件项 List fileItems = upload.parseRequest(request); // 处理上传的文件项 Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) { // 获取上传文件的参数 String fieldName = fi.getFieldName(); String fileName = fi.getName(); String contentType = fi.getContentType(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // 写入文件 if( fileName.lastIndexOf("\\") >= 0 ){ file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ; }else{ file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ; } fi.write( file ) ; out.println("Uploaded Filename: " + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); }catch(Exception ex) { System.out.println(ex); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { throw new ServletException("GET method used with " + getClass( ).getName( )+": POST method required."); } }
编译和运行 Servlet
编译上面的 Servlet UploadServlet,并在 web.xml 文件中创建所需的条目,如下所示:
<servlet> <servlet-name>UploadServlet</servlet-name> <servlet-class>UploadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UploadServlet</servlet-name> <url-pattern>/UploadServlet</url-pattern> </servlet-mapping>
现在尝试使用您在上面创建的 HTML 表单来上传文件。当您在浏览器中访问:http://localhost:8080/UploadFile.htm 时,它会显示下面的结果,这将有助于您从本地计算机上传任何文件。
<b>文件上传:</b> 请选择要上传的文件:<br /> <input type="file" name="file" size="50" /> <br /> <input type="button" value="上传文件" />
如果您的 Servelt 脚本能正常工作,那么您的文件会被上传到 c:\apache-tomcat-5.5.29\webapps\data\ 目录中。
JSP全名为Java Server Pages,java服务器页面。JSP是一种基于文本的程序,其特点就是HTML和Java代码共同存在!
JSP是为了简化Servlet的工作出现的替代品,Servlet输出HTML非常困难,JSP就是替代Servlet输出HTML的。
String s = "HelloWorda"; out.println(s);
JSP也是Servlet,运行时只有一个实例,JSP初始化和销毁时也会调用Servlet的init()和destroy()方法。另外,JSP还有自己初始化和销毁的方法
JSP代码可以分为两部分:
JSP脚本
<jsp:scriptlet> String s = "HelloWorld"; out.println(s); </jsp:scriptlet>
JSP注释
<%--这是JSP注释--%> <%--%> //这是java的当行注释 // /*这是java的多行注释*/ /**/
JSP指令
JSP指令用来声明JSP页面的相关属性,例如编码方式、文档类型等等
JSP指令的语法:
<%@指令 属性名="值" %>
page指令
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page contentType="application/msword;charset=UTF-8" language="java" %> <html> <head> <title>简单使用JSP</title> </head> <body> 1111 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %> <html> <head> <title>该页面出错了!</title> </head> <body> <%--模拟页面出错了!!!--%> <% int result = 2 / 0; %> 你好呀 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> <html> <head> <title>友好提示页面</title> </head> <body> 服务器正忙着呢! </body> </html>
<error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <error-page> <exception-type>java.lang.NullPointerException</exception-type> <location>/error.jsp</location> </error-page>
include指令
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页头</title> </head> <body> 我是页头 <br> <br> <br> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页尾</title> </head> <body> 我是页尾 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>包含页头和页尾进来</title> </head> <body> <%@include file="head.jsp" %> <%@include file="foot.jsp" %> </body> </html>
taglib指令
JSP行为
JSP行为(JSP Actions)是一组JSP内置的标签,只书写少量的标记代码就能够使用JSP提供丰富的功能,JSP行为是对常用的JSP功能的抽象和封装。
为什么我不把它直接称为JSP标签呢?我把这些JSP内置的标签称之为JSP行为,能够和JSTL标签区分开来。当然了,你也可以把它称之为JSP标签,你不要搞混就行了。我个人喜欢把这些JSP内置标签称之为JSP行为。
include行为
<jsp:include page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>包含页头和页尾进来</title> </head> <body> <jsp:include page="head.jsp"/> <jsp:include page="foot.jsp"/> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页头</title> </head> <body> <% String s = "zhongfucheng"; %> 我是页头呀 <br> <br> <br> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>页尾</title> </head> <body> <% String s = "zhongfucheng"; %> 我是页尾呀 </body> </html>
param行为
forward行为
<jsp:forward page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>访问1.jsp就跳转到head.jsp</title> </head> <body> <jsp:forward page="head.jsp"/> </body> </html>
<jsp:forward page="head.jsp"> <jsp:param name="username" value="zhongfucheng"/> </jsp:forward>
<% String ss = request.getParameter("username"); %> 获取到的参数是: <%=ss%>
directive行为
<jsp:directive.include file="head.jsp"></jsp:directive.include> <jsp:directive.include file="foot.jsp"></jsp:directive.include>
javaBean行为
<jsp:useBean id=""/> <jsp:setProperty name="" property=""/> <jsp:getProperty name="" property=""/>
文章来源:https://dwz.cn/OtXvyvh3
作者:Java3y
*请认真填写需求信息,我们会在24小时内与您取得联系。