Download presentation
Presentation is loading. Please wait.
1
Lec 06 David Presentation on Advanced Spring
Build Sports Van application in class File I/O in Java $git fetch gerber updates $git fetch gerber updates_ws $git checkout -b lec06 gerber/updates $git checkout -b lec06_ws gerber/updates_ws
2
Themyleaf
3
Introducing Thymeleaf
It's a Java Template Engine Can be used as view layer in Spring MVC First stable release: July 2011 Elegant, configurable, extensible 21st-century feature set
4
How does it look like?
5
Standard and SpringStandard Dialects
XML, XHTML 1.0, XHTML 1.1 and HTML5 support. Complete substitute for view-layer web technologies such as JSP. Easy-to-use, elegant syntax based on attributes only (these dialects include no tags). Great for web templates: <input type="text" th:field="*{name}" /> instead of <mylib:text field="name" /> Natural templating for easy prototyping: display your templates statically in a browser (without running your app server). Expression evaluation: OGNL (Standard) and Spring Expression Language (SpringStandard). Complete integration with Spring MVC (SpringStandard): form binding, property editors, internationalization, etc. Spring WebFlow support (including AJAX events). Fully-featured template logic: iteration, conditional evaluation, context variable declaration, etc. Several options for layout (Composite View pattern): A builtin mechanism (Thymeleaf's native fragment inclusion). Specific Apache Tiles integration (through extras package). Can be used along with SiteMesh. Internationalization support: easy inclusion of externalized messages into templates. URL rewriting capabilities for adding context and session information to URLs. JavaScript and Dart inlining: intelligent evaluation of expressions inside javascript/dart code. Template validation support for XML, XHTML 1.0 and XHTML 1.1.
6
Can Jsp do it?
7
Can JSP+JSTL Do it?
8
Can Velocity do it?
9
Can FreeMarker Do it?
10
...Can Thymeleaf do it?
11
How do we evaluate it? “valid document, don't break structure”
Templates should be statically displayable Static = Open in browser, no web server Templates should work as prototypes
12
Now Let’s write templates!
13
Texts th:text HTML-escaped text (default) th:utext unescaped text
14
Formatting #dates, #calendars, #numbers, #strings...
15
URLs @{...} syntax Automatic URL-rewriting is performed
16
Iteration th:each
17
Iteration status th:each
18
Conditionals th:if th:unless th:swithc/th:case
19
Forms and bean-binding
th:object, th:field
20
Page compostion Declare fragment with th:fragment Resuse th:include
21
Who is using Thymeleaf?
22
Vans with Thymeleaf >thymeleaf example (see router at ~/posts/)
Examine POM for starters added -web and thyme Web, thyme, jpa, hal, h2 Other possible apps: make/model : movie/actor : restaurant/dish : country/attraction
23
Cars with Thymeleaf >thymeleaf example (see router at ~/posts/)
thymeleaf.org/ >thymeleaf example (see router at ~/posts/) Examine POM for starters added -web and thyme Web, thyme, jpa, hal, h2 Other possible apps: make/model : movie/actor : restaurant/dish : country/attraction
24
I/O (input and output)
25
File I/O Read Memory (Primary Memory) Hard Disk (Secondary Memory)
Process Write
26
Java - Streams JAVA provides 2 types of streams
Text streams - containing ‘characters‘ Program I ‘ M A S T R I N G \n Device Binary Streams - containing 8 – bit information Program Device
29
FilterInputStream is superclass
29
30
You will read in bytes but store them in int
-128 ~ +127 -1 implies end of stream 30
31
See ByteStreamDriver ByteStream
32
Stream with Unicode int b1 = in.read(); int b2 = in.read();
char c = (char) (b1*256 + b2); Difficult to handle That is why Java introduced Reader/Writer in Java 1.1 32
33
Character Stream The Java platform stores character values using Unicode conventions Character stream I/O automatically translates this internal format to and from the local character set. Character streams are often wrappers for byte streams. The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes. See CharStreamDriver 33
34
What is a Buffer You can use buffered I/O streams for an overhead reduction (overhead generated by each such request often triggers disk access, network activity, or some other operation that is relatively expensive). Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. See BuffStreamDriver 34
35
Streams in I/O Java8 stream is like a collection, only arrayed in time. Java I/O stream is linked to a physical device and either consumes or produces a stream of data.
36
What is an i/o Stream Java programs perform I/O through streams. A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner, even if the actual physical devices to which they are linked differ. Thus, the same I/O classes and methods can be applied to any type of device. This means that an input stream can abstract many different kinds of input: from a disk file, a keyboard, or a network socket. Likewise, an output stream may refer to the console, a disk file, or a network connection. Streams are a clean way to deal with input/output without having every part of your code understand the difference between a keyboard and a network, for example. Java implements streams within class hierarchies defined in the java.io package. 36
37
See StreamExample and others in package.
Stream Concept Input Stream Output Stream See StreamExample and others in package. 37
38
java.io.InputStream/OutputStream
BufferedInputStream ByteArrayInputStream DataInputStream FileInputStream FilterInputStream ObjectInputStream PipedInputStream PushbackInputStream SequenceInputStream StringBufferedInputStr eam LineNumberInputStrea m BufferedOutputStream ByteArrayOutputStream DataOutputStream FileOutputStream FilterOutputStream ObjectOutputStream PipedOutputStream PrintStream 38
39
Examine Object Hierarchy in IDE
File Operations FileInputStream FileOutputStream Examine Object Hierarchy in IDE 39
40
What is a Pipe A pipe consists of a pair of channels: A writable sink channel and a readable source channel. Once some bytes are written to the sink channel they can be read from source channel in exactly the order in which they were written. Whether or not a thread writing bytes to a pipe will block until another thread reads those bytes, or some previously-written bytes, from the pipe is system-dependent and therefore unspecified. Many pipe implementations will buffer up to a certain number of bytes between the sink and source channels, but such buffering should not be assumed. See PipeExample 40
41
IOException IOException is everywhere!
Note that it is a checked exception We need to try/catch it Why do we need to deal with IO Exception? 41
42
IOException java.io.InputStream
public abstract int read() throws IOException java.io.OutputStream public abstract void write(int b) throws IOException 42
43
See StreamRedirection
System.setOut System.setOut(new PrintStream(new FileOutputStream(“output.txt”))); System.setIn System.setIn(new FileInputStream(“input.txt”)); See StreamRedirection 43
44
Reader/Writer BufferedReader CharArrayReader FileReader FilterReader
InputStreamReader PipedReader StringReader PushbackReader LinedNumberReader BufferedWriter CharArrayWriter FileWriter FilterWriter OutputStreamWriter PipedWriter StringWriter PrintWriter 44
45
Line-Oriented Reader/Writer
public class CopyLines { public static void main(String[] args) throws IOException { BufferedReader inputReader = null; PrintWriter outputWriter = null; inputReader = new BufferedReader(new FileReader(“input.txt")); outputWriter = new PrintWriter(new FileWriter("output.txt")); String line; while ((line = inputReader.readLine()) != null) { outputWriter.println(line); } inputReader.close(); outputWriter.close(); 45
46
Reading Files public static void readFile() {
import java.io.*; public static void readFile() { BufferedReader ins = new BufferedReader(new FileReader(“test.txt”)); while(ins.ready()) String s = ins.readLine(); System.out.println(s); } ins.close(); Important: Place the code inside Try{…} Catch{….} Wherever necessary
47
Writing File public static void writeFile() {
import java.io.*; public static void writeFile() { String s = “I love programming”; BufferedWriter outs = new BufferedWriter(new FileWriter(“test_out.txt”)); outs.write(s); } outs.close(); Important: Place the code inside Try{…} Catch{….} Wherever necessary
48
Reading tokens from file
import java.io.*; Import java.util.Scanner; Public static void readFile() { BufferedReader ins = new BufferedReader(new FileReader(“test.txt”)); Scanner scanner = new Scanner(ins); while(scanner.hasNext()) System.out.println(scanner.next()); } scanner.close(); Important: Place the code inside Try{…} Catch{….} Wherever necessary
49
Reading and Writing Files
Create a stream object and associate it with a disk-file Give the stream object the desired functionality while there is more information read(write) next data from(to) the stream close the stream
50
Browse Files using GUI private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser fc = new JFileChooser(); if(fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) File file = fc.getSelectedFile(); String inputFilePath = file.getAbsolutePath().toString(); jTextField1.setText(inputFilePath); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.