Spring Boot中关于 HTTP 中请求转发与重定向以及注解

1.控制器

    1.Controller
    2.RestController

 2.HTTP请求参数
 

    1.字符串查询参数
    2.协议主体中的负载
    3.URL路径
    

3.注解

1.工程目录


User.java 

package com.newer.anno;

//附加信息
//元数据信息,反射(黑魔法)可以获得这些信息
//源码级别,编译时的检查
//框架中定义了大量注解(5.0,早期使用XML)
//附加信息和代码写到一个文件中
@Table(db="hr",dbType = DbType.ORACLE,tableName="users")

public class User {

	@Element(name = "users") 
	public String sayHello() {
		return "hello";
	}
	
	/**
	 * 过时:不推荐继续使用
	 * 
	 * 推荐:String bye(String)
	 */
	@Deprecated
	public void bye() {
		System.out.println("再见");
	}
	
	public String bye(String msg) {
		return "bye"+msg;
	}
}

Student.java

package com.newer.anno;


@Table(tableName="students",db="hr")
public class Student extends User {
	
	
//	重写父类的方法
//	语法强制检查
//	注解,源码级别,编译时,检查父类中是否存在同名方法,若没有,编译不通过
//	安全性
	
//	目标:方法
//	保留:源码
	@Override
	public String sayHello() {
		
		return "你好";
	}
	
	
}

Element.java

package com.newer.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//注解作用的目标(类,字段,方法,参数....)
@Target(value = {ElementType.TYPE,
		ElementType.METHOD,
		
})

//RUNTIME,CLASS,SOURCE
@Retention(RetentionPolicy.RUNTIME)
public @interface Element {
	
//	定义注解的一个属性
	String name() default "";

}

Table.java

package com.newer.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Table {

	/**
	 * 表名
	 * @return
	 */
	String tableName() default "";
	
/**
 * 数据库名
 * @return
 */
	String db();
	
	/**
	 * 数据库类型
	 * @return
	 */
	DbType dbType() default DbType.MYSQL;
}

DbType.java

package com.newer.anno;

public enum DbType {

	ORACLE,DB2,MYSQL
}

App.java

package com.newer.anno;

import java.util.Date;

public class App {
	public static void main(String[] args) {
		
//		注解 java5开始
//		元数据编程
//		标记源码:存在的阶段:1.源码  2.类(默认)  3.运行时
		
//		批注
//		Date now=new Date();
//		
//		System.out.println(now.toLocaleString());
		
		
		User u1=new User();
		System.out.println(u1.sayHello());
		
		
		Student s1=new Student();
		System.out.println(s1.sayHello());
		s1.bye();
		s1.bye("java");
		
	}

}

以上就是注解的使用及定义,不足之处请大佬赐教!!!


4. HTTP 中请求转发与重定向

      1.工程目录


HomeController.java 

package com.newer.web2;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 
 * @author Admin
 *
 */

//注解,相当于贴标签扮演特定的角色
//Spring 容器管理的组件
//注解不等于注释
//定义注解,借助反射才能发挥作用
//Controller是组件
//作用:接收用户的请求,调用业务逻辑(执行逻辑),响应数据(json)或视图(html:直接静态,服务端引擎渲染的JSP,JSTL等)

@Controller
public class HomeController {

//	Get "/"
	@GetMapping("/")
	public String home() {
		return "index.html";
	}
	
//	@RequestParam(name="username") String user==String username
	
//	HTTP参数:字符串查询参数
	@GetMapping("/api1")
	public String login(String username,
			int age,String phone) {
		System.out.println("username:"+username);
		System.out.println("age:"+age);
		System.out.println("phone:"+phone);
		
//		视图名(路径,静态文件)
//		静态文件
//		请求转发到内部视图
		return "user.html";
	}
	
//	HTTP数据:Person来自表单数据,不需要写@RequestBody
	@PostMapping("/api2")
	public String api2(Person p) {
		System.out.println(p.getUser());
		System.out.println(p.getAge());
		System.out.println(p.getTel());
		
//		返回静态页面
//		return "user.html";
		
//		重定向
//		让用户发起一个新的HTTP Get请求,地址是user
//		地址发生改变
		return "redirect:user";
		
	}
	
	
//	Get "/user"--->user.html
	@GetMapping("/user")
	public String foo() {
		return "user.html";
	}
	

	
	
	
	
	
	
	
	
	
}

Person.java

package com.newer.web2;

public class Person {
	
//	与HTML页面的表单中字段名一致
	String user;
	int age;
	String tel;
	
	public Person() {
		
	}

	public Person(String user, int age, String tel) {
		super();
		this.user = user;
		this.age = age;
		this.tel = tel;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@Override
	public String toString() {
		return "Person [user=" + user + ", age=" + age + ", tel=" + tel + "]";
	}
	
	
}

Web2Application.java

package com.newer.web2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Web2Application {

	public static void main(String[] args) {
		SpringApplication.run(Web2Application.class, args);
	}

}

index.html

<!doctype html>
<html lang="en">

<head>
    <title>Title</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>
    <div class="container mt-5">
        <div class="jumbotron">
            <h1 class="display-3">表单</h1>
            <hr>
            <!-- 表单 -->
            <h1>GET/API1</h1>
            <h1>URL中的字符串查询参数</h1>
            <form 
            action="api1" method="get">
                <!-- 字段 -->
                <div class="form-group">
                    <label for="">用户名</label>
                    <input type="text" class="form-control" name="username" id="" aria-describedby="helpId"
                        placeholder="用户名">

                </div>
                <div class="form-group">
                    <label for="">年龄</label>
                    <input type="text" class="form-control" name="age" id="" aria-describedby="helpId" placeholder="年龄">

                </div>
                <div class="form-group">
                    <label for="">手机号</label>
                    <input type="text" class="form-control" name="phone" id="" aria-describedby="helpId"
                        placeholder="手机号">

                </div>

                <button type="submit" class="btn btn-primary btn-block">提交</button>

            </form>
            <hr>
              <!-- 表单 -->
              <h1>GET/API2</h1>
              <h1>HTTP请求中的表单数据</h1>
              <form 
              action="api2" method="post">
                  <!-- 字段 -->
                  <div class="form-group">
                      <label for="">用户名</label>
                      <input type="text" class="form-control" name="user" id="" aria-describedby="helpId"
                          placeholder="用户名">
  
                  </div>
                  <div class="form-group">
                      <label for="">年龄</label>
                      <input type="text" class="form-control" name="age" id="" aria-describedby="helpId" placeholder="年龄">
  
                  </div>
                  <div class="form-group">
                      <label for="">手机号</label>
                      <input type="text" class="form-control" name="tel" id="" aria-describedby="helpId"
                          placeholder="手机号">
  
                  </div>
  
                  <button type="submit" class="btn btn-primary btn-block">提交</button>
  
              </form>


        </div>
    </div>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
        integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
</body>

</html>

user.html

user

HTML+CSS+JS
使用JS动态加载数据

程序运行,浏览器界面:


 API1(请求转发)


API2 (重定向)

发布了113 篇原创文章 · 获赞 130 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44364444/article/details/105099338