1. 程式人生 > 其它 >SpringMVC框架(一)

SpringMVC框架(一)

SpringMVC最核心:DispatcherServlet

SpringMVC環境搭建:

結構:

過程:

  1.導包

  2.宣告SpringMVC核心Servlet:org.springframework.web.servlet.DispatcherServlet

    宣告Spring配置檔案的路徑:

      1.可以自己宣告

      2.預設值:/WEB-INF/springDispatcherServlet-servlet.xml

  3.新建Spring配置檔案

    (註解方式)

    1.掃描器:<context:component-scan base-package="com.maya"></context:component-scan>

    2.配置檢視解析器,設定字首字尾

    3.開啟SpringMVC註解驅動:<mvc:annotation-driven></mvc:annotation-driven>

    4.編寫控制器類,宣告方法

    5.執行頁面

程式碼:

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>test_easyui2</display-name>
 4 
 5 <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
 6     <servlet>
 7         <servlet-name>springDispatcherServlet</servlet-name>
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9         <!-- 初始化引數,如果不寫有預設值,/WEB-INF/springDispatcherServlet-servlet.xml -->
10         <init-param>
11             <param-name>contextConfigLocation</param-name>
12             <param-value>classpath:spring-mvc.xml</param-value>
13         </init-param>
14         <!-- 值大於0時,啟動伺服器時載入,數字越小,優先順序越高 -->
15         <load-on-startup>1</load-on-startup>
16     </servlet>
17 
18     <!-- Map all requests to the DispatcherServlet for handling -->
19     <servlet-mapping>
20         <servlet-name>springDispatcherServlet</servlet-name>
21         <url-pattern>/</url-pattern>
22     </servlet-mapping>
23 </web-app>

spring-mvc.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 9 
10     <!-- <bean id="/helloworld" class="com.maya.controller.HelloWorldController"></bean>
11     
12     spring2.5之前的配置方式
13     將SpringBean的名字作為對映路徑的請求
14     <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
15     處理介面卡,將請求對映給conntroller類
16     <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
17     檢視解析器
18     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
19      -->
20      
21      <!-- 註解方式 -->
22      <!-- 掃描器 -->
23     <context:component-scan base-package="com.maya"></context:component-scan>
24     <!-- 檢視解析器 -->
25     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26         <property name="prefix" value="/page/"></property>
27         <property name="suffix" value=".jsp"></property>
28     </bean>
29     <!-- 開啟springmvc註解驅動 -->
30     <mvc:annotation-driven></mvc:annotation-driven>
31     
32 </beans>

兩種處理方式類:

 1 package com.maya.controller;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.web.servlet.ModelAndView;
 7 import org.springframework.web.servlet.mvc.Controller;
 8 
 9 //只能處理單一請求
10 public class HelloWorldController implements Controller {
11 
12     @Override
13     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
14         ModelAndView mav=new ModelAndView();
15         mav.setViewName("page/success.jsp");
16         
17         mav.addObject("message","測試,屬性message");
18         //以上方法等同於request.setAttribute(arg0, arg1);
19         return mav;
20     }
21 
22 }

註解方式:

 1 package com.maya.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.servlet.ModelAndView;
 6 
 7 @Controller
 8 public class TestController {
 9     
10     @RequestMapping("/testHelloWorld")
11     public ModelAndView testHelloWorld(){
12         ModelAndView mav=new ModelAndView();
13         mav.addObject("msg", "測試屬性msg");
14         mav.setViewName("success");
15         return mav;
16     }
17 }

jsp頁面:

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <div>
11         <span>spring 2.5 之前</span>
12         <a href="helloworld">傳送門</a>
13     </div>
14     <div>
15         <span>基於註解</span>
16         <a href="testHelloWorld">傳送門2</a>
17     </div>
18 </body>
19 </html>
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h1>成功!!!</h1>
11     ${message }
12     ${msg }
13 </body>
14 </html>

常用註解:

@Controller  宣告控制器類

@RequestMapping  宣告對映的請求

@RequestParam  引數繫結

@PathVariable  繫結路徑引數

@ResponseBody  將方法的返回值直接返回

@SessionAttribute  將指定的物件放到session

@ModelAttribute