轉載:http://blog.csdn.net/wm5920/article/details/8173480
1.web.xml 配置:
<> ></> ></> > >> ></> ></> > ></> </> <> ></> ></> </>
這樣,所有的.htm的請求,都會被DispatcherServlet處理;
初始化 DispatcherServlet 時,該框架在 web 應用程序WEB-INF 目錄中尋找一個名為[servlet-名稱]-servlet.xml的文件,並在那里定義相關的Beans,重寫在全局中定義的任何Beans,像上面的web.xml中的代碼,對應的是dispatcher-servlet.xml;當然也可以使用<init-param>元素,手動指定配置文件的路徑;
dispatcher-servlet.xml 配置:
<? = =?> < =
- =
- =
- =
- =
- =
- =
- ="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- 使Spring支持自動檢測組件,如注解的Controller
- --> =/> =
- =
- =
- =
- >
2.spring mvc處理方法支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void
ModelAndView
- ()
- ModelAndView show1(HttpServletRequest request,
- HttpServletResponse response) Exception {
- ModelAndView mav = ModelAndView();
- mav.addObject(, );
- mav;
- }
通過ModelAndView構造方法可以指定返回的頁面名稱,也可以通過setViewName()方法跳轉到指定的頁面 ,
使用addObject()設置需要返回的值,addObject()有幾個不同參數的方法,可以默認和指定返回對象的名字。
調用addObject()方法將值設置到一個名為ModelMap的類屬性,ModelMap是LinkedHashMap的子類,
具體請看類。
Model 是一個接口, 其實現類為ExtendedModelMap,繼承了ModelMap類。
model.addAttribute("pojo", pojo);
Map
@RequestMapping"/demo2/show" Map<String, String> getMap() {
- Map<String, String> map = HashMap<String, String>();
- map.put(, );
- map.put(, );
- map;
- }
在jsp頁面中可直通過${key1}獲得到值, map.put()相當於request.setAttribute方法。
寫例子時發現,key值包括 - . 時會有問題.
View 可以返回pdf excel等,暫時沒詳細了解。
String 指定返回的視圖頁面名稱,結合設置的返回地址路徑加上頁面名稱后綴即可訪問到。
注意:如果方法聲明了注解@ResponseBody ,則會直接將返回值輸出到頁面。
例如:
@RequestMapping, method = RequestMethod.GET)
- String helloWorld() {
- ;
- }
上面的結果會將文本"Hello World "直接寫到http響應流。
@RequestMapping"/welcome" public String welcomeHandler() {
- ;
- }
對應的邏輯視圖名為“center”,URL= prefix前綴+視圖名稱 +suffix后綴組成。
void 如果返回值為空,則響應的視圖頁面對應為訪問地址
@RequestMapping"/welcome" public welcomeHandler() {}
此例對應的邏輯視圖名為"welcome"。
小結:
1.使用 String 作為請求處理方法的返回值類型是比較通用的方法,這樣返回的邏輯視圖名不會和請求 URL 綁定,具有很大的靈活性,而模型數據又可以通過 ModelMap 控制。
2.使用void,map,Model 時,返回對應的邏輯視圖名稱真實url為:prefix前綴+視圖名稱 +suffix后綴組成。
3.使用String,ModelAndView返回視圖名稱可以不受請求的url綁定,ModelAndView可以設置返回的視圖名稱。
Model model,HttpServletRequest request, ModelMap map聲明變量
request.getSession().setAttribute("test", "haiwei2Session");
request.setAttribute("test", "haiwei1request");
map.addAttribute("test", "haiweiModelMap");
model.addAttribute("test", "haiweiModel");
我通過${test}這個方式取值,優先取Model和ModelMap的,Model和ModelMap是同一個東西,誰最后賦值的就取誰的,然后是request,最后是從session中獲取
第一個Controller:
package import import @Controller public IndexController {
- ()
- String index() {
- ;
- }
- }
@Controller注解標識一個控制器,@RequestMapping注解標記一個訪問的路徑(/index.htm),return "index"標記返回視圖(index.jsp);
注:如果@RequestMapping注解在類級別上,則表示一相對路徑,在方法級別上,則標記訪問的路徑;
從@RequestMapping注解標記的訪問路徑中獲取參數:
Spring MVC 支持RESTful風格的URL參數,如:
@Controller public IndexController {
- ()
- String index(<span style=></span>() String username) {
- System.out.print(username);
- ;
- }
- }
在@RequestMapping中定義訪問頁面的URL模版,使用{}傳入頁面參數,使用@PathVariable 獲取傳入參數,即可通過地址:http://localhost:8080/crm/index/tanqimin.htm 訪問;
根據不同的Web請求方法,映射到不同的處理方法:
使用登陸頁面作示例,定義兩個方法分辨對使用GET請求和使用POST請求訪問login.htm時的響應。可以使用處理GET請求的方法顯示視圖,使用POST請求的方法處理業務邏輯;
@Controller public LoginController {
- (value = , method = RequestMethod.GET)
- String login() {
- ;
- }
- (value = , method = RequestMethod.POST)
- String login2(HttpServletRequest request) {
- ).trim();
- System.out.println(username);
- ;
- }
- }
在視圖頁面,通過地址欄訪問login.htm,是通過GET請求訪問頁面,因此,返回登陸表單視圖login.jsp;當在登陸表單中使用POST請求提交數據時,則訪問login2方法,處理登陸業務邏輯;
防止重復提交數據,可以使用重定向視圖:
return
可以傳入方法的參數類型:
<strong>(value = , method = RequestMethod.POST)
- String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
- );
- System.out.println(username);
- ;
- }</strong>
可以傳入HttpServletRequest、HttpServletResponse、HttpSession,值得注意的是,如果第一次訪問頁面,HttpSession沒被創建,可能會出錯;
其中,String username = request.getParameter("username");可以轉換為傳入的參數:
@RequestMapping, method = RequestMethod.POST)
- String testParam(HttpServletRequest request, HttpServletResponse response, HttpSession session,() String username) {
- );
- System.out.println(username);
- ;
- }
使用@RequestParam 注解獲取GET請求或POST請求提交的參數;
獲取Cookie的值:使用@CookieValue :
獲取printwriter:
可以直接在Controller的方法中傳入PrintWriter對象,就可以在方法中使用:
@RequestMapping, method = RequestMethod.POST)
- String testParam(PrintWriter out, <span style=></span>() String username) {
- ;
- }
獲取表單中提交的值,並封裝到POJO中,傳入Controller的方法里:
POJO如下(User.java):
public User{
- id;
- String username;
- String password;
- }
通過表單提交,直接可以把表單值封裝到User對象中:
@RequestMapping, method = RequestMethod.POST)
- String testParam(PrintWriter out, User user) {
- ;
- }
可以把對象,put 入獲取的Map對象中,傳到對應的視圖:
<strong>(value = , method = RequestMethod.POST)
- String testParam(User user, Map model) {
- ,user);
- ;
- }</strong>
在返回的view.jsp中,就可以根據key來獲取user的值(通過EL表達式,${user }即可);
Controller中方法的返回值:
void:多數用於使用PrintWriter輸出響應數據;
String 類型:返回該String對應的View Name;
任意類型對象:
返回ModelAndView:
自定義視圖(JstlView,ExcelView):
攔截器(Inteceptors):
<strong> MyInteceptor HandlerInterceptor {
- preHandle(HttpServletRequest request, HttpServletResponse response, Object o)
- Exception {
- ;
- postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView mav)
- Exception {
- }
- afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception excptn)
- Exception {
- }</strong>
攔截器需要實現HandleInterceptor接口,並實現其三個方法:
preHandle:攔截器的前端,執行控制器之前所要處理的方法,通常用於權限控制、日志,其中,Object o表示下一個攔截器;
postHandle:控制器的方法已經執行完畢,轉換成視圖之前的處理;
afterCompletion:視圖已處理完后執行的方法,通常用於釋放資源;
在MVC的配置文件中,配置攔截器與需要攔截的URL:
<> > =
- =
- > </>
國際化:
在MVC配置文件中,配置國際化屬性文件:
< =
- =
- => </>
那么,Spring就會在項目中搜索相關的國際化屬性文件,如:message.properties、message_zh_CN.properties
在VIEW中,引入Spring標簽:<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>,使用<spring:message code="key" />調用,即可;
如果一種語言,有多個語言文件,可以更改MVC配置文件為:
< = => => > ></> ></> ></> > > </>