Spring mvc如何實(shí)現(xiàn)數(shù)據(jù)處理
處理提交數(shù)據(jù)
1、提交的域名稱和處理方法的參數(shù)名一致
提交數(shù)據(jù) : http://localhost:8080/hello?name=xiaohua
處理方法 :
@RequestMapping("/hello")
public String hello(String name){
System.out.println(name);
return "hello";
}
后臺輸出 : xiaohua
2、提交的域名稱和處理方法的參數(shù)名不一致
提交數(shù)據(jù) : http://localhost:8080/hello?username=xiaohua
處理方法 :
//@RequestParam("username") : username提交的域的名稱 .
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
System.out.println(name);
return "hello";
}
后臺輸出 : xiaohua
3、提交的是一個(gè)對象
要求提交的表單域和對象的屬性名一致 , 參數(shù)使用對象即可
實(shí)體類
public class User {
private int id;
private String name;
private int age;
//構(gòu)造
//get/set
//tostring()
}
提交數(shù)據(jù) : http://localhost:8080/mvc04/user?name=xiaohua&id=1&age=15
處理方法 :
@RequestMapping("/user")
public String user(User user){
System.out.println(user);
return "hello";
}
后臺輸出 : User { id=1, name='xiaohua', age=15 }
說明:如果使用對象的話,前端傳遞的參數(shù)名和對象名必須一致,否則就是null。
數(shù)據(jù)顯示到前端
第一種 : 通過ModelAndView
我們前面一直都是如此 . 就不過多解釋
public class ControllerTest1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
//返回一個(gè)模型視圖對象
ModelAndView mv = new ModelAndView();
mv.addObject("msg","ControllerTest1");
mv.setViewName("test");
return mv;
}
}
第二種 : 通過ModelMap
ModelMap
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
//封裝要顯示到視圖中的數(shù)據(jù)
//相當(dāng)于req.setAttribute("name",name);
model.addAttribute("name",name);
System.out.println(name);
return "hello";
}
第三種 : 通過Model
Model
@RequestMapping("/ct2/hello")
public String hello(@RequestParam("username") String name, Model model){
//封裝要顯示到視圖中的數(shù)據(jù)
//相當(dāng)于req.setAttribute("name",name);
model.addAttribute("msg",name);
System.out.println(name);
return "test";
}
對比
- Model 只有寥寥幾個(gè)方法只適合用于儲存數(shù)據(jù),簡化了對于Model對象的操作和理解;
- ModelMap 繼承了 LinkedMap ,除了實(shí)現(xiàn)了自身的一些方法,同樣的繼承 LinkedMap 的方法和特性;
- ModelAndView 可以在儲存數(shù)據(jù)的同時(shí),可以進(jìn)行設(shè)置返回的邏輯視圖,進(jìn)行控制展示層的跳轉(zhuǎn)。
亂碼問題
SpringMVC給我們提供了一個(gè)過濾器 , 可以在web.xml中配置 .
修改了xml文件需要重啟服務(wù)器!
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
有些極端情況下.這個(gè)過濾器對get的支持不好 .
處理方法 :
修改tomcat配置文件 : 設(shè)置編碼!
<Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
自定義過濾器
package com.xiaohua.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* 解決get和post請求 全部亂碼的過濾器
*/
public class GenericEncodingFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//處理response的字符編碼
HttpServletResponse myResponse=(HttpServletResponse) response;
myResponse.setContentType("text/html;charset=UTF-8");
// 轉(zhuǎn)型為與協(xié)議相關(guān)對象
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// 對request包裝增強(qiáng)
HttpServletRequest myrequest = new MyRequest(httpServletRequest);
chain.doFilter(myrequest, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
//自定義request對象,HttpServletRequest的包裝類
class MyRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
//是否編碼的標(biāo)記
private boolean hasEncode;
//定義一個(gè)可以傳入HttpServletRequest對象的構(gòu)造函數(shù),以便對其進(jìn)行裝飾
public MyRequest(HttpServletRequest request) {
super(request);// super必須寫
this.request = request;
}
// 對需要增強(qiáng)方法 進(jìn)行覆蓋
@Override
public Map getParameterMap() {
// 先獲得請求方式
String method = request.getMethod();
if (method.equalsIgnoreCase("post")) {
// post請求
try {
// 處理post亂碼
request.setCharacterEncoding("utf-8");
return request.getParameterMap();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else if (method.equalsIgnoreCase("get")) {
// get請求
Map<String, String[]> parameterMap = request.getParameterMap();
if (!hasEncode) { // 確保get手動編碼邏輯只運(yùn)行一次
for (String parameterName : parameterMap.keySet()) {
String[] values = parameterMap.get(parameterName);
if (values != null) {
for (int i = 0; i < values.length; i++) {
try {
// 處理get亂碼
values[i] = new String(values[i]
.getBytes("ISO-8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
hasEncode = true;
}
return parameterMap;
}
return super.getParameterMap();
}
//取一個(gè)值
@Override
public String getParameter(String name) {
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
if (values == null) {
return null;
}
return values[0]; // 取回參數(shù)的第一個(gè)值
}
//取所有值
@Override
public String[] getParameterValues(String name) {
Map<String, String[]> parameterMap = getParameterMap();
String[] values = parameterMap.get(name);
return values;
}
}
然后在web.xml中配置這個(gè)過濾器即可!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信公眾帳號開發(fā)-自定義菜單的創(chuàng)建及菜單事件響應(yīng)的實(shí)例
本篇文章主要介紹了微信公眾帳號開發(fā)-自定義菜單的創(chuàng)建及菜單事件響應(yīng)的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12
基于Spring?Boot的線程池監(jiān)控問題及解決方案
這篇文章主要介紹了基于Spring?Boot的線程池監(jiān)控方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
BootStrap Jstree 樹形菜單的增刪改查的實(shí)現(xiàn)源碼
這篇文章主要介紹了BootStrap Jstree 樹形菜單的增刪改查的實(shí)現(xiàn)源碼,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
Java使用Jsoup解析html網(wǎng)頁的實(shí)現(xiàn)步驟
Jsoup是一個(gè)用于解析HTML文檔的Java庫,本文主要介紹了Java使用Jsoup解析html網(wǎng)頁的實(shí)現(xiàn)步驟,可以提取文本、鏈接、圖片等,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
SpringBoot 實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解
這篇文章主要介紹了SpringBoot 實(shí)現(xiàn)定時(shí)任務(wù)的方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08

