hooyantsing's Blog

第115次课程_Spring_MVC架构

字数统计: 1.8k阅读时长: 10 min
2020/02/29

源辰76班

第115次课程

2020.02.29

内容

Spring_MVC架构

项目路径:****G:\YC74\200228_Spring_MVC原理\code

web测试地址: http://127.0.0.1/注解路径 (例如: http://127.0.0.1/servlet

1.参数映射

1.1基础:参数映射和地址映射

ParamAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.yc.springmvc.web.d0229;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ParamAction {
    
    /*
     * 参数映射
     * 请求参数名必须和方法参数名一致
     * 参数类型必须兼容
     * 1.请求参数  /..?param
     * 如果名称不同@RequestParam
     * 2.地址参数  /param/param
     * spring支持地址参数@PathVariable
     */
    
    //请求参数
    @GetMapping("login")
    public String login(String user,String pwd) {
        return "user = " + user + " pwd = " + pwd;
    }

    @GetMapping(path="login1",params="username")
    public String login1(@RequestParam("username")String user,String pwd) {
        return "user = " + user + " pwd = " + pwd;
    }
    
    //地址参数
    @GetMapping("login2/{username}/{password}")
    public String login2(@PathVariable("username")String user,@PathVariable("password")String pwd) {
        return "user = " + user + " pwd = " + pwd;
    }

}

2.高级****参数映射

hall.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="saveHall" method="post">
     id:<input name="id"/><br/>
     name:<input name="name"/><br/>
     hallType<input name="hallType"/><br/>
     <input type="submit" value="提交">
</form>
</body>
</html>

beans.xml

2.1直接可以访问webapp目录下的静态文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    <!-- 加入 mvc 命名空间 -->
    <!-- SpringMVC 控制器注解驱动 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 包扫描 -->
    <context:component-scan  base-package="com.yc.springmvc"></context:component-scan>
    
    <!-- 定义视图解析器 -->
    <bean id="viewResolver"  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
         <property name="viewClass"  value="org.springframework.web.servlet.view.JstlView"/>
         <!-- 视图解析器视图前缀 -->
         <!--  webapp    /WEB-INF/jsp/hello.jsp     -->
         <property name="prefix" value="/WEB-INF/jsp/"/>
         <!-- 视图解析器视图后缀 -->
         <property name="suffix" value=".jsp"/>
     </bean>
    
    <!-- 处理静态资源请求 -->
   <!-- 用于直接访问静态资源 -->
   <mvc:default-servlet-handler/>
</beans>

ParamAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.yc.springmvc.web.d0229;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.springmvc.bean.Hall;

@RestController
public class ParamAction {
    /*
     * 高级参数映射
     * 自动装箱 请求参数名与对象属性名一致
     * 例如:Integer i = new Integer(100) -自动装箱-> Integer i = 100;
     */
    
    @PostMapping("saveHall")
    public String saveHall(Hall hall) {
        hall.setCapacity(10000);
        hall.setCinemaId(2222);
        return hall.toString();
    }
}

2.2嵌套属性

Hall.java

[Bean]

1
2
3
4
5
6
7
8
9
10
11
public class Hall {
    private Integer id;
    private String name;
    private String hallType;
    private Integer capacity;
    private Integer cinemaId;
    private Cinema cinema;

    //get set toString code ...

}

hall.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="saveHall" method="post">
     id:<input name="id"/><br/>
     name:<input name="name"/><br/>
     hallType<input name="hallType"/><br/>
     
     影院ID:<input name="cinema.id"/><br/>
     影院名称:<input name="cinema.name"/><br/>
     影院GPS:<input name="cinema.gps"/><br/>
     <input type="submit" value="提交">
</form>
</body>
</html>

2.3获取Cookie值、请求头和Servlet原生对象(request response session)

ParamAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.yc.springmvc.web.d0229;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import  org.springframework.web.bind.annotation.CookieValue;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.PathVariable;
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestHeader;
import  org.springframework.web.bind.annotation.RequestParam;
import  org.springframework.web.bind.annotation.RestController;
import com.yc.springmvc.bean.Hall;
@RestController
public class ParamAction {
     
     /*
      * 获取Cookie值
      */
     @GetMapping("cookie")
     public String cookie(@CookieValue("JSESSIONID")  String sessionid) {
          return "JSESSIONID : " + sessionid;
     }
     
     /*
      * 获取请求头
      */
     @GetMapping("header")
     public String cookie(@RequestHeader("Host") String  host,@RequestHeader("Accept") String accpet) {
          return "Host : " + host + "Accept : " + accpet;
     }
     
     /*
      * 获取原生Servlet对象 -> 请求 响应 会话
      */
     @GetMapping("servlet")
     public String servlet(HttpServletRequest  req,HttpServletResponse resp,HttpSession session) {
          return "Request : " + req +  "<br/>======================<br/>" +
                 "Response : " +  resp + "<br/>======================<br/>" +
                 "Session : " + session;
     }

}

3.数据推送

**Model **ModelAndView Map

login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html;  charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${!empty msg }">
     <font color="red">${msg}</font>
</c:if>
<form action="signin">
     username:<input name="username"/><br/>
     password:<input name="password"/><br/>
     <input type="submit"/><br/>
</form>
</body>
</html>

success.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page language="java" contentType="text/html;  charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8">
<title>Insert title here</title>
</head>
<body>
成功页面<br/>
username:${user.username }<br/>
password:${user.password }<br/>
email:${user.email }<br/>
phone:${user.phone }<br/>
</body>
</html>

UserAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.yc.springmvc.web.d0229;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import  org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import com.yc.springmvc.bean.User;
@Controller
public class UserAction {
     
     @GetMapping("tologin")
     public String tologin() {
          return "login";
     }
     
     /*
      * 实现数据推送的对象:
      * 1.Model
      * 2.ModelAndView  Model + View
      * 3.Map
      *
      */
     
     //Model
     /*@GetMapping("signin")
     public String login(User user,Model model) {
          if("yc".equals(user.getUsername()) &&  "123".equals(user.getPassword())) {
              user.setEmail("123123123@qq.com");
              user.setPhone("1313501111");
              model.addAttribute("user",user);
              return "success";
          } else {
              model.addAttribute("msg", "用户名或密码错误");
              return "login";
          }
     }*/
     
     //ModelAndView
     /*@GetMapping("signin")
     public ModelAndView login(User user) {
          ModelAndView mav = new ModelAndView("success");
          if("yc".equals(user.getUsername()) &&  "123".equals(user.getPassword())) {
              user.setEmail("123123123@qq.com");
              user.setPhone("1313501111");
              mav.addObject("user",user);
              return mav;
          } else {
              mav.addObject("msg", "用户名或密码错误");
              mav.setViewName("login");
              return mav;
          }
     }*/
     
     //Map
     @GetMapping("signin")
     public String login(User user,Map<String,Object>  map) {
          if("yc".equals(user.getUsername()) &&  "123".equals(user.getPassword())) {
              user.setEmail("123123123@qq.com");
              user.setPhone("1313501111");
              map.put("user",user);
              return "success";
          } else {
              map.put("msg", "用户名或密码错误");
              return "login";
          }
     }
}
CATALOG