国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

SpringMVC 學習 03 - spring mvc的獲取參數和傳遞參數

2019-11-08 20:23:05
字體:
來源:轉載
供稿:網友

工程結構

在lib下面加入jar包,jar圖片如下所示:

2,配置web.xml,也就是sPRing mvc設置啟動的servlet

[html] view plain copy<servlet>      <servlet-name>hello</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <init-param><!-- 這兩行是重新定位spring的配置文件路徑,默認是在WEB-INF下面的hello-servlet.xml -->          <param-name>contextConfigLocation</param-name>          <param-value>WEB-INF/web-main.xml</param-value>      </init-param>  </servlet>  <servlet-mapping>      <servlet-name>hello</servlet-name>      <url-pattern>*.htm</url-pattern>  </servlet-mapping>  3,書寫spring的配置文件

[html] view plain copy<?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"      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">            <!-- 開始spring mvc的注解 -->      <mvc:annotation-driven/>            <!-- 這樣根目錄下面的resource的文件(.CSS,.js等)就不會被spring的DispatchServlet進行過濾 -->      <mvc:resources location="/resources/" mapping="/resources/**"/>            <!-- 配置注解掃描的包路徑 -->      <context:component-scan base-package="cn.edu.hj.controller"></context:component-scan>            <!-- 配置action中返回的視圖配置  -->      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">          <property name="prefix" value="/WEB-INF/jsp/"></property>          <property name="suffix" value=".jsp"></property>      </bean>            <!-- 上傳文件時需要做的配置 -->      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">          <property name="maxUploadSize" value="5000000"></property>      </bean>        </beans>  

4,書寫action中的代碼

int userId = ServletRequestUtils.getIntParameter(request, "userId",-1);

ServletRequestUtils是Spring 2.0新增的工具類,可以方便地按類型獲取請求參數的值,它位于org.springframework.web.bind包中。

[java] view plain copypackage cn.edu.hj.controller;  import java.util.Map;  import javax.servlet.http.HttpServletRequest;  import org.springframework.stereotype.Controller;  import org.springframework.ui.Model;  import org.springframework.web.bind.annotation.RequestMapping;  import org.springframework.web.bind.annotation.RequestParam;    @Controller  //@RequestMapping(value = "/hello")//表示要訪問這個action的時候都要加上這個/hello路徑  public class HelloController {        /* 接收參數getParameter()的時候:  * 如果地址欄/springmvc/hello.htm上面沒有傳遞參數,那么當id為int型的時候會報錯,當id為Integer的時候值為null  * 當地址欄為/springmvc/hello.htm?id=10的時候,action中有三種接收方式  * 1、String hello(@RequestParam(value = "userid") int id),這樣會把地址欄參數名為userid的值賦給參數id,如果用地址欄上的參數名為id,則接收不到  * 2、String hello(@RequestParam int id),這種情況下默認會把id作為參數名來進行接收賦值  * 3、String hello(int id),這種情況下也會默認把id作為參數名來進行接收賦值  * 注:如果參數前面加上@RequestParam注解,如果地址欄上面沒有加上該注解的參數,例如:id,那么會報404錯誤,找不到該路徑  */      @RequestMapping(value = "/hello.htm")      public String hello(int id){//getParameter()的方式          System.out.println("hello action:"+id);  //      return "hello";          return "redirect:/index.jsp";//不能重定向web-info里面的文件,而且需要寫上絕對路徑      }            //返回頁面參數的第一種方式,在形參中放入一個map      @RequestMapping(value = "/hello1.htm")      public String hello(int id,Map<String,Object> map){          System.out.println("hello1 action:"+id);          map.put("name", "huangjie");          return "hello";      }            //返回頁面參數的第二種方式,在形參中放入一個Model      @RequestMapping(value = "/hello2.htm")      public String hello2(int id,Model model){          System.out.println("hello2 action:"+id);          model.addAttribute("name", "huangjie");          //這個只有值沒有鍵的情況下,使用Object的類型作為key,String-->string          model.addAttribute("ok");          return "hello";      }        //得到request,response,session等,只要在方法形參中聲明參數即可      @RequestMapping(value = "/hello3.htm")      public String hello3(HttpServletRequest request){          String id = request.getParameter("id");          System.out.println("hello3 action:"+id);          return "hello";      }  }  5,書寫返回視圖的jsp頁面

/WebRoot下面的index.jsp

[html] view plain copy<body>     This is my JSP page. <br>  </body>  /WebRoot/WEB-INF下面的hello.jsp

[html] view plain copy<body>    This is my hello page. <br>    name:${name } <br>    value:${string} <br>  </body>  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒲江县| 墨竹工卡县| 西吉县| 永嘉县| 松潘县| 永昌县| 泸西县| 大足县| 四会市| 巨野县| 宁蒗| 蒙阴县| 尼勒克县| 京山县| 轮台县| 东宁县| 吴川市| 巴楚县| 英吉沙县| 涟水县| 武宁县| 当涂县| 上饶县| 利津县| 海宁市| 台南县| 崇文区| 韩城市| 靖西县| 璧山县| 武川县| 漾濞| 临城县| 新田县| 蓬莱市| 晴隆县| 华亭县| 思茅市| 合阳县| 咸宁市| 洞头县|