1、導(dǎo)包,四大核心包,一個(gè)切面包(AOP),logging,web,springmvc
江北網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
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" >獲取請求參數(shù)</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/> <!-- 級聯(lián)屬性來封裝值 --> 作者name;<input type="text" name="person.name"/><br/> 作者address;<input type="text" name="person.address"/><br/> <input type="submit" value="保存圖書"/> </form> <hr/> <h3>給頁面攜帶數(shù)據(jù)</h3> <a href="output01" rel="external nofollow" >output01</a> </body>

3./WEB-INF/pages 跳轉(zhuǎn)后的內(nèi)容
1).success.jsp
<body>
<h2>成功!</h2>
${msg}===${reMsg}
</body> 2).testScope.jsp
<body>
<h2>測試數(shù)據(jù)帶在了哪個(gè)scope</h2>
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è)和請求帶來的參數(shù)的名字相同的變量
* 2、這個(gè)變量就封裝的是帶來的參數(shù)的值
* user = request.getParameter("user")
* 3、如果沒有就是null
*
* @RequestParam("user"):指定獲取哪個(gè)參數(shù)的值
* 1、默認(rèn)發(fā)送請求必須帶上這個(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";
}
/**
* 獲取請求頭;
* 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;直接幫我們封裝頁面的值; 方便簡單,少寫很多代碼,實(shí)現(xiàn)代碼分離,解耦和
* 1、book = new Book();
* 2、把book對象中的每一個(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還允許我們在請求參數(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ù)放在請求域中;
* 2、在請求參數(shù)上傳入Model、Map、ModelMap都行;給他們里面保存的數(shù)據(jù)會(huì)放在請求域中
* Model、Map、ModelMap最終其實(shí)都是再有用BindingAwareModelMap;
* 相當(dāng)于給BindingAwareModelMap中保存數(shù)據(jù)就是給請求域中保存
* Model Map
* || ||
* || \/
* || ModelMap
* \/ \/
* ExtendedModelMap【隱含模型】 extends ModelMap implements Model
* \/
* BindingAwareModelMap
* @return
*/
@RequestMapping("/output04")
public String output04(ModelMap model){
//視圖解析器會(huì)對視圖名進(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ì)對視圖名進(jìn)行拼串
map.put("msg", "output02");
System.out.println(map.getClass());
return "testScope";
}
@RequestMapping("/output01")
public ModelAndView output01(){
//視圖解析器會(huì)對視圖名進(jìn)行拼串
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("testScope");
modelAndView.addObject("msg", "output01");
return modelAndView;
}
}以上這篇springmvc之獲取參數(shù)的方法(必看)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。
本文標(biāo)題:springmvc之獲取參數(shù)的方法(必看)
標(biāo)題網(wǎng)址:http://www.yijiale78.com/article34/ihojse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、用戶體驗(yàn)、網(wǎng)站維護(hù)、面包屑導(dǎo)航、網(wǎng)站改版、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)