1、導包,四大核心包,一個切面包(AOP),logging,web,springmvc
2、配置文件,核心代碼如下:
web.xml
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
|
< servlet > < servlet-name >springDispatcherServlet</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:springmvc.xml</ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > <!-- Map all requests to the DispatcherServlet for handling --> < servlet-mapping > < servlet-name >springDispatcherServlet</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > <!--字符編碼的filter一定要放在最前面 --> < filter > < filter-name >CharacterEncodingFilter</ filter-name > < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > <!-- 配置encoding,告訴我們指定的編碼格式 --> < init-param > < param-name >encoding</ param-name > < param-value >utf-8</ param-value > </ init-param > <!-- 解決響應亂碼 --> < init-param > < param-name >forceEncoding</ param-name > < param-value >true</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >CharacterEncodingFilter</ filter-name > < url-pattern >/</ url-pattern > </ filter-mapping > <!-- 支持rest的filter --> < filter > < filter-name >HiddenHttpMethodFilter</ filter-name > < filter-class >org.springframework.web.filter.HiddenHttpMethodFilter</ filter-class > </ filter > < filter-mapping > < filter-name >HiddenHttpMethodFilter</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > |
springmvc.xml
1
2
3
4
5
6
7
|
< context:component-scan base-package = "com.atguigu" ></ context:component-scan > < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <!-- 視圖分析器 --> < property name = "prefix" value = "/WEB-INF/pages/" ></ property > < property name = "suffix" value = ".jsp" ></ property > </ bean > </ beans > |
index.jsp: 首頁進入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
< body > < a href = "hello" rel = "external nofollow" >hello</ a >< br /> < a href = "handle02" rel = "external nofollow" >獲取請求頭</ a >< br /> < form action = "saveBook" method = "post" > 圖書id< input type = "text" name = "id" />< br /> 圖書name< input type = "text" name = "name" />< br /> 圖書author< input type = "text" name = "author" />< br /> 圖書price< input type = "text" name = "price" />< br /> 圖書sales< input type = "text" name = "sales" />< br /> 圖書stock< input type = "text" name = "stock" />< br /> < hr /> <!-- 級聯屬性來封裝值 --> 作者name;< input type = "text" name = "person.name" />< br /> 作者address;< input type = "text" name = "person.address" />< br /> < input type = "submit" value = "保存圖書" /> </ form > < hr /> < h2 >給頁面攜帶數據</ h2 > < a href = "output01" rel = "external nofollow" >output01</ a > </ body > |
3./WEB-INF/pages 跳轉后的內容
1).success.jsp
1
2
3
4
|
< body > < h1 >成功!</ h1 > ${msg}===${reMsg} </ body > |
2).testScope.jsp
1
2
3
4
5
6
|
< body > < h1 >測試數據帶在了哪個scope</ h1 > request:${requestScope.msg }< br /> session:${sessionScope.msg }< br /> application:${applicationScope.msg} </ body > |
4.src/bean包 Author.java
1
2
3
|
public class Author { private String name; private String address; Book.java: |
1
2
3
4
5
6
7
8
9
|
public class Book { private int id; private String name; private double price; private int sales; private int stock; private Author person; private String imgPath = "static/img/default.jpg"; private String author; |
src/controller 包, HelloController.java: 如果不加,則不能訪問
1
2
3
4
5
6
7
8
9
10
11
12
|
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloController { @RequestMapping("/hello") //連接地址必須加上"/hello" public String hello(){ return "success"; } } |
TestParamController.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
68
69
70
71
72
73
74
75
76
77
78
79
|
@Controller public class TestParamController { /** * 1、直接給方法的參數位置寫上一個和請求帶來的參數的名字相同的變量 * 2、這個變量就封裝的是帶來的參數的值 * user = request.getParameter("user") * 3、如果沒有就是null * * @RequestParam("user"):指定獲取哪個參數的值 * 1、默認發送請求必須帶上這個參數; * 否則:HTTP Status 400 - Required String parameter 'user' is not present * 2、required=false可以設置不是必須的;沒帶null * 3、defaultValue="未命名"指定沒帶時的默認值; * user = request.getParameter("user"); */ @RequestMapping("/handle01") public String handle01( @RequestParam(value = "user", required = false, defaultValue = "未命名") String user) { System.out.println("獲取的User:" + user); return "success"; } /** * 獲取請求頭; * request.getHeader("User-Agent") * 注解做了下面這些事情 * @RequestHeader("User-Agent")String userAgent; * userAgent = request.getHeader("User-Agent");*/ @RequestMapping("/handle02") public String handle02( @RequestHeader(value = "User-Agent", required = false, defaultValue = "沒有的") String userAgent) { System.out.println("User-Agent:" + userAgent); return "success"; } /** * 獲取某個cookie的值; * JSESSIONID=B05C018F82AA1B0BD3845831FADFE49A * @CookieValue("JSESSIONID")String jid * 注解做了下面這些事情 * Cookie[] cookies = request.getCookies(); * for(Cookie c:cookies){ * if(c.getName().equals("JSESSIONID")){ * jid = c.getValue(); * } * }*/ @RequestMapping("/handle03") public String handle03( @CookieValue(value = "JSESSIONID", required = false, defaultValue = "hhhhh") String jid) { System.out.println("jid:" + jid); return "success"; } /*傳入POJO;直接幫我們封裝頁面的值; 方便簡單,少寫很多代碼,實現代碼分離,解耦和 * 1、book = new Book(); * 2、把book對象中的每一個屬性的值查出來,request.getParameter(屬性); * 3、把這個值設置進去 * 注意:因為SpringMVC會進行類型轉換,所以提交的數據一定要是合法的,否則400錯誤*/ @RequestMapping("/saveBook") public String handle04(Book book) { System.out.println("book的值:" + book); return "success"; } @RequestMapping("/handle05") // pringMVC還允許我們在請求參數上使用原生的ServletAPI HttpServletRequest HttpServletResponse // HttpSession public String handle05(HttpSession session, HttpServletRequest request, HttpServletResponse response) { session.setAttribute("msg", "哈哈哈"); request.setAttribute("reqMsg", "嘿嘿嘿"); return "success"; } } |
src/dataout/ DataOutPutController.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
|
@Controller //給頁面攜帶數據 public class DataOutPutController { /** * 1、返回值改為ModelAndView(包含模型數據(Model)和要去的頁面地址(View)); * 數據放在請求域中; * 2、在請求參數上傳入Model、Map、ModelMap都行;給他們里面保存的數據會放在請求域中 * Model、Map、ModelMap最終其實都是再有用BindingAwareModelMap; * 相當于給BindingAwareModelMap中保存數據就是給請求域中保存 * Model Map * || || * || \/ * || ModelMap * \/ \/ * ExtendedModelMap【隱含模型】 extends ModelMap implements Model * \/ * BindingAwareModelMap * @return */ @RequestMapping("/output04") public String output04(ModelMap model){ //視圖解析器會對視圖名進行拼串 model.addAttribute("msg","output04"); System.out.println(model.getClass()); return "testScope"; } @RequestMapping("/output03") public String output03(Model model){ model.addAttribute("msg", "output03"); System.out.println(model.getClass()); return "testScope"; } @RequestMapping("/output02") public String output02(Map< String ,Object>map){ //視圖解析器會對視圖名進行拼串 map.put("msg", "output02"); System.out.println(map.getClass()); return "testScope"; } @RequestMapping("/output01") public ModelAndView output01(){ //視圖解析器會對視圖名進行拼串 ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("testScope"); modelAndView.addObject("msg", "output01"); return modelAndView; } } |
以上這篇springmvc之獲取參數的方法(必看)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/JavaBlackHole/p/7382384.html