最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springmvc之獲取參數(shù)的方法(必看)

 更新時(shí)間:2017年08月18日 09:24:16   投稿:jingxian  
下面小編就為大家?guī)硪黄猻pringmvc之獲取參數(shù)的方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

1、導(dǎo)包,四大核心包,一個(gè)切面包(AOP),logging,web,springmvc

2、配置文件,核心代碼如下:

web.xml

<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>
  <!-- 解決響應(yīng)亂碼 -->
  <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

<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: 首頁進(jìn)入

<body>
 <a href="hello" rel="external nofollow" >hello</a><br/>
 <a href="handle01?user=123456" rel="external nofollow" >獲取請(qǐng)求參數(shù)</a><br/>
 <a href="handle02" rel="external nofollow" >獲取請(qǐng)求頭</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/>
  <!-- 級(jí)聯(lián)屬性來封裝值 -->
  作者name;<input type="text" name="person.name"/><br/>
  作者address;<input type="text" name="person.address"/><br/>
  <input type="submit" value="保存圖書"/>
 </form>
 <hr/>
 <h2>給頁面攜帶數(shù)據(jù)</h2>
 <a href="output01" rel="external nofollow" >output01</a>
</body>

3./WEB-INF/pages 跳轉(zhuǎn)后的內(nèi)容

1).success.jsp

<body>
 <h1>成功!</h1>
 ${msg}===${reMsg}
</body> 

2).testScope.jsp

<body>
 <h1>測(cè)試數(shù)據(jù)帶在了哪個(gè)scope</h1>
 request:${requestScope.msg }<br /> 
 session:${sessionScope.msg }<br /> 
 application:${applicationScope.msg}
</body>

4.src/bean包   Author.java  

public class Author {
  private String name;
  private String address;   Book.java: 


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: 如果不加,則不能訪問

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

@Controller
public class TestParamController {
 
 /**
  * 1、直接給方法的參數(shù)位置寫上一個(gè)和請(qǐng)求帶來的參數(shù)的名字相同的變量
  * 2、這個(gè)變量就封裝的是帶來的參數(shù)的值
  * user = request.getParameter("user")
  * 3、如果沒有就是null
  * 
  * @RequestParam("user"):指定獲取哪個(gè)參數(shù)的值
  * 1、默認(rèn)發(fā)送請(qǐng)求必須帶上這個(gè)參數(shù);
  *   否則:HTTP Status 400 - Required String parameter 'user' is not present
  * 2、required=false可以設(shè)置不是必須的;沒帶null
  * 3、defaultValue="未命名"指定沒帶時(shí)的默認(rèn)值;
  *   user = request.getParameter("user");
  */
 
 @RequestMapping("/handle01")
 public String handle01(
   @RequestParam(value = "user", required = false, defaultValue = "未命名") String user) {
  System.out.println("獲取的User:" + user);
  return "success";
 }
 
 /**
  * 獲取請(qǐng)求頭;
  *   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";
 }
 
 /**
  * 獲取某個(gè)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;直接幫我們封裝頁面的值; 方便簡(jiǎn)單,少寫很多代碼,實(shí)現(xiàn)代碼分離,解耦和
  * 1、book = new Book();
  * 2、把book對(duì)象中的每一個(gè)屬性的值查出來,request.getParameter(屬性);
  * 3、把這個(gè)值設(shè)置進(jìn)去
  * 注意:因?yàn)镾pringMVC會(huì)進(jìn)行類型轉(zhuǎn)換,所以提交的數(shù)據(jù)一定要是合法的,否則400錯(cuò)誤*/
 @RequestMapping("/saveBook")
 public String handle04(Book book) {
  System.out.println("book的值:" + book);
  return "success";
 }

 @RequestMapping("/handle05")
 // pringMVC還允許我們?cè)谡?qǐng)求參數(shù)上使用原生的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 給頁面攜帶數(shù)據(jù)

@Controller //給頁面攜帶數(shù)據(jù)
public class DataOutPutController {

 /**
  * 1、返回值改為ModelAndView(包含模型數(shù)據(jù)(Model)和要去的頁面地址(View));
  *   數(shù)據(jù)放在請(qǐng)求域中;
  * 2、在請(qǐng)求參數(shù)上傳入Model、Map、ModelMap都行;給他們里面保存的數(shù)據(jù)會(huì)放在請(qǐng)求域中
  * Model、Map、ModelMap最終其實(shí)都是再有用BindingAwareModelMap;
  * 相當(dāng)于給BindingAwareModelMap中保存數(shù)據(jù)就是給請(qǐng)求域中保存
  * Model   Map
  *  ||   ||
  *  ||   \/
  *  ||  ModelMap
  *  \/   \/
  *  ExtendedModelMap【隱含模型】 extends ModelMap implements Model
  *   \/
  *  BindingAwareModelMap
  * @return
  */
 @RequestMapping("/output04")
 public String output04(ModelMap model){
  //視圖解析器會(huì)對(duì)視圖名進(jìn)行拼串
  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){
  //視圖解析器會(huì)對(duì)視圖名進(jìn)行拼串
  map.put("msg", "output02");
  System.out.println(map.getClass());
  return "testScope";
 }
 @RequestMapping("/output01")
 public ModelAndView output01(){
  //視圖解析器會(huì)對(duì)視圖名進(jìn)行拼串
  ModelAndView modelAndView = new ModelAndView();
  modelAndView.setViewName("testScope");
  modelAndView.addObject("msg", "output01");
  return modelAndView;
  
 }
}

以上這篇springmvc之獲取參數(shù)的方法(必看)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA中如何引入spring的命名空間

    IDEA中如何引入spring的命名空間

    這篇文章主要介紹了IDEA中如何引入spring的命名空間問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 詳解java中this.getClass()和super.getClass()的實(shí)例

    詳解java中this.getClass()和super.getClass()的實(shí)例

    這篇文章主要介紹了詳解java中this.getClass()和super.getClass()的實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • 詳解Spring配置文件中bean的相關(guān)屬性

    詳解Spring配置文件中bean的相關(guān)屬性

    這篇文章主要為大家詳細(xì)介紹了Spring配置文件中bean的相關(guān)屬性的知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • SpringSecurityOAuth2 如何自定義token信息

    SpringSecurityOAuth2 如何自定義token信息

    這篇文章主要介紹了SpringSecurityOAuth2 自定義token信息的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java中SynchronousQueue的底層實(shí)現(xiàn)原理剖析

    Java中SynchronousQueue的底層實(shí)現(xiàn)原理剖析

    BlockingQueue的實(shí)現(xiàn)類中,有一種阻塞隊(duì)列比較特殊,就是SynchronousQueue(同步移交隊(duì)列),隊(duì)列長(zhǎng)度為0。本文就來剖析一下SynchronousQueue的底層實(shí)現(xiàn)原理,感興趣的可以了解一下
    2022-11-11
  • scala 操作數(shù)據(jù)庫的方法

    scala 操作數(shù)據(jù)庫的方法

    這篇文章主要介紹了scala 操作數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導(dǎo)出,在線文件導(dǎo)出)

    SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導(dǎo)出,在線文件導(dǎo)出)

    這篇文章主要介紹了SpringBoot快速集成jxls-poi(自定義模板,支持本地文件導(dǎo)出,在線文件導(dǎo)出),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java算法中的歸并排序算法代碼實(shí)現(xiàn)

    Java算法中的歸并排序算法代碼實(shí)現(xiàn)

    這篇文章主要介紹了Java算法中的歸并排序算法代碼實(shí)現(xiàn),歸并排序使用的是分治思想(Divide and Conquer),分治,顧名思義,就是分而治之,是將一個(gè)大問題分解成小的子問題來解決,需要的朋友可以參考下
    2023-12-12
  • 基于Jenkins+Maven+Gitea+Nexus搭建CICD環(huán)境的方式

    基于Jenkins+Maven+Gitea+Nexus搭建CICD環(huán)境的方式

    這篇文章主要介紹了基于Jenkins+Maven+Gitea+Nexus從0到1搭建CICD環(huán)境,大家都知道Nexus是一套“開箱即用”的系統(tǒng)不需要數(shù)據(jù)庫,它使用文件系統(tǒng)加Lucene來組織數(shù)據(jù),需要的朋友可以參考下
    2022-01-01
  • Mybatis如何根據(jù)List批量查詢List結(jié)果

    Mybatis如何根據(jù)List批量查詢List結(jié)果

    這篇文章主要介紹了Mybatis如何根據(jù)List批量查詢List結(jié)果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論

武胜县| 常州市| 阜阳市| 宕昌县| 荔浦县| 连南| 梅河口市| 莎车县| 武邑县| 宜君县| 龙胜| 西城区| 清水河县| 疏附县| 郯城县| 进贤县| 庐江县| 囊谦县| 黄陵县| 张家港市| 奇台县| 通城县| 霍城县| 济源市| 大埔区| 荣成市| 奉化市| 崇州市| 阿合奇县| 金坛市| 从化市| 崇信县| 铁力市| 阳信县| 威海市| 德江县| 车致| 赫章县| 仲巴县| 鸡泽县| 静安区|