|
@@ -0,0 +1,97 @@
|
|
|
+package com.lovecoding.springmvc;
|
|
|
+
|
|
|
+import com.lovecoding.springmvc.pojo.Student;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.CookieValue;
|
|
|
+import org.springframework.web.bind.annotation.RequestHeader;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.servlet.support.RequestContext;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+
|
|
|
+@Controller
|
|
|
+public class RequestController {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 我们可以直接在 控制器函数 实参内 获取 原生的参数类型
|
|
|
+ * 一般常用的参数有 HttpServletRequest HttpServletResponse HttpSession
|
|
|
+ * Spring框架会在 Bean对象集合内 帮我们 自动匹配参数, 要求是 实现类必须是 空间构造
|
|
|
+ * @param req
|
|
|
+ * @param resp
|
|
|
+ * @param session
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/students/req")
|
|
|
+ public String saveStudent(HttpServletRequest req, HttpServletResponse resp, HttpSession session){
|
|
|
+
|
|
|
+
|
|
|
+ System.out.println( "当前URL :" + req.getRequestURL() );
|
|
|
+ System.out.println( "当前编码: " + resp.getCharacterEncoding() );
|
|
|
+ System.out.println( "当前用户SessionID + " + session.getId() );
|
|
|
+
|
|
|
+ return "hello";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 我们在设计 API 接口的时候, 一般自定义数据实体
|
|
|
+ * SpringMVC框架 允许我们将自己定义的 Pojo 实体类 当作参数 传入
|
|
|
+ * 这样我们直接就可以拿到 实体类对象了, 非常的方便!
|
|
|
+ * 不需要使用第三方工具 转化对象类型, 和 set 数据了
|
|
|
+ * @param student
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/students/add")
|
|
|
+ public String saveStudent(Student student){
|
|
|
+
|
|
|
+ System.out.println( student );
|
|
|
+
|
|
|
+ return "/page/hello.jsp";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 我们可以在 控制器函数 实参内 定义自己想要的 数据
|
|
|
+ * URL 如果携带了 相关参数(参数名匹配), 那么我们就可以直接获得参数值
|
|
|
+ * Spring 框架会自动入参
|
|
|
+ * @param req
|
|
|
+ * @param name
|
|
|
+ * @param age
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/students/save")
|
|
|
+ public String saveStudent(HttpServletRequest req, String name, int age ){
|
|
|
+
|
|
|
+ String contextPath = req.getContextPath();
|
|
|
+ System.out.println( "contextPath" + contextPath );
|
|
|
+ System.out.println( "name " + name );
|
|
|
+ System.out.println( "age " + age );
|
|
|
+ return "/page/hello.jsp";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 我们可以使用一些 参数内注解, 单独处理请求参数
|
|
|
+ * 比方说 RequestParam 可以修改参数是否是 必填参数
|
|
|
+ * 比方说 RequestHeader 可以直接获取 Header值
|
|
|
+ * @param agent
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping("/students/agent")
|
|
|
+ public String saveStudent( @RequestHeader("User-Agent") String agent ){
|
|
|
+
|
|
|
+ System.out.println( "用户浏览器 Agent:" + agent );
|
|
|
+
|
|
|
+ return "/student.jsp";
|
|
|
+ }
|
|
|
+
|
|
|
+ @RequestMapping("/students/cookie")
|
|
|
+ public String cookie( @CookieValue("JSESSIONID") String sessionId ){
|
|
|
+
|
|
|
+ System.out.println( "用户 JSESSIONID:" + sessionId );
|
|
|
+
|
|
|
+ return "/student.jsp";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|