hello springmvc
什么是Spring MVC?
Spring MVC 為展現(xiàn)層提供的基于 MVC 設(shè)計理念的優(yōu)秀的Web 框架,是目前最主流的 MVC 框架之一感昼。
Spring3.0 后全面超越 Struts2,成為最優(yōu)秀的 MVC 框架系忙。
導(dǎo)入jar包
我們基于Spring mvc框架進行開發(fā)映跟,需要依賴一下的spring jar包:
spring-aop-4.0.4.RELEASE.jar
spring-beans-4.0.4.RELEASE.jar
spring-context-4.0.4.RELEASE.jar
spring-core-4.0.4.RELEASE.jar
spring-expression-4.0.4.RELEASE.jar
spring-web-4.0.4.RELEASE.jar
spring-webmvc-4.0.4.RELEASE.jar
commons-logging-1.1.1.jar(用來打印log)
在WEB-INF目錄下新建lib文件夾,并將上面的jar包放入其中拗小。
配置文件web.xml(WEB-INF下)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 配置DispatchcerServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置Spring mvc下的配置文件的位置和名稱 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
注意: <param-value>classpath:springmvc.xml</param-value>用于配置spring mvc的配置文件的位置和名稱重罪,這里說明會新建一個springmvc.xml的配置文件。
這里的servlet-mapping表示攔截的模式,這里是“*.do”蛆封,表示對于.do結(jié)尾的請求進行攔截唇礁。
Springmvc.xml(scr下)
在src目錄下新建springmvc.xml
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置自動掃描的包 -->
<context:component-scan base-package="com.neusoft.controller"></context:component-scan>
<!-- 配置視圖解析器 如何把handler 方法返回值解析為實際的物理視圖 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value="/WEB-INF/views/"></property>
<property name = "suffix" value = ".jsp"></property>
</bean>
</beans>
<context:component-scan base-package="com.neusoft.controller"></context:component-scan>
表示spring監(jiān)聽的范圍,這里是在com.neusoft.controller下
HelloWorldController.java(com.neusoft.controller下)
package com.neusoft.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorldController {
/**
* 1\. 使用RequestMapping注解來映射請求的URL
* 2\. 返回值會通過視圖解析器解析為實際的物理視圖, 對于InternalResourceViewResolver視圖解析器惨篱,會做如下解析
* 通過prefix+returnVal+suffix 這樣的方式得到實際的物理視圖盏筐,然后會轉(zhuǎn)發(fā)操作
* "/WEB-INF/views/success.jsp"
* @return
*/
@RequestMapping("/helloworld.do")
public String hello(){
System.out.println("hello world");
return "success";
}
}
- 首先要在類的前面添加“Controller”注解,表示是spring的控制器砸讳,這里會寫一個方法hello()
2. hello方法上方有一個@RequestMapping琢融, 是用于匹配請求的路徑,比如這里匹配的請求路徑就是“http://localhost:8080/helloworld.do”簿寂,即當tomcat服務(wù)啟動后漾抬,在瀏覽器輸入這個url時,如果在這個方法打斷點了常遂,就會跳入該方法纳令。
3. 這個return的結(jié)果不是亂寫的,這個返回的字符串就是與上面springmvc.xml中進行配合的克胳,springmvc.xml中聲明了prefix和suffix平绩,而夾在這兩者之間的就是這里返回的字符串,所以執(zhí)行完這個方法后漠另,我們可以得到這樣的請求資源路徑“/WEB-INF/views/success.jsp”捏雌,這個success.jsp是需要我們新建的
index.jsp(WebContent下)
在新建success.jsp之前,我們需要有一個入口笆搓,也就是這里的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="helloworld.do">hello world</a>
</body>
</html>
當訪問index.jsp時性湿,頁面上會展示一個超鏈接,點擊超鏈后满败,url中的地址就會發(fā)生跳轉(zhuǎn)肤频,由“http://localhost:8080/springTest/index.jsp”跳轉(zhuǎn)到“http://localhost:8080/springTest/helloworld.do”,而這個url請求就會進入HelloWorld中的hello方法算墨,因為其與該方法上的“/helloworld.do”匹配着裹。
success.jsp(WEB-INF/views下)
該頁面是作為請求成功后的相應(yīng)頁面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h4>Success Page</h4>
</body>
</html>
Controller方法的返回值
可以有以下幾種:
1、返回ModelAndView
返回ModelAndView時最常見的一種返回結(jié)果米同。需要在方法結(jié)束的時候定義一個ModelAndView對象骇扇,并對Model和View分別進行設(shè)置。
@Controller
public class HelloWorldController {
@RequestMapping("/helloworld.do")
public String hello(){
System.out.println("hello world");
return "success";
}
@RequestMapping("/abc.do")
public ModelAndView abc(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","zhangsan");
modelAndView.setViewName("success");
return modelAndView;
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${username}
</body>
</html>
2面粮、返回String
1):字符串代表邏輯視圖名
真實的訪問路徑=“前綴”+邏輯視圖名+“后綴”
注意:如果返回的String代表邏輯視圖名的話少孝,那么Model的返回方式如下:
@RequestMapping("/helloworld.do")
public String hello(Model model){
model.addAttribute("username","zhangsan");
System.out.println("hello world");
return "success";
}
2):代表redirect重定向
redirect的特點和servlet一樣,使用redirect進行重定向那么地址欄中的URL會發(fā)生變化熬苍,同時不會攜帶上一次的request
案例:
@Controller
public class HelloWorldController {
@RequestMapping("/helloworld.do")
public String hello(Model model){
model.addAttribute("username","zhangsan");
System.out.println("hello world");
return "success";
}
@RequestMapping("/abc.do")
public ModelAndView abc(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","zhangsan");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/redirect.do")
public String redirect(){
return "redirect:login.do";
}
}
3):代表forward轉(zhuǎn)發(fā)
通過forward進行轉(zhuǎn)發(fā)稍走,地址欄中的URL不會發(fā)生改變袁翁,同時會將上一次的request攜帶到寫一次請求中去
案例:
@Controller
public class HelloWorldController {
@RequestMapping("/helloworld.do")
public String hello(Model model){
model.addAttribute("username","zhangsan");
System.out.println("hello world");
return "success";
}
@RequestMapping("/abc.do")
public ModelAndView abc(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","zhangsan");
modelAndView.setViewName("success");
return modelAndView;
}
@RequestMapping("/redirect.do")
public String redirect(){
return "forward:login.do";
}
}
3、返回void
返回這種結(jié)果的時候可以在Controller方法的形參中定義HTTPServletRequest和HTTPServletResponse對象進行請求的接收和響應(yīng)
1)使用request轉(zhuǎn)發(fā)頁面
request.getRequestDispatcher("轉(zhuǎn)發(fā)路徑").forward(request,response);
2)使用response進行頁面重定向
response.sendRedirect("重定向路徑");
3)也可以使用response指定響應(yīng)結(jié)果
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().println("json串");
@RequestMapping("/returnvoid.do")
public void returnvoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.getRequestDispatcher("轉(zhuǎn)發(fā)路徑").forward(request,response);
// response.sendRedirect("重定向路徑");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().println("json串");
}
以上三種返回值沒有什么重要和不重要的分別婿脸,一般來說都會使用到粱胜, 只不過有的時候使用的方式會有一些細微的差別
SpringMVC的各種參數(shù)綁定方式
- 基本數(shù)據(jù)類型(以int為例,其他類似):
Controller代碼:
@RequestMapping("saysth.do")
public void test(int count) {
}
表單代碼:
<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
</form>
表單中input的name值和Controller的參數(shù)變量名保持一致狐树,就能完成數(shù)據(jù)綁定焙压,如果不一致可以使用@RequestParam注解。需要注意的是抑钟,如果Controller方法參數(shù)中定義的是基本數(shù)據(jù)類型涯曲,但是從頁面提交過來的數(shù)據(jù)為null的話,會出現(xiàn)數(shù)據(jù)轉(zhuǎn)換的異常在塔。也就是必須保證表單傳遞過來的數(shù)據(jù)不能為null幻件。所以,在開發(fā)過程中蛔溃,對可能為空的數(shù)據(jù)绰沥,最好將參數(shù)數(shù)據(jù)類型定義成包裝類型,具體參見下面的例子贺待。
- 包裝類型(以Integer為例徽曲,其他類似):
Controller代碼:
@RequestMapping("saysth.do")
public void test(Integer count) {
}
表單代碼:
<form action="saysth.do" method="post">
<input name="count" value="10" type="text"/>
</form>
和基本數(shù)據(jù)類型基本一樣,不同之處在于狠持,表單傳遞過來的數(shù)據(jù)可以為null,以上面代碼為例瞻润,如果表單中count為null或者表單中無count這個input喘垂,那么,Controller方法參數(shù)中的count值則為null绍撞。
- 自定義對象類型:
Model代碼:
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Controller代碼:
@RequestMapping("saysth.do")
public void test(User user) {
}
表單代碼:
<form action="saysth.do" method="post">
<input name="firstName" value="張" type="text"/>
<input name="lastName" value="三" type="text"/>
......
</form>
非常簡單正勒,只需將對象的屬性名和input的name值一一匹配即可。
解決亂碼問題
web.xml里加入過濾器
<filter>
<filter-name>characterEncodingFilter</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>
<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>
RESTful架構(gòu)
RESTful架構(gòu)傻铣,就是目前最流行的一種互聯(lián)網(wǎng)軟件架構(gòu)章贞。它結(jié)構(gòu)清晰、符合標準非洲、易于理解鸭限、擴展方便箫老,所以正得到越來越多網(wǎng)站的采用铜异。RESTful(即Representational State Transfer的縮寫)其實是一個開發(fā)理念,是對http的很好的詮釋泡仗。
對url進行規(guī)范梦染,寫RESTful格式的url
非REST的url:http://...../queryItems.action?id=001
REST的url風(fēng)格:http://..../items/001
特點:url簡潔赡麦,將參數(shù)通過url傳到服務(wù)端
修改web.xml朴皆,添加DispatcherServlet的Restful配置
<servlet>
<servlet-name>springmvc-servlet-rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-servlet-rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<url-pattern>/</url-pattern>表明所有url模式為/
URL 模板模式映射
@RequestMapping(value="/ viewItems/{id}")
{×××}占位符,請求的URL可以是“/viewItems/1”或“/viewItems/2”泛粹,通過在方法中使用@PathVariable獲取{×××}中的×××變量遂铡。@PathVariable用于將請求URL中的模板變量映射到功能處理方法的參數(shù)上。
@RequestMapping("/viewItems/{id}")
public @ResponseBody viewItems(@PathVariable("id") String id) throws Exception{
}
如果RequestMapping中表示為"/viewItems/{id}"晶姊,id和形參名稱一致扒接,@PathVariable不用指定名稱。
@RequestMapping("/viewItems/{id}")
public @ResponseBody viewItems(@PathVariable String id) throws Exception{
}
多個參數(shù)
@Controller
@RequestMapping("/person")
public class PersonAction{
@RequestMapping(value="/delete/{id}/{name}")
public String delete(@PathVariable Integer id,@PathVariable String name){
System.out.println(id + " " + name) ;
return "person";
}
}
靜態(tài)資源訪問<mvc:resources>
如果在DispatcherServlet中設(shè)置url-pattern為 /則必須對靜態(tài)資源進行訪問處理帽借,否則對css珠增,js等文件的請求會被DispatcherServlet攔截。
spring mvc 的<mvc:resources mapping="" location="">實現(xiàn)對靜態(tài)資源進行映射訪問砍艾。告訴springmvc框架蒂教,描述的靜態(tài)資源,無須DispatcherServlet攔截脆荷,以及查詢的目錄凝垛。
如下是對css和js文件訪問配置:
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
上傳圖片
在頁面form中提交enctype="multipart/form-data"的數(shù)據(jù)時,需要springmvc對multipart類型的數(shù)據(jù)進行解析蜓谋。
在springmvc.xml中配置multipart類型解析器梦皮。
<!-- 文件上傳 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="80000"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
上傳圖片代碼
頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="upload.do" method="post" enctype="multipart/form-data">
<h2>文件上傳</h2>
文件:<input type="file" name="file1"/><br/><br/>
用戶名:<input type="text" name="username">
<br/><br/>
圖片:<img src="${imgpath}"/><br/><br/>
<input type="submit" value="上傳"/>
</form>
</body>
</html>
controller方法
@Controller
public class uploadController {
@RequestMapping("/upload.do")
public void doUpload(@RequestParam MultipartFile file1, HttpServletRequest request) throws IOException {
String strName = request.getParameter("username");
System.out.println(strName);
if(file1.isEmpty())
{
System.out.println("文件未上傳!");
}
else
{
//得到上傳的文件名
String fileName = file1.getOriginalFilename();
//得到服務(wù)器項目發(fā)布運行所在地址
String strFolder = request.getServletContext().getRealPath("/image")+ File.separator;
File folder = new File(strFolder);
if(!folder.exists())
{
folder.mkdir();
}
// 此處未使用UUID來生成唯一標識,用日期做為標識
String strNewFilePath = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ fileName;
String strFinalPath = strFolder + strNewFilePath;
//查看文件上傳路徑,方便查找
System.out.println(strFinalPath);
//把文件上傳至path的路徑
File localFile = new File(strFinalPath);
file1.transferTo(localFile);
request.getSession().setAttribute("imgpath", "image"+ File.separator+strNewFilePath);
}
}
}
攔截器
攔截器是用來動態(tài)攔截 action 調(diào)用的對象。它提供了一種機制可以使開發(fā)者可以定義在一個 action 執(zhí)行的前后執(zhí)行的代碼桃焕,也可以在一個 action 執(zhí)行前阻止其執(zhí)行剑肯,同時也提供了一種可以提取 action 中可重用部分的方式
HandlerInterceptor概述
在SpringMVC 中定義一個Interceptor是比較非常簡單,實現(xiàn)HandlerInterceptor接口观堂。
HandlerInterceptor接口主要定義了三個方法:
- boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handle)方法:該方法將在請求處理之前進行調(diào)用让网,只有該方法返回true,才會繼續(xù)執(zhí)行后續(xù)的Interceptor和Controller师痕,當返回值為true 時就會繼續(xù)調(diào)用下一個Interceptor的preHandle 方法溃睹,如果已經(jīng)是最后一個Interceptor的時候就會是調(diào)用當前請求的Controller方法;
2.void postHandle (HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView)方法:該方法將在請求處理之后胰坟,DispatcherServlet進行視圖返回渲染之前進行調(diào)用因篇,可以在這個方法中對Controller 處理之后的ModelAndView 對象進行操作。
3.void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex)方法:該方法也是需要當前對應(yīng)的Interceptor的preHandle方法的返回值為true時才會執(zhí)行笔横,該方法將在整個請求結(jié)束之后竞滓,也就是在DispatcherServlet 渲染了對應(yīng)的視圖之后執(zhí)行。用于進行資源清理吹缔。
簡單的一個例子:
xml需要配置:兩種配置方式(對所有的請求記性攔截虽界,對特定的請求進行攔截)
<mvc:interceptors>
<!--對所有的請求記性攔截-->
<!--<beans:bean class="com.sunp.common.interceptor.Myinterceptor"/>-->
<!--對特定的請求進行攔截-->
<mvc:interceptor>
<mapping path="/kfc/brands/brand1/*"/>
<bean class="com.sunp.common.interceptor.Myinterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
interceptors類
public class Myinterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("preHandle run!");
return true;
}
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle run!");
}
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion run!");
}
}
需求
1、用戶請求url
2涛菠、攔截器進行攔截校驗
如果請求的url是公開地址(無需登陸即可訪問的url)莉御,讓放行
如果用戶session 不存在跳轉(zhuǎn)到登陸頁面
如果用戶session存在放行撇吞,繼續(xù)操作。
<mvc:interceptors>
<!--如果配置了多個攔截器礁叔,則按順序執(zhí)行 -->
<!-- 登陸認證攔截器 -->
<mvc:interceptor>
<!-- /**表示所有url包括子url路徑 -->
<mvc:mapping path="/**"/>
<bean class="cn.itcast.ssm.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
定義攔截器牍颈,實現(xiàn)HandlerInterceptor接口,實現(xiàn)該接口提供了3個方法:
public class LoginInterceptor implements HandlerInterceptor{
//進入Handler方法之前執(zhí)行
//用于身份認證琅关,身份授權(quán)
//比如身份認證煮岁,如果認證通過表示當前用戶沒有登陸,需要此方法攔截不再往下執(zhí)行
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
//獲取請求的url
String url = request.getRequestURI();
//判斷url是否是公開地址(實際使用時將公開地址配置配置文件中)
//這里公開地址是登陸提交的地址
if(url.indexOf("login.action")>0){
return true;
}
//判斷session
HttpSession session = request.getSession();
//從session中取出用戶身份信息
String username = (String) session.getAttribute("username");
if(username != null){
//身份存在涣易,放行
return true;
}
//執(zhí)行到這里時表示用戶身份需要認證画机,跳轉(zhuǎn)到登陸頁面
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
//進入Handler方法之后,返回modelAndView之前執(zhí)行
//應(yīng)用場景從modelAndView出發(fā):將公用的模型數(shù)據(jù)(比如菜單導(dǎo)航)在這里傳到視圖新症,也可以在這里統(tǒng)一指定視圖
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
System.out.println(" LoginInterceptor..postHandle");
}
//執(zhí)行Handler完成后執(zhí)行此方法
//應(yīng)用場景:統(tǒng)一異常處理步氏,統(tǒng)一日志處理
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
System.out.println("LoginInterceptor..afterCompletion");
}
}
登錄的controller方法
@Controller
public class LoginController {
// 登陸
@RequestMapping("/login")
public String login(HttpSession session, String username, String password)
throws Exception {
// 調(diào)用service進行用戶身份驗證
// 在session中保存用戶身份信息
session.setAttribute("username", username);
// 重定向到商品列表頁面
return "redirect:/items/queryItems.action";
}
// 退出
@RequestMapping("/logout")
public String logout(HttpSession session) throws Exception {
// 清除session
session.invalidate();
// 重定向到商品列表頁面
return "redirect:/items/queryItems.action";
}
}
攔截器無法攔截jsp