pringBoot
springboot的目的是為了簡(jiǎn)化spring應(yīng)用的開發(fā)搭建以及開發(fā)過程。內(nèi)部使用了特殊的處理,使得開發(fā)人員不需要進(jìn)行額外繁鎖的xml文件配置的編寫,其內(nèi)部包含很多模塊的配置只需要添加maven依賴即可使用,這項(xiàng)功能可謂對(duì)開發(fā)人員提供了大大的好處。使用springboot只需要簡(jiǎn)單配置一下就可以完成之前復(fù)雜的配置過程。可以到https://start.spring.io/此網(wǎng)站上,下載一個(gè)最簡(jiǎn)單的springboot應(yīng)用,然后一步一步實(shí)現(xiàn)自已的應(yīng)用。
可以看出當(dāng)前的穩(wěn)定版本為2.1.0,點(diǎn)擊Generate Project 按鈕,即可下載一個(gè)可用的springboot應(yīng)用。
這個(gè)是我下載下來后,雙擊后出來的??梢钥闯鲆怨こ淌且粋€(gè)基于maven的項(xiàng)目。你可以將其解壓到任何一個(gè)目錄下,通過eclipse或其他IDE進(jìn)行導(dǎo)入后運(yùn)行,eclipse導(dǎo)入流程為File->import->maven->existing maven projects,查找到自己的項(xiàng)目目錄。也可以基于此工程來建立自已的maven項(xiàng)目。
下面以建立自己的maven項(xiàng)目
建立自己的springboot項(xiàng)目
在建立項(xiàng)目時(shí),可以創(chuàng)建一個(gè)多模塊聚合項(xiàng)目,即在創(chuàng)建項(xiàng)目時(shí)選中
選擇為pom。
創(chuàng)建后的工程結(jié)構(gòu)為
<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>
將此段代碼復(fù)制到 spring-boot-study工程中的pom文件中
將下面的依賴復(fù)制到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自動(dòng)完成項(xiàng)目工程的配置。完成后項(xiàng)目中所有需要依賴的jar包自動(dòng)配置完成。
@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就已經(jīng)完成后??梢栽跒g覽器中輸入localhost:8080/hello即可看到效果
springboot默認(rèn)啟動(dòng)后的端口為8080,但可以在application.properties文件中進(jìn)行修改。
server.port=9001
將端口修改為9001,重新啟動(dòng)項(xiàng)目后,在瀏覽器中輸入入localhost:9001/hello同樣可以看到相同的結(jié)果。
<!-- 加入thymeleaf的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
但在Spring Boot項(xiàng)目中,一般src/main/resources/static目錄用于存放各類靜態(tài)資源文件,例如css、js和image等。src/main/resources/templates用于存放頁面文件,例如html,jsp等。所以在spring-boot-web中的resources目錄下創(chuàng)建static目錄與templates目錄,并將相應(yīng)的資源文件放置在各自的目錄下。
配置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"; } }
重新啟動(dòng)程序,訪問localhost:9001/就可成功跳轉(zhuǎn)至login.html登陸界面上。
注:thymeleaf對(duì)html標(biāo)簽要求很嚴(yán)格,每一個(gè)標(biāo)簽都需要成對(duì)出現(xiàn)。
調(diào)試過程中遇到下面異常信息
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-。。。。。。。。。。。
因?yàn)殄e(cuò)將templates寫成templatse導(dǎo)致。
至此實(shí)現(xiàn)從后端服務(wù)訪問到登陸界面的搭建,還沒有具體登陸邏輯實(shí)現(xiàn)。
ervlet 可以與 HTML form 標(biāo)簽一起使用,來允許用戶上傳文件到服務(wù)器。上傳的文件可以是文本文件或圖像文件或任何文檔。
創(chuàng)建一個(gè)文件上傳表單
下面的 HTML 代碼創(chuàng)建了一個(gè)文件上傳表單。以下幾點(diǎn)需要注意:
<html> <head> <title>文件上傳表單</title> </head> <body> <h3>文件上傳:</h3> 請(qǐng)選擇要上傳的文件:<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>
這將顯示下面的結(jié)果,允許用戶從本地計(jì)算機(jī)選擇一個(gè)文件,當(dāng)用戶點(diǎn)擊"上傳文件"時(shí),表單會(huì)連同從本地計(jì)算機(jī)選擇的文件一起提交:
<b>文件上傳:</b> 請(qǐng)選擇要上傳的文件:<br /> <input type="file" name="file" size="50" /> <br /> <input type="button" value="上傳文件" /> <br /> 注:這只是虛擬的表單,不會(huì)正常工作。
編寫后臺(tái) Servlet
以下是 Servlet UploadServlet,會(huì)接受上傳的文件,并把它儲(chǔ)存在目錄 <Tomcat-installation-directory>/webapps/data 中。這個(gè)目錄名也可以使用外部配置來添加,比如 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 的源代碼,可以一次處理多個(gè)文件的上傳。在繼續(xù)操作之前,請(qǐng)確認(rèn)下列各項(xiàng):
// 導(dǎo)入必需的 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( ){ // 獲取文件將被存儲(chǔ)的位置 filePath = getServletContext().getInitParameter("file-upload"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { // 檢查我們有一個(gè)文件上傳請(qǐng)求 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(); // 文件大小的最大值將被存儲(chǔ)在內(nèi)存中 factory.setSizeThreshold(maxMemSize); // Location to save data that is larger than maxMemSize. factory.setRepository(new File("c:\\temp")); // 創(chuàng)建一個(gè)新的文件上傳處理程序 ServletFileUpload upload = new ServletFileUpload(factory); // 允許上傳的文件大小的最大值 upload.setSizeMax( maxFileSize ); try{ // 解析請(qǐng)求,獲取文件項(xiàng) List fileItems = upload.parseRequest(request); // 處理上傳的文件項(xiàng) 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 () ) { // 獲取上傳文件的參數(shù) 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."); } }
編譯和運(yùn)行 Servlet
編譯上面的 Servlet UploadServlet,并在 web.xml 文件中創(chuàng)建所需的條目,如下所示:
<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>
現(xiàn)在嘗試使用您在上面創(chuàng)建的 HTML 表單來上傳文件。當(dāng)您在瀏覽器中訪問:http://localhost:8080/UploadFile.htm 時(shí),它會(huì)顯示下面的結(jié)果,這將有助于您從本地計(jì)算機(jī)上傳任何文件。
<b>文件上傳:</b> 請(qǐng)選擇要上傳的文件:<br /> <input type="file" name="file" size="50" /> <br /> <input type="button" value="上傳文件" />
如果您的 Servelt 腳本能正常工作,那么您的文件會(huì)被上傳到 c:\apache-tomcat-5.5.29\webapps\data\ 目錄中。
JSP全名為Java Server Pages,java服務(wù)器頁面。JSP是一種基于文本的程序,其特點(diǎn)就是HTML和Java代碼共同存在!
JSP是為了簡(jiǎn)化Servlet的工作出現(xiàn)的替代品,Servlet輸出HTML非常困難,JSP就是替代Servlet輸出HTML的。
String s = "HelloWorda"; out.println(s);
JSP也是Servlet,運(yùn)行時(shí)只有一個(gè)實(shí)例,JSP初始化和銷毀時(shí)也會(huì)調(diào)用Servlet的init()和destroy()方法。另外,JSP還有自己初始化和銷毀的方法
JSP代碼可以分為兩部分:
JSP腳本
<jsp:scriptlet> String s = "HelloWorld"; out.println(s); </jsp:scriptlet>
JSP注釋
<%--這是JSP注釋--%> <%--%> //這是java的當(dāng)行注釋 // /*這是java的多行注釋*/ /**/
JSP指令
JSP指令用來聲明JSP頁面的相關(guān)屬性,例如編碼方式、文檔類型等等
JSP指令的語法:
<%@指令 屬性名="值" %>
page指令
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page contentType="application/msword;charset=UTF-8" language="java" %> <html> <head> <title>簡(jiǎn)單使用JSP</title> </head> <body> 1111 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %> <html> <head> <title>該頁面出錯(cuò)了!</title> </head> <body> <%--模擬頁面出錯(cuò)了?。?!--%> <% int result = 2 / 0; %> 你好呀 </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> <html> <head> <title>友好提示頁面</title> </head> <body> 服務(wù)器正忙著呢! </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>包含頁頭和頁尾進(jìn)來</title> </head> <body> <%@include file="head.jsp" %> <%@include file="foot.jsp" %> </body> </html>
taglib指令
JSP行為
JSP行為(JSP Actions)是一組JSP內(nèi)置的標(biāo)簽,只書寫少量的標(biāo)記代碼就能夠使用JSP提供豐富的功能,JSP行為是對(duì)常用的JSP功能的抽象和封裝。
為什么我不把它直接稱為JSP標(biāo)簽?zāi)兀?strong>我把這些JSP內(nèi)置的標(biāo)簽稱之為JSP行為,能夠和JSTL標(biāo)簽區(qū)分開來。當(dāng)然了,你也可以把它稱之為JSP標(biāo)簽,你不要搞混就行了。我個(gè)人喜歡把這些JSP內(nèi)置標(biāo)簽稱之為JSP行為。
include行為
<jsp:include page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>包含頁頭和頁尾進(jìn)來</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就跳轉(zhuǎn)到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"); %> 獲取到的參數(shù)是: <%=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
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。