當學習一個mvc框架步驟:
1,環境搭建 實現 helloworld
2,如何傳遞參數到Controller
3,如何從控制器獲取參數
4,如何完成文件的上傳
5,如何完成驗證
6,如何處理異常
7,深入學習一些原理,和源代碼的學習
1,本筆記學習基於spring的jar包
spring-framework-3.1.0.RC2-with-docs
2,spring中的幾個概念
Dispatcher servlet 中央控制器 轉發器
Handler Mapping 映射處理器
Controller 控制器
ViewResolver 視圖轉換器
View 視圖 瞭解即可
3,環境搭建
一,創建 web project
二,拷貝 上述文件中dist文件下的所有jar包到lib目錄下
三,拷貝 common-logging 1.1.1 .jar
四,配置Dispatcher Servlet
在web.xml中 可以參考 doc下的幫助文檔
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
注意在此配置路徑的時候要使用/ 不能寫成/* /*攔截所有的請求
五,在WEB-INF下創建spring的配置文件 hello-servlet.xml
注意配置文件的名字和servlet的名字是由關聯的
配置文件的內容如下,參考幫助文檔
<?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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- 打開spring mvc支持 -->
<mvc:annotation-driven />
<!-- 掃描包的路徑 -->
<context:component-scan base-package="org.springframework.samples.petclinic.web"/>
<!-- ... -->
</beans>
六,創建Controller
@Controller
public class HelloContoller{
@RequestMappging(value={"/","/hello"})
public String hello () {
return "hello";
}
}
七,配置ViewResolvers試圖轉換器
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
視圖控制器解釋
return "hello";
"/WEB-INF/jsp/" + "hello" + ".jsp" = "/WEB-INF/jsp/hello.jsp"
4,訪問路徑配置
@RequestMapping(value = "/hello")
@RequestMapping(value = {"/","/hello"})
5,參數的傳遞
在要訪問的方法中添加參數
一,@RequestParam("userId") int id
二,@RequestParam int id
三,int id
參數傳遞到頁面
一,Map<String,Object> map
map.put("hello","haha");
${hello}
二,Model model 推薦
model.addAttribute("hello","value");
//使用object類型作爲key integer
model.addAttribute(23);
6,怎麼獲取request和response
直接添加參數
HttpServletRequest request;
HttpServletResponse response;
7,文件上傳
用到時候現查即可
Spring mvc 學習開發筆記
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
FastDFS分佈式文件系統
qq593e3b758e013
2019-02-23 12:55:03
spring mvc 環境搭建
翱翔不屈
2019-02-22 23:59:38
Spring3 MVC 深入研究
靜心紅葉
2019-02-22 21:57:30
Spring MVC 3 深入總結
wellwy
2019-02-22 18:50:55
Spring <context:annotation-config/>解析
dengxin919820
2019-02-22 18:07:42
Spring MVC參數化測試 - Junit Parameterized
sgyyz
2019-02-22 17:23:44
[Spring MVC] 中文亂碼
nealfu2014
2019-02-22 14:58:49
Spring Mvc 日誌配置
tjs469779
2019-02-22 13:47:37
spring quartz cron
我是誰VS夢
2019-02-22 13:45:46
Thinkphp5 開發 OA 辦公系統 - 開發語言選型
樂兔CRM
2018-12-07 21:12:02
Spring MVC之異常處理
勇敢的飛石
2018-12-03 21:14:23
Spring思維導圖,讓Spring不再難懂(mvc篇) 頂 原 薦
java思維導圖
2018-12-02 21:22:40
Spring MVC 通過切面,實現超靈活的註解式數據校驗 原 薦
DiamondFsd
2018-12-02 21:18:01
Spring AOP 實現業務和異常日誌記錄實戰
it林工
2018-09-20 02:32:32
Spring MVC全註解配置 - 無web.xml
sgyyz
2018-09-12 07:02:10