端口号
TCP通信的步骤:
服务器端先启动,服务器端不会主动请求客户端,必须客户端请求服务器端,客户端和服务器端就会建立一个逻辑连接,而这个连接中会包含一个IO对象,客户端和服务器端就可以使用这个对象进行通信,通信的数据不仅仅是字符,因此这个IO对象是字节流对象。
TCP通信的客户端:向服务器发送连接请求,给服务器发送数据,读取服务器回写的数据
public class TcpSocket {
public static void main(String[] args) throws IOException {
//创建客户端对象
Socket socket = new Socket("127.0.0.1", 8888);
//获取网络流对象
OutputStream os = socket.getOutputStream();
//向服务器端发送数据
os.write("你好服务器".getBytes());
//获取网络流对象,读取服务器回写的数据
InputStream is = socket.getInputStream();
//处理
byte[] bytes=new byte[1024];
int len = is.read(bytes);
System.out.println(new String(bytes,0,len));
//释放资源
socket.close();
}
}
TCP通信的服务器端:接收客户端的请求,读取客户端发送的信息,给客户端回写信息
ServerSocket:
public class TcpServer {
public static void main(String[] args) throws IOException {
//创建ServerSocket对象
ServerSocket server = new ServerSocket(8888);
//获取客户端对象
Socket socket = server.accept();
//获取客户端对象的网络字节输入流
InputStream is = socket.getInputStream();
byte[] bytes=new byte[1024];
int len = is.read(bytes);
System.out.println(new String(bytes,0,len));
//获取客户端对象的网络字节输出流
OutputStream os = socket.getOutputStream();
//回写数据
os.write("我已经受到你的数据了".getBytes());
//释放资源
socket.close();
server.close();
}
}
public class TcpSocket {
public static void main(String[] args) throws IOException {
//创建本地字节输入流读取本地文件
FileInputStream fis = new FileInputStream("D:\\IDEA_Project\\Experiment\\experiment\\src\\test\\a.jpg");
//创建客户端Socket对象,获取网络字节输出流
Socket socket = new Socket("127.0.0.1", 8888);
OutputStream os = socket.getOutputStream();
//读取本地的文件,发送给服务器
byte[] bytes=new byte[1024];
int len=0;
while ((len=fis.read(bytes))!=-1){
os.write(bytes,0,len);
}
//创建网络字节输入流,读取服务器发来的信息
InputStream is = socket.getInputStream();
while ((len=is.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
//释放资源
fis.close();
socket.close();
}
}
public class TcpServer {
public static void main(String[] args) throws IOException {
//创建ServerSocket对象
ServerSocket server = new ServerSocket(8888);
//创建本地字节输出流
FileOutputStream fos = new FileOutputStream("C:\\Users\\Julia\\Desktop\\b.jpg");
//获取客户端对象
Socket socket = server.accept();
//通过socket对象获取网络字节输入流读取客户端发来的信息
InputStream is = socket.getInputStream();
int len=0;
byte[] bytes=new byte[1024];
while ((len=is.read(bytes))!=-1){
//将读取到客户端发来的信息写入到服务器端的硬盘上
fos.write(bytes,0,len);
}
//创建网络字节输出流,向客户端回写数据
OutputStream os = socket.getOutputStream();
os.write("上传成功".getBytes());
//释放资源
fos.close();
socket.close();
server.close();
}
}
文件上传案例阻塞问题:
客户端中第一个 while ((len=fis.read(bytes))!=-1),读取本地文件,结束标记是读取到-1结束,但是while循环不会读取到-1,因此就不会把结束标记传给服务器,在服务器的while ((len=is.read(bytes))!=-1)中,永远读取不到结束标记,因此进入阻塞状态,一直循环等待结束标记,后面的回写代码就不会被执行。同样的,在客户端的第二个read中,读取不到服务器的回写数据,进入到阻塞状态。
解决:上传完文件,给服务器写一个结束标志
void shutdownOutput()
socket.shutdownOutput()
文件上传案例优化:
优化文件的命名规则、不让服务器上传完就停止(让服务器一直处于监听状态)、使用多线程来提高效率(有一个客户端要上传,就开启一个多线程)
public class TcpServer {
public static void main(String[] args) throws IOException {
//创建ServerSocket对象
ServerSocket server = new ServerSocket(8888);
//创建本地字节输出流
File file=new File("C:\\Users\\Julia\\Desktop");
if(!file.exists()){
//如果文件不存在就创建
file.mkdirs();
}
String filename=System.currentTimeMillis()+new Random().nextInt(9999)+".jpg";
FileOutputStream fos = new FileOutputStream(file+"\\"+filename);
//让服务器一直处于监听状态,有一个客户端都不关闭
while (true){
//获取客户端对象
Socket socket = server.accept();
//每次有一个客户端要上传文件,就开启一个线程
new Thread(new Runnable() {
@Override
public void run() {
try {
//通过socket对象获取网络字节输入流读取客户端发来的信息
InputStream is = socket.getInputStream();
int len=0;
byte[] bytes=new byte[1024];
while ((len=is.read(bytes))!=-1){
//将读取到客户端发来的信息写入到服务器端的硬盘上
fos.write(bytes,0,len);
}
//创建网络字节输出流,向客户端回写数据
OutputStream os = socket.getOutputStream();
os.write("上传成功".getBytes());
//释放资源
fos.close();
socket.close();
}catch (IOException e){
System.out.println(e);
}
}
}).start();
}
//关闭服务器
//server.close();
}
}
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(8080);
//浏览器解析服务器回写的html页面,页面中如果有图片,那么浏览器就会单独开启一个线程,读取图片
//需要让服务器一直处于监听状态,以应对客户端的每一次请求
while (true){
//获取客户端对象
Socket socket = server.accept();
//为了提高效率,每有一个请求,就开启一个线程来处理
new Thread(new Runnable() {
@Override
public void run() {
try {
//获取网络字节输入流
InputStream is = socket.getInputStream();
//把is网络字节输入流转换为字符缓冲输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//读取请求信息的第一行
String line = br.readLine(); //GET /test/web/index.html HTTP/1.1
//获取/test/web/index.html
String[] arr = line.split(" ");
//获取test/web/index.html
String htmlPath = arr[1].substring(1);
//创建一个本地字节输入流,读取要访问的html
FileInputStream fis = new FileInputStream(htmlPath);
//使用网络字节输出流写到网页上
OutputStream os = socket.getOutputStream();
//写入HTTP响应头,固定写法
os.write("HTTP/1.1 200 OK\r\n".getBytes());
os.write("Content-Type:text/html\r\n".getBytes());
//必须要写入空行,否则浏览器不解析
os.write("\r\n".getBytes());
byte[] bytes=new byte[1024];
int len=0;
while ((len=fis.read(bytes))!=-1){
os.write(bytes,0,len);
}
//释放资源
fis.close();
socket.close();
}catch (IOException e){
System.out.println(e);
}
}
}).start();
}
//关闭浏览器
//server.close();
}
}
概念:Java DataBase Connectivity:Java 数据库连接。Java语言操作数据库
本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库厂商去实现这套接口,提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。
步骤:
1、导入驱动jar包 mysql-connector-java-5.1.37-bin.jar
新建包libs(一般和src放在同一级目录下) -》将mysql-connector-java-5.1.37-bin.jar复制到libs目录下 -》右键点击 Add As Library(将jar包加入到工作项目中)
public class JdbcDemo1 {
public static void main(String[] args) throws Exception {
//1.导入驱动jar包
//2.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db4", "root", "root");
//定义sql语句
String sql="update account set balance=500 where id=1";
//获取执行sql的对象
Statement stmt = conn.createStatement();
//执行sql
int count = stmt.executeUpdate(sql);
//处理结果
System.out.println(count);
//释放资源
stmt.close();
conn.close();
}
}
JDBC的五个对象:
DriverManager:驱动管理对象
Connection:数据库连接对象
Statement:执行静态sql的对象
ResultSet:结果集对象,封装查询结果
PreparedStatement:执行动态sql的对象
insert语句:
优化:无论是否成功,都要释放资源,因此要对异常进行try…catch处理:
public class JdbcDemo1 {
public static void main(String[] args){
Connection conn =null;
Statement stmt =null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db4", "root", "root");
String sql="insert into account values(null,'王五',3000)";
stmt=conn.createStatement();
int count = stmt.executeUpdate(sql);
System.out.println(count);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//要避免空指针异常
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
DDL语句:
public class JdbcDemo1 {
public static void main(String[] args){
Connection conn =null;
Statement stmt =null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql:///db4", "root", "root");
String sql="create table student (id int,name varchar(20))";
stmt=conn.createStatement();
int count = stmt.executeUpdate(sql);
System.out.println(count);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
ResultSet:
public class JdbcDemo1 {
public static void main(String[] args){
Connection conn =null;
Statement stmt =null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql:///db4", "root", "root");
String sql="select * from account";
stmt=conn.createStatement();
rs= stmt.executeQuery(sql);
while (rs.next()){
int id = rs.getInt(1);
String name = rs.getString("name");
double balance = rs.getDouble(3);
System.out.println(id+" "+name+" "+balance);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
注意:这里的jdbc.properties是配置文件,需要放在src目录下:
/**
* JDBC工具类
*/
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
static {
//读取资源文件获取值
try {
//创建Properties集合类
Properties pro = new Properties();
//获取src路径下文件的方式
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
//加载文件
pro.load(new FileReader(path));
//获取数据赋值
url = pro.getProperty("url");
user=pro.getProperty("user");
password=pro.getProperty("password");
driver=pro.getProperty("driver");
//注册驱动
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取连接
* @return 连接对象
*/
public static Connection getConnection() throws SQLException {
//需求:不想传递参数,还得保证工具类的通用性。
//解决方案:配置文件,只需要读取jdbc.properties就可以拿到url和user和password了
//但是文件的读取只需要做一次就可以了,因此把文件读取放到上面的静态代码块
return DriverManager.getConnection(url,user,password);
}
/**
* 释放资源
* @param stmt
* @param conn
*/
public static void close(Statement stmt,Connection conn){
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 释放资源
* @param rs
* @param stmt
* @param conn
*/
public static void close(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public class JdbcDemo1 {
public static void main(String[] args){
Connection conn =null;
Statement stmt =null;
ResultSet rs =null;
//注册驱动,获取数据库连接对象
try {
conn = JDBCUtils.getConnection();
//定义sql语句
String sql="select * from account";
//获取执行sql语句的对象
stmt=conn.createStatement();
//执行sql
rs=stmt.executeQuery(sql);
while (rs.next()){
int id = rs.getInt(1);
String name = rs.getString("name");
double balance = rs.getDouble(3);
System.out.println(id+" "+name+" "+balance);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//释放资源
JDBCUtils.close(rs,stmt,conn);
}
}
}
登录案例:
需求:通过键盘录入用户名和密码,判断用户是否成功
步骤:
CREATE TABLE USER(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32),
PASSWORD VARCHAR(32)
);`在这里插入代码片`
INSERT INTO USER VALUES(NULL,'Julia','123');
INSERT INTO USER VALUES(NULL,'Tom','234');
public class JdbcDemo1 {
//登录方法
public static boolean login(String username,String password){
if(username==null||password==null){
return false;
}
//连接数据库,判断是否登录成功
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
//注册驱动,获取数据库连接对象
conn = JDBCUtils.getConnection();
//定义SQL
String sql="select * from user where username='"+username+"' and password='"+password+"'";
stmt=conn.createStatement();
rs = stmt.executeQuery(sql);
//判断有没有查询出数据,如果有则成功
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//释放资源
JDBCUtils.close(rs,stmt,conn);
}
return false;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String username = scanner.next();
System.out.println("请输入密码");
String password = scanner.next();
if(login(username,password)){
System.out.println("恭喜你登录成功");
}else {
System.out.println("用户名或密码错误!");
}
}
}
PreparedStatement对象:优化上面的登录案例
Sql注入问题:在拼接sql时,有一些sql的特殊关键字参与字符串的拼接,会造成安全性问题
预编译sql:参数使用?作为占位符
public class JdbcDemo1 {
//登录方法
public static boolean login(String username,String password){
if(username==null||password==null){
return false;
}
//连接数据库,判断是否登录成功
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
//注册驱动,获取数据库连接对象
conn = JDBCUtils.getConnection();
//定义SQL
String sql="select * from user where username= ? and password=?";
//获取sql的执行对象
pstmt=conn.prepareStatement(sql);
//给问号赋值
pstmt.setString(1,username);
pstmt.setString(2,password);
//执行sql语句
rs=pstmt.executeQuery();
//判断有没有查询出数据,如果有则成功
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//释放资源
JDBCUtils.close(rs,pstmt,conn);
}
return false;
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名");
String username = scanner.next();
System.out.println("请输入密码");
String password = scanner.next();
if(login(username,password)){
System.out.println("恭喜你登录成功");
}else {
System.out.println("用户名或密码错误!");
}
}
}
事务:一个包含多个步骤的业务操作,如果这个业务操作被事务管理,则这多个步骤要么同时成功,要么同时失败。
使用的是Connection对象来管理事务
操作:
public class JdbcDemo1 {
public static void main(String[] args){
Connection conn =null;
PreparedStatement pstmt1=null;
PreparedStatement pstmt2=null;
try {
conn= JDBCUtils.getConnection();
//开启事务
conn.setAutoCommit(false);
//定义sql语句 转账
String sql1="update account set balance=balance-? where id=?";
String sql2="update account set balance=balance+? where id=?";
pstmt1=conn.prepareStatement(sql1);
pstmt2=conn.prepareStatement(sql2);
//给问号赋值
pstmt1.setDouble(1,500);
pstmt1.setInt(2,1);
pstmt2.setDouble(1,500);
pstmt2.setInt(2,2);
//执行sql语句
pstmt1.executeUpdate();
pstmt2.executeUpdate();
//提交事务
conn.commit();
} catch (Exception e) {
//不论出现什么异常,都回滚
try {
if(conn!=null)
conn.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
}finally {
//释放资源
JDBCUtils.close(pstmt1,conn);
JDBCUtils.close(pstmt2,null);
}
}
}
数据库连接池:
使用步骤:
public class C3P0Demo {
public static void main(String[] args) throws Exception {
//创建数据库连接池对象
DataSource ds = new ComboPooledDataSource();
//获取数据库连接对象
Connection conn = ds.getConnection();
System.out.println(conn);
}
}
步骤:
public class DruidDemo {
public static void main(String[] args) throws Exception {
//导入包
//定义配置文件
//加载配置文件
Properties pro = new Properties();
InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//获取连接池对象
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
//获取数据库连接对象
Connection conn = ds.getConnection();
System.out.println(conn);
}
}
定义工具类:
public class JDBCUtils {
private static DataSource ds;
static {
try {
//加载配置文件
Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
//获取ds
ds= DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取数据库连接对象
* @return 数据库连接对象
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 释放资源
* @param stmt
* @param conn
*/
public static void close(Statement stmt,Connection conn){
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close(); //归还连接
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 释放资源
* @param rs
* @param stmt
* @param conn
*/
public static void close(ResultSet rs,Statement stmt, Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 获取连接池的方法
*/
public static DataSource getDataSource(){
return ds;
}
}
druid_工具类测试:
public class DruidDemo {
public static void main(String[] args){
//获取数据库连接对象
Connection conn = null;
PreparedStatement pstmt =null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
//定义sql
String sql="select * from account where balance=?";
//获取执行sql的对象
pstmt = conn.prepareStatement(sql);
//给?赋值
pstmt.setDouble(1,3000);
//执行sql
rs = pstmt.executeQuery();
//遍历结果集
while (rs.next()){
int id = rs.getInt(1);
String name = rs.getString("name");
double balance = rs.getDouble(3);
System.out.println(id+" "+name+" "+balance);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//释放资源
JDBCUtils.close(rs,pstmt,conn);
}
}
}
Spring框架对JDBC的简单封装,提供了一个JDBCTemplate对象简化JDBC的开发
步骤:
public class DruidDemo {
public static void main(String[] args){
//创建jdbcTemplate对象
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
//执行方法
String sql="update account set balance =? where id=?";
int count = template.update(sql, 5000, 6);
System.out.println(count);
//不需要再释放连接了,因为他会自动释放资源
}
}
public class DruidDemo {
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
//使用Junit单元测试,可以使方法独立运行
@Test
public void test1(){
//查询id为1的记录并封装为map
String sql="select * from account where id=?";
Map<String, Object> map = template.queryForMap(sql,1);
System.out.println(map); //{id=1, name=zhangsan, balance=10000}
}
@Test
public void test2(){
//查询所有的记录封装为list集合
String sql="select * from account";
List<Map<String, Object>> maps = template.queryForList(sql);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
/**
* 打印结果为:
* {id=1, name=zhangsan, balance=10000}
* {id=2, name=lisi, balance=1500}
* {id=3, name=Lisa, balance=3000}
* {id=4, name=Mary, balance=2000}
* {id=5, name=Mary, balance=2000}
* {id=6, name=王五, balance=5000}
*/
}
@Test
public void test3(){
//查询所有记录,将其封装为javaBean对象并封装到list集合中
String sql="select * from account";
//两个参数,第二个参数是告诉他要封装为什么对象,自己实现封装过程
List<Account> list = template.query(sql, new RowMapper<Account>() {
@Override
public Account mapRow(ResultSet rs, int i) throws SQLException {
Account account=new Account();
int id = rs.getInt("id");
String name = rs.getString("name");
double balance = rs.getDouble("balance");
account.setId(id);
account.setName(name);
account.setBalance(balance);
return account;
}
});
//遍历list集合
for (Account account : list) {
System.out.println(account);
}
}
@Test
public void test4(){
String sql="select * from account";
//第二个参数告诉他要封装为什么对象,并把class类传给他,并且让他自己实现封装
List<Account> list = template.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
//遍历list
for (Account account : list) {
System.out.println(account);
}
}
@Test
public void test5(){
//查询总记录数
String sql="select count(id) from account";
//第2个参数传递希望的返回值结果类型
Long count = template.queryForObject(sql, Long.class);
System.out.println(count);
}
}
JavaWeb:使用Java语言开发基于互联网的项目
软件架构:
C/S:
B/S:
概念:Hyper Text Markup Language 超文本标记语言
基础入门:
<form action="#" method="get">
<label for="name">用户名:</label>
<input type="text" id="name" name="username" placeholder="请输入用户名"><br>
<label for="password">密码:</label>
<input type="password" name="password" placeholder="请输入密码" id="password"><br>
性别:<input type="radio" name="sex" value="male"> 男
<input type="radio" name="sex" value="female"> 女 <br>
爱好:<input type="checkbox" name="aihao" value="swim">游泳
<input type="checkbox" name="aihao" value="java">java
<input type="checkbox" name="aihao" value="sing">唱歌 <br>
上传文件: <input type="file" name="file"> <br>
隐藏域: <input type="hidden" name="hidden" value="aaa"> <br>
色器: <input type="color" name="color"> <br>
生日: <input type="date" name="birth"> <br>
<input type="datetime-local" name="birth"> <br>
邮箱: <input type="email" name="email"> <br>
年龄: <input type="number" name="age"> <br>
<input type="submit" value="提交">
</form>
HTML要显示一些特殊字符:
案例:旅游网站的首页:
步骤:
1、使用table来完成布局
2、如果某一行只有一个单元格,则使用< tr>< td>< /td>< /tr>
3、如果一行有多个单元格,则使用表格的嵌套< tr>< td>< table>< /table>< /td>< /tr>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!--指定字符集是中文-->
<title>黑马旅游网</title>
</head>
<body>
<!--采用table来完成布局-->
<!--最外层的表格,用于整个页面的布局-->
<table align="center" width="100%">
<!--第1行-->
<tr>
<td>
<img src="../image/top_banner.jpg" alt="" width="100%">
</td>
</tr>
<!--第2行-->
<tr>
<td>
<table align="center" width="100%">
<tr>
<td><img src="../image/logo.jpg" alt=""></td>
<td><img src="../image/search.png" alt=""></td>
<td><img src="../image/hotel_tel.png" alt=""></td>
</tr>
</table>
</td>
</tr>
<!--第3行-->
<tr>
<td>
<table align="center" width="100%">
<tr bgcolor="orange" align="center" height="45">
<td><a href="">首页</a></td>
<td><a href="">首页</a></td>
<td><a href="">首页</a></td>
<td><a href="">首页</a></td>
<td><a href="">首页</a></td>
<td><a href="">首页</a></td>
<td><a href="">首页</a></td>
<td><a href="">首页</a></td>
<td><a href="">首页</a></td>
</tr>
</table>
</td>
</tr>
<!--第4行-->
<tr>
<td>
<img src="../image/banner_3.jpg" alt="" width="100%">
</td>
</tr>
<!--第5行-->
<tr>
<td>
<img src="../image/icon_5.jpg" alt="">
<font>黑马精选</font>
<hr color="orange" size="3px">
</td>
</tr>
<!--第6行-->
<tr>
<td>
<table align="center" width="95%">
<tr>
<td>
<img src="../image/jiangxuan_1.jpg" alt="">
<p>上海的哈达阿呆挨打司法打算戴大哥大动感啊大大的</p>
<font color="red">¥998</font>
</td>
<td>
<img src="../image/jiangxuan_1.jpg" alt="">
<p>上海的哈达阿呆挨打司法打算戴大哥大动感啊大大的</p>
<font color="red">¥998</font>
</td>
<td>
<img src="../image/jiangxuan_1.jpg" alt="">
<p>上海的哈达阿呆挨打司法打算戴大哥大动感啊大大的</p>
<font color="red">¥998</font>
</td>
<td>
<img src="../image/jiangxuan_1.jpg" alt="">
<p>上海的哈达阿呆挨打司法打算戴大哥大动感啊大大的</p>
<font color="red">¥998</font>
</td>
</tr>
</table>
</td>
</tr>
<!--第7行-->
<tr>
<td>
<img src="../image/icon_6.jpg" alt="">
<font>国内游</font>
<hr color="orange" size="3px">
</td>
</tr>
<!--第8行-->
<tr>
<td>
<table align="center" width="100%">
<tr>
<td rowspan="2">
<img src="../image/jiangwai_1.jpg" alt="">
</td>
<td>
<img src="../image/jiangxuan_2.jpg" alt="">
<p>hadahdafhdahdasada</p>
<font color="red">¥699</font>
</td>
<td>
<img src="../image/jiangxuan_2.jpg" alt="">
<p>hadahdafhdahdasada</p>
<font color="red">¥699</font>
</td>
<td>
<img src="../image/jiangxuan_2.jpg" alt="">
<p>hadahdafhdahdasada</p>
<font color="red">¥699</font>
</td>
</tr>
<tr>
<td>
<img src="../image/jiangxuan_2.jpg" alt="">
<p>hadahdafhdahdasada</p>
<font color="red">¥699</font>
</td>
<td>
<img src="../image/jiangxuan_2.jpg" alt="">
<p>hadahdafhdahdasada</p>
<font color="red">¥699</font>
</td>
<td>
<img src="../image/jiangxuan_2.jpg" alt="">
<p>hadahdafhdahdasada</p>
<font color="red">¥699</font>
</td>
</tr>
</table>
</td>
</tr>
<!--第9行-->
<tr>
<td>
<img src="../image/footer_service.png" alt="" width="100%">
</td>
</tr>
<!--第10行-->
<tr bgcolor="orange">
<td>
<center>
<font color="#696969" size="3px">江苏传智播客教育科技股份有限公司 <br>
版权所有Copyright 2006-2018, All Rights Reserved 苏ICP备16007882</font>
</center>
</td>
</tr>
</table>
</body>
</html>
案例:注册页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!--指定字符集是中文-->
<title>hello</title>
</head>
<body>
<form action="#" method="post">
<table align="center" border="1">
<tr>
<td>
<label for="name">用户名</label>
</td>
<td>
<input type="text" name="username" id="name" placeholder="请输入账号">
</td>
</tr>
<tr>
<td><label for="password">密码</label></td>
<td>
<input type="password" name="password" id="password" placeholder="请输入密码">
</td>
</tr>
<tr>
<td><label for="email">邮箱</label></td>
<td><input type="email" name="email" id="email" placeholder="请输入email"></td>
</tr>
<tr>
<td><label for="name1">姓名</label></td>
<td><input type="text" name="name1" id="name1" placeholder="请输入真实姓名"></td>
</tr>
<tr>
<td><label for="phone">手机号</label></td>
<td><input type="text" name="phone" id="phone" placeholder="请输入您的手机号"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="sex" value="male"> 男
<input type="radio" name="sex" value="femal"> 女
</td>
</tr>
<tr>
<td>出生年月</td>
<td><input type="date" name="birth"></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="code"><img src="../image/verify_code.jpg" alt=""></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>
Cascading Style Sheets:层叠样式表。页面美化和布局控制。
好处:
CSS的使用:与html结合
选择器:筛选具有相似特征的元素。
分类:
注意事项:每一对属性需要用分号隔开,最后一个不用分号。
属性:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!--指定字符集是中文-->
<title>hello</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background:url("../image/register_bg.png") no-repeat center;
}
.rg_layout{
border: 8px solid black;
width: 900px;
height: 500px;
background-color: #fff;
/*让div水平居中*/
margin: auto;
margin-top: 15px;
}
.rg_left{
float: left;
margin: 15px;
}
.rg_left p:first-child{
font-size: 20px;
color: orange;
}
.rg_left p:last-child{
font-size: 20px;
color: dimgrey;
}
.rg_center{
float: left;
width: 450px;
}
.rg_right{
float: right;
margin: 15px;
}
.rg_right p{
font-size: 10px;
}
.rg_right p a{
color: indianred;
}
.td_left{
text-align: right;
width: 100px;
height: 45px;
}
.td_right{
padding-left: 50px;
}
.text{
width: 251px;
height: 32px;
border:1px solid grey;
padding: 10px;
}
#code{
width: 110px;
height: 32px;
/*设置图片垂直居中*/
vertical-align: middle;
}
#btn{
width: 150px;
height: 40px;
background-color: yellow;
border: 1px solid yellow;
}
</style>
</head>
<body>
<div class="rg_layout">
<div class="rg_left">
<p>新用户注册</p>
<p>USER REGISTER</p>
</div>
<div class="rg_center">
<div class="rg_form">
<form action="#" method="post">
<table>
<tr>
<td class="td_left">
<label for="name">用户名</label>
</td>
<td class="td_right">
<input type="text" name="username" id="name" placeholder="请输入账号" class="text">
</td>
</tr>
<tr>
<td class="td_left"><label for="password">密码</label></td>
<td class="td_right">
<input type="password" name="password" id="password" placeholder="请输入密码" class="text">
</td>
</tr>
<tr>
<td class="td_left"><label for="email">邮箱</label></td>
<td class="td_right"><input type="email" name="email" id="email" placeholder="请输入email" class="text"></td>
</tr>
<tr>
<td class="td_left"><label for="name1">姓名</label></td>
<td class="td_right"><input type="text" name="name1" id="name1" placeholder="请输入真实姓名" class="text"></td>
</tr>
<tr>
<td class="td_left"><label for="phone">手机号</label></td>
<td class="td_right"><input type="text" name="phone" id="phone" placeholder="请输入您的手机号" class="text"></td>
</tr>
<tr>
<td class="td_left">性别</td>
<td class="td_right">
<input type="radio" name="sex" value="male"> 男
<input type="radio" name="sex" value="femal"> 女
</td>
</tr>
<tr>
<td class="td_left">出生年月</td>
<td class="td_right"><input type="date" name="birth"></td>
</tr>
<tr>
<td class="td_left">验证码</td>
<td class="td_right"><input type="text" name="code"><img src="../image/verify_code.jpg" alt="" id="code"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="注册" id="btn"></td>
</tr>
</table>
</form>
</div>
</div>
<div class="rg_right">
<p>已有账号?<a href="#">立即登录</a></p>
</div>
</div>
</body>
</html>
一门客户端脚本语言,运行在客户端浏览器中的。每一个浏览器都有JavaScript的解析引擎。
脚本语言:不需要进行编译,直接就可以被浏览器解析执行。
功能:
JavaScript=ECMAScript+JavaScript自己特有的东西(BOM+DOM)
基本语法:
内部js:定义script标签,标签体内容就是js代码
外部js:定义script标签,通过src属性引入外部的js文件
注意:script可以定义在html页面的任何地方,但是定义位置会影响执行顺序;可以定义多个script标签
原始数据类型:
变量定义的语法:var 变量名=初始化值。要知道变量是什么类型的,用typeof方法。特殊:null运算后得到的是object
其他类型转number:
注意:正负号
在JS中,如果运算数并不是运算符所要求的数据类型,引擎会自动的将运算数进行类型转换。
<script>
var a= +"string";
a++;
document.write(typeof(a)); //number
document.write(a); //NAN
var flag1=+ true;
var flag2=+ false;
document.write(typeof(flag1));//number
document.write(flag1+1); //2
document.write(flag2+1); //1
</script>
比较运算:
其他类型转boolean:
<script>
//number
var num =3;
document.write(!!num); //true
var num2=0;
document.write(!!num2); //false
var num3=NaN;
document.write(!!num3); //false
//string
var str1="hello";
document.write(!!str1); //true
var str2="";
document.write(!!str2); //false
//null&undefined
var a=null;
document.write(!!a); //false
var b=undefined;
document.write(!!b); //false
//对象
var date=new Date();
document.write(!!date); //true
</script>
案例:九九乘法表:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!--指定字符集是中文-->
<title>hello</title>
<style>
td{
border: 1px solid black;
}
</style>
<script>
document.write("");
document.write("" +"九九乘法表"+">");
for(var i=1;i<=9;i++){
document.write("");
for(var j=1;j<=i;j++){
document.write("");
document.write(i+"*"+j+"="+(i*j)+"  ");
document.write(" ");
}
document.write(" ");
}
document.write("
")
</script>
</head>
<body>
</body>
</html>
Date日期对象:
<script>
var date=new Date();
document.write(date+"
");
//返回本地字符串时间格式
document.write(date.toLocaleString());
//返回毫秒值
document.write(date.getTime());
</script>
Function函数对象:
//求任意个数的和
function add() {
var sum=0;
for(var i =0;i<arguments.length;i++){
sum+=arguments[i];
}
return sum;
}
var res=add(1,2,3);
alert(res);
Array数组对象:
Math数学对象:
//随机产生1-100之间的整数
var num=Math.floor(Math.random()*100)+1;
document.write(num)
RegExp正则表达式对象:
正则表达式:定义字符串的组成规则
正则表达式对象:
var reg1=new RegExp("^\\w{6,12}$");
var reg2=/^\w{
6,12}$/;
var flag=reg2.test("zhangsan");
alert(flag);
Global对象:
概念:Browser Object Model 浏览器对象模型
案例1:轮播图
<img src="../image/banner_1.jpg" width="100%" id="img">
<script>
//修改图片src的方法
var num=1;
function fun() {
num++;
//判断number是否大于3
if(num>3){
num=1;
}
var img=document.getElementById("img");
img.src="../image/banner_"+num+".jpg";
}
//定义定时器
setInterval(fun,3000);
</script>
案例2:自动跳转首页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
<style>
#time{
color: red;
}
p{
text-align: center;
}
</style>
</head>
<body>
<p> <span id="time">5</span>秒之后,自动跳转到首页...</p>
<script>
var num=5;
var time = document.getElementById("time");
function fun() {
num--;
if(num==0){
//跳转
location.href="http://www.baidu.com";
}
time.innerText=num;
}
setInterval(fun,1000);
</script>
</body>
</html>
DOM对象模型:Document Object Model 文档对象模型
将标记语言文档的各个组成部分,封装为对象,可以使用这些对象,对标记语言文档进行CRUD动态操作。
核心DOM模型:
超链接的功能:
1、可以被点击,样式
2、点击后跳转到href指定的url
需求:保留1功能,去掉2功能。实现:href=“javascript:void(0);”
案例3 动态表格
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
<style>
div{
text-align: center;
margin: 50px;
}
table{
width: 500px;
border: 1px solid black;
margin: auto;
}
tr,td,th{
border: 1px solid black;
text-align: center;
}
</style>
</head>
<body>
<div>
<input type="text" name="id" placeholder="请输入编号" id="id">
<input type="text" name="name" placeholder="请输入姓名" id="name">
<input type="text" name="sex" placeholder="请输入性别" id="sex">
<input type="button" value="添加" id="add">
</div>
<table id="table">
<caption>学生信息表</caption>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>操作</th>
</tr>
<tr>
<td>1</td>
<td>令狐冲</td>
<td>男</td>
<td><a href="javascript:void(0)" onclick="del_row(this)">删除</a></td>
</tr>
<tr>
<td>2</td>
<td>任我行</td>
<td>男</td>
<td><a href="javascript:void(0)" onclick="del_row(this)">删除</a></td>
</tr>
<tr>
<td>3</td>
<td>岳不群</td>
<td>?</td>
<td><a href="javascript:void(0)" onclick="del_row(this)">删除</a></td>
</tr>
</table>
<script>
//添加
var add = document.getElementById(`add`);
add.onclick=function () {
//获取文本框中值
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
var sex = document.getElementById("sex").value;
//创建td,将文本框中的内容放进去
var td1 = document.createElement("td");
td1.innerText=id;
var td2 = document.createElement("td");
td2.innerText=name;
var td3 = document.createElement("td");
td3.innerText=sex;
var td4 = document.createElement("td");
var a = document.createElement("a");
a.innerText="删除";
a.setAttribute("href","javascript:void(0);");
a.setAttribute("onclick","del_row(this)");
td4.appendChild(a);
//创建tr,将td放进去
var tr = document.createElement("tr");
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
//将tr放进table中
document.getElementById("table").appendChild(tr);
};
//删除
function del_row(obj) {
//此时obj为点的a对象
//若要删除这一行tr,那么需要获取到table
var table = obj.parentNode.parentNode.parentNode;
table.removeChild(obj.parentNode.parentNode);
}
</script>
</body>
</html>
HTML DOM模型:
事件:某些操作:如:单击、双击、键盘按下、鼠标移动
事件源:组件。如:按钮,文本输入框…
监听器:代码。
注册监听:将事件源和事件,监听器结合在一起。当事件源上发生了某个事件。则触发执行某个监听器代码。
常见的事件:
案例4 表格全选
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
<style>
table{
margin: auto;
width: 500px;
border: 1px solid black;
}
tr,td,th{
border: 1px solid black;
text-align: center;
}
div{
text-align: center;
margin-top: 10px;
}
</style>
</head>
<body>
<table>
<caption>学生信息表</caption>
<tr>
<th><input type="checkbox" name="checkbox" id="cb"></th>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>操作</th>
</tr>
<tr>
<td><input type="checkbox" name="checkbox"></td>
<td>1</td>
<td>令狐冲</td>
<td>男</td>
<td><a href="javascript:void(0)">删除</a></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox"></td>
<td>2</td>
<td>任我行</td>
<td>男</td>
<td><a href="javascript:void(0)">删除</a></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox"></td>
<td>3</td>
<td>岳不群</td>
<td>男</td>
<td><a href="javascript:void(0)">删除</a></td>
</tr>
</table>
<div>
<input type="button" value="全选" id="all">
<input type="button" value="全不选" id="notall">
<input type="button" value="反选" id="rev">
</div>
<script>
var all = document.getElementById("all");
var notall = document.getElementById("notall");
var rev = document.getElementById("rev");
var checkboxs = document.getElementsByName("checkbox");
//点击全选
all.onclick=function () {
//获取所有的checkbox都选上
for(var i=0;i<checkboxs.length;i++){
checkboxs[i].checked=true;
}
};
//点击全不选
notall.onclick=function () {
//将所有checkbox都不选
for(var i=0;i<checkboxs.length;i++){
checkboxs[i].checked=false;
}
};
//点击反选
rev.onclick=function () {
//将所有的已经选的变成不选,不选的变成选
for(var i =0;i<checkboxs.length;i++){
if(checkboxs[i].checked){
checkboxs[i].checked=false;
}else{
checkboxs[i].checked=true;
}
}
};
//点击了第一个checkbox后让下面所有的checkbox都与他的状态相等
var cb = document.getElementById("cb");
cb.onclick=function () {
for(var i=1;i<checkboxs.length;i++){
checkboxs[i].checked=this.checked;
}
};
var trs = document.getElementsByTagName("tr");
for(var i=0;i<trs.length;i++){
trs[i].onmouseover=function () {
this.style.backgroundColor="pink";
};
trs[i].onmouseout=function () {
this.style.backgroundColor="white";
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <!--指定字符集是中文-->
<title>hello</title>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
background:url("../image/register_bg.png") no-repeat center;
}
.rg_layout{
border: 8px solid black;
width: 900px;
height: 500px;
background-color: #fff;
/*让div水平居中*/
margin: auto;
margin-top: 15px;
}
.rg_left{
float: left;
margin: 15px;
}
.rg_left p:first-child{
font-size: 20px;
color: orange;
}
.rg_left p:last-child{
font-size: 20px;
color: dimgrey;
}
.rg_center{
float: left;
width: 450px;
}
.rg_right{
float: right;
margin: 15px;
}
.rg_right p{
font-size: 10px;
}
.rg_right p a{
color: indianred;
}
.td_left{
text-align: right;
width: 100px;
height: 45px;
}
.td_right{
padding-left: 50px;
}
.text{
width: 251px;
height: 32px;
border:1px solid grey;
padding: 10px;
}
#code{
width: 110px;
height: 32px;
/*设置图片垂直居中*/
vertical-align: middle;
}
#btn{
width: 150px;
height: 40px;
background-color: yellow;
border: 1px solid yellow;
}
.error{
color: red;
font-size: 12px;
}
</style>
</head>
<body>
<div class="rg_layout">
<div class="rg_left">
<p>新用户注册</p>
<p>USER REGISTER</p>
</div>
<div class="rg_center">
<div class="rg_form">
<form action="#" method="get" id="form">
<table>
<tr>
<td class="td_left">
<label for="name">用户名</label>
</td>
<td class="td_right">
<input type="text" name="username" id="name" placeholder="请输入用户名" class="text">
<span id="s_username" class="error"></span>
</td>
</tr>
<tr>
<td class="td_left"><label for="password">密码</label></td>
<td class="td_right">
<input type="password" name="password" id="password" placeholder="请输入密码" class="text">
<span id="s_password" class="error"></span>
</td>
</tr>
<tr>
<td class="td_left"><label for="email">邮箱</label></td>
<td class="td_right"><input type="email" name="email" id="email" placeholder="请输入email" class="text"></td>
</tr>
<tr>
<td class="td_left"><label for="name1">姓名</label></td>
<td class="td_right"><input type="text" name="name1" id="name1" placeholder="请输入真实姓名" class="text"></td>
</tr>
<tr>
<td class="td_left"><label for="phone">手机号</label></td>
<td class="td_right"><input type="text" name="phone" id="phone" placeholder="请输入您的手机号" class="text"></td>
</tr>
<tr>
<td class="td_left">性别</td>
<td class="td_right">
<input type="radio" name="sex" value="male"> 男
<input type="radio" name="sex" value="femal"> 女
</td>
</tr>
<tr>
<td class="td_left">出生年月</td>
<td class="td_right"><input type="date" name="birth"></td>
</tr>
<tr>
<td class="td_left">验证码</td>
<td class="td_right"><input type="text" name="code"><img src="../image/verify_code.jpg" alt="" id="code"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="注册" id="btn"></td>
</tr>
</table>
</form>
</div>
</div>
<div class="rg_right">
<p>已有账号?<a href="#">立即登录</a></p>
</div>
</div>
<script>
//给表单绑定onsubmit事件,监听器中监听每一个校验结果
//定义一些方法分别校验各个表单项
//校验用户名
function checkUsername(){
//正则表达式
var name=document.getElementById("name").value;
//var reg=new RegExp("\\w{6,12}");
var reg=/^\w{
6,12}$/;
var flag=reg.test(name);
var username = document.getElementById("s_username");
if(flag){
//提示绿色的对勾
username.innerHTML="";
}else {
//提示红色的用户名有误
username.innerText="用户名输入有误";
}
return flag;
}
//校验密码
function checkPassword(){
var password=document.getElementById("password").value;
var reg=/^\w{
6,12}$/;
var flag=reg.test(password);
var password2 = document.getElementById("s_password");
if(flag){
//提示绿色的对勾
password2.innerHTML="";
}else {
password2.innerText="密码格式有误";
}
return flag;
}
document.getElementById("form").onclick=function () {
//调用用户校验方法
//调用密码校验方法
return checkUsername() && checkPassword();
};
//给用户名和密码框绑定离焦事件
document.getElementById("name").onblur=checkUsername;
document.getElementById("password").onblur=checkPassword;
</script>
</body>
</html>
Bootstrap:一个前端开发的框架
Bootstrap中文网
快速入门:
基本摸版:
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Templatetitle>
<link href="../css/bootstrap.min.css" rel="stylesheet">
<script src="../js/jquery-3.2.1.min.js">script>
<script src="../js/bootstrap.min.js">script>
head>
<body>
<h1>你好,世界!h1>
body>
html>
响应式布局:依赖于栅格系统,将一行平均分成12个格子,可以指定一个元素占几个格子。
步骤:
注意:如果超出12,会自动换行。栅格类属性可以向上兼容。如果真实设备宽度小于了设置栅格类属性的设备代号代码的最小值,默认会一个元素占满一整行。
全局CSS样式
组件:
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigationspan>
<span class="icon-bar">span>
<span class="icon-bar">span>
<span class="icon-bar">span>
button>
<a class="navbar-brand" href="#">首页a>
div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)span>a>li>
<li><a href="#">Linka>li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret">span>a>
<ul class="dropdown-menu">
<li><a href="#">Actiona>li>
<li><a href="#">Another actiona>li>
<li><a href="#">Something else herea>li>
<li role="separator" class="divider">li>
<li><a href="#">Separated linka>li>
<li role="separator" class="divider">li>
<li><a href="#">One more separated linka>li>
ul>
li>
ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
div>
<button type="submit" class="btn btn-default">Submitbutton>
form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Linka>li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret">span>a>
<ul class="dropdown-menu">
<li><a href="#">Actiona>li>
<li><a href="#">Another actiona>li>
<li><a href="#">Something else herea>li>
<li role="separator" class="divider">li>
<li><a href="#">Separated linka>li>
ul>
li>
ul>
div>
div>
nav>
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«span>
a>
li>
<li><a href="#">1a>li>
<li><a href="#">2a>li>
<li><a href="#">3a>li>
<li><a href="#">4a>li>
<li><a href="#">5a>li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»span>
a>
li>
ul>
nav>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active">li>
<li data-target="#carousel-example-generic" data-slide-to="1">li>
<li data-target="#carousel-example-generic" data-slide-to="2">li>
ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="..." alt="...">
div>
<div class="item">
<img src="..." alt="...">
div>
div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true">span>
<span class="sr-only">Previousspan>
a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true">span>
<span class="sr-only">Nextspan>
a>
div>
案例:旅游网:利用栅格系统来布局
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Templatetitle>
<link href="../css/bootstrap.min.css" rel="stylesheet">
<script src="../js/jquery-3.2.1.min.js">script>
<script src="../js/bootstrap.min.js">script>
<style>
.margintop{
margin-top: 10px;
}
.search-input{
float: left;
border: 2px solid #ffc900;
width: 400px;
height: 35px;
padding-left: 5px;
margin-top: 15px;
}
.search-btn{
float: left;
height: 35px;
border: 2px solid #ffc900;
width: 90px;
background-color: #ffc900;
text-align: center;
line-height: 35px;
margin-top: 15px;
}
.bottomline{
border-bottom: 2px solid #ffc900;
padding: 5px;
}
style>
head>
<body>
<header class="container-fluid">
<div class="row">
<img src="../image/top_banner.jpg" class="img-responsive">
div>
<div class="row margintop">
<div class="col-md-3"><img src="../image/logo.jpg" class="img-responsive">div>
<div class="col-md-5">
<input type="text" placeholder="请输入线路名称" class="search-input">
<a href="javascript:void(0)" class="search-btn">搜索a>
div>
<div class="col-md-4"><img src="../image/hotel_tel.png">div>
div>
<div class="row">
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigationspan>
<span class="icon-bar">span>
<span class="icon-bar">span>
<span class="icon-bar">span>
button>
<a class="navbar-brand" href="#">首页a>
div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)span>a>li>
<li><a href="#">Linka>li>
<li><a href="#">Linka>li>
<li><a href="#">Linka>li>
<li><a href="#">Linka>li>
ul>
div>
div>
nav>
div>
<div class="row">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active">li>
<li data-target="#carousel-example-generic" data-slide-to="1">li>
<li data-target="#carousel-example-generic" data-slide-to="2">li>
ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="../image/banner_1.jpg" alt="...">
div>
<div class="item">
<img src="../image/banner_2.jpg" alt="...">
div>
<div class="item">
<img src="../image/banner_3.jpg" alt="...">
div>
div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true">span>
<span class="sr-only">Previousspan>
a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true">span>
<span class="sr-only">Nextspan>
a>
div>
div>
header>
<div class="container margintop">
<div class="row bottomline">
<img src="../image/icon_5.jpg">
<span>黑马精选span>
div>
<div class="row margintop">
<div class="col-md-3">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由行(春节销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
<div class="col-md-3">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由行(春节销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
<div class="col-md-3">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由行(春节销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
<div class="col-md-3">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由行(春节销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
div>
<div class="row bottomline">
<img src="../image/icon_6.jpg">
<span>国内游span>
div>
<div class="row margintop">
<div class="col-md-4">
<img src="../image/guonei_1.jpg" class="img-responsive">
div>
<div class="col-md-8">
<div class="row">
<div class="col-md-4">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由dasfaasfafafaf行(春节销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
<div class="col-md-4">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由fafafafafafasfafasf行(春节销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
<div class="col-md-4">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由行(春节fafafasfafafasfa销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
div>
<div class="row">
<div class="col-md-4">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由行(春节fasfasfasfasfasfa销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
<div class="col-md-4">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由行(春节fafafafafafafas销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
<div class="col-md-4">
<div class="thumbnail">
<img src="../image/jiangxuan_3.jpg">
<p>上海飞三亚4晚自由行(春节fasffsdfsfafafafafa销售+亲子游+自由行)p>
<font color="red">¥699font>
div>
div>
div>
div>
div>
div>
<footer class="container-fluid">
<div class="row">
<img src="../image/footer_service.png" class="img-responsive">
div>
<div class="row" style="background-color: #ffc900">
<center>
<font color="#696969" size="3px">江苏传智播客教育科技股份有限公司 <br>
版权所有Copyright 2006-2018, All Rights Reserved 苏ICP备16007882font>
center>
div>
footer>
body>
html>
XML:可扩展标记语言
功能:存储数据(配置文件)、在网络中传输
xml与html的区别:
xml文档基本语法:
<users>
<user id='1'>
<name>zhangsaanname>
<age>21age>
<gender>malegender>
user>
<user id='2'>
<name>lisiname>
<age>20age>
<gender>malegender>
user>
users>
约束:规定xml的书写规则
分类:
DTD:
解析:操作xml文档,将文档中的数据读取到内存中
解析XML的方式:
xml常见解析器:
Jsoup:
步骤:
Jsoup快速入门:
student.xml:
<students>
<student number="heima_0001">
<name>tomname>
<age>18age>
<sex>malesex>
student>
<student number="heima_0002">
<name>jackname>
<age>18age>
<sex>femalesex>
student>
students>
package jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
public class JsoupDemo1 {
public static void main(String[] args) throws IOException {
//获取Document对象,根据xml文档获得
//获取student.xml的路径
String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath();
//加载文档进内存
Document document = Jsoup.parse(new File(path), "utf-8");
//获取元素对象
Elements elements = document.getElementsByTag("name");
//获取第一个name的Element对象
Element element = elements.get(0);
//获取数据
String name = element.text();
System.out.println(name);
}
}
对象的使用:
快捷的查询方式:
public class JsoupDemo1 {
public static void main(String[] args) throws XpathSyntaxErrorException, IOException {
//获取Document对象,根据xml文档获得
//获取student.xml的路径
String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath();
//加载文档进内存
Document document = Jsoup.parse(new File(path), "utf-8");
//根据document对象创建JxDocument对象
JXDocument jxDocument=new JXDocument(document);
//结合Xpath语法查询
//查询所有的student标签
List<JXNode> jxNodes = jxDocument.selN("//student");
//查询所有student下的name标签
List<JXNode> jxNodes1 = jxDocument.selN("//student/name");
//查询student标签下带有id属性的name标签
List<JXNode> jxNodes2 = jxDocument.selN("//student/name[@id]");
//查询student标签下带有id属性的name标签,并且id的属性值为itcast
List<JXNode> jxNodes3 = jxDocument.selN("//student/name[@id='itcast']");
}
}