目錄
1.傳參方式:
2.中文亂碼問題:
3.通路路徑:
4.定義攔截器
5.傳值方式:
6.檔案上下傳:
7.檔案下載下傳
8.将傳回的對象解析成JSON字元串
1.傳參方式:
SpringMVC的傳參方式:
四種形式
@Controller
public class ParaControlloer {
@RequestMapping("/param1")
//使用serlvet原生api來實作
public ModelAndView method(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String userName =request.getParameter("userName");
String password =request.getParameter("password");
User u=new User(userName,password);
System.out.println(u);
return null;
}
//前台需要傳入的參數名和方法定義的形參名字一緻(基于同名比對規則)
@RequestMapping("/param2")
public ModelAndView method2(String userName,
String password) throws Exception {
User u=new User(userName,password);
System.out.println(u);
return null;
}
//目前台名稱和背景形參名字不一緻的情況,需要使用@RequestParam(“前台參數名”)來注入
@RequestMapping("/param3")
public ModelAndView method3(@RequestParam("userName1")String userName,
String password) throws Exception {
User u=new User(userName,password);
System.out.println(u);
return null;
}
//使用模型傳參.(前台參數名字需要和模型中的字段名一緻.)
@RequestMapping("/param4")
public ModelAndView method4(User u) throws Exception {
System.out.println(u);
return null;
}
@RequestMapping("/delete/{delID}")
public ModelAndView method5(@PathVariable("delID")Long id) throws Exception {
System.out.println("删除ID"+id);
return null;
}
}
注意:
單頁面多表單送出時非常容易出錯 ,總是會将一份空的送出上去
2.中文亂碼問題:
當使用SpringMVC的時候,不像原生的Serlvet可以直接設定字元編碼;
需要對web.xml進行如下配置
<filter>
<filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.通路路徑:
@RequestMapping如果作用在類上,表示請求都帶上這樣的位址;
前台通路位址:localhost/test/method
4.定義攔截器
1.定義攔截器類.需要實作接口:
org.springframework.web.servlet.HandlerInterceptor
2.編寫對應的邏輯:
public class MyInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println("MyInterceptor.preHandle()");
return true;
}
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println("MyInterceptor.afterCompletion()");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println("MyInterceptor.postHandle()");
}
}
true:不攔截
false:攔截
3,将攔截器配置到主配置檔案中.
其中:mvc:mapping path="/**"可以攔截“兩級目錄”,注意此時會将css、JS等檔案統一攔下。
5.傳值方式:
1.通過serlvet原生api方式回顯資料:
@RequestMapping("/value")
public ModelAndView value(HttpServletRequest request,
HttpServletResponse response) throws Exception {
request.setAttribute("msg", "終于沒下雨了");
request.getRequestDispatcher("/hello.jsp").forward(request, response);
return null;
}
2.使用springMVC所提供的ModelAndView對象回顯内容:
@RequestMapping("/value2")
public ModelAndView value2(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mv=new ModelAndView();
mv.addObject("msg", "說什麼呢");
Map<String, Object> map=new HashMap<String, Object>();
map.put("name","小強");
mv.addObject(map);
mv.addObject("HelloWordld");
mv.addObject(new User("admin","123"));
mv.setViewName("/value2.jsp");
return mv;
}
3.直接傳回模型: (與TP5非常相似)
如果參數中沒有response對象且傳回值又沒有傳回ModelAndView對象,
這時候springMVC是不知道 處理的視圖是哪個的?
此時就需要配置視圖解析器來告訴springMVC傳回的視圖是怎樣的. 沒有配置視圖解析器之前
當請求/value3位址,去到方法後,沒有ModelAndView對象,不知道如何去找視圖,會自動給你預設的視圖/value3,相當于循環調用.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
Notes:
這裡的prefix如果是eclipse的動态項目則這樣配 ,如果是myeclipse則需要将“檔案丢進去”
@RequestMapping("/value3")
public User value3(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new User("root","123");
}
配置視圖解析器後:
當請求/value3位址,去到方法後,沒有ModelAndView對象,不知道如何去找視圖,會自動給你預設的視圖/value3,最後會使用視圖解析器幫我們傳回的位址添加字首資訊和字尾資訊,/WEB-INF/value3.jsp.
傳回視圖名:字首+傳回值+字尾
如果配置了視圖解析器,預設傳回結果都會帶上字首和字尾資訊
如果不需要加上字首和字尾' 可以傳回字元串前加上這樣的資訊.
兩種方式:重定向、請求轉發
6.檔案上下傳:
1.需要配置檔案上傳解析器.
<!-- 配置檔案上傳解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 設定上傳檔案的最大尺寸為1MB -->
<property name="maxUploadSize">
<value>#{1024*1024}</value>
</property>
</bean>
2.倒入檔案上傳的Jar包:
3.編寫核心類.
@RequestMapping("/upload")
public void upload(MultipartFile file){
System.out.println(file.getContentType());
System.out.println(file.getName());
System.out.println(file.getOriginalFilename());
System.out.println(file.getSize());
InputStream is=null;
FileOutputStream os=null;
try {
is=file.getInputStream();
os=new FileOutputStream(new File("D:/today1.png"));
IOUtils.copy(is, os);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (os!=null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is!=null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意此處使用的是:
MultipartFile 而不是FIle類
7.檔案下載下傳
與前面的Serlvet基本一緻:
8.将傳回的對象解析成JSON字元串
這個比Strus2處理友善實在太多:
1.加入Jackson的包:
2.在傳回方法的時候加上@ResponseBody
(表示我們的請求不再交給springmvc處理,會結合JSON包,将對象解析成JSON字元串)
原理:
1.判斷方法上是否有@ResponseBody标簽
2.是否有Jackson的包.
如果條件都滿足 會使用:
com.fasterxml.jackson.databind.ObjectMapper
将傳回的對象解析成JSON字元串:
String value = {"userName":"admin","password":"123"}
通過response.writer(value)