一個簡單的入門實例教程
該實例的源碼和實例中的jar
所需要的jar: SpringMVCjar.zip
另外一篇關(guān)于SpringMVC 文件上傳,多文件上傳:http://www.ythuaji.com.cn/article/85142.html
簡單注解配置的實例:
一、創(chuàng)建項目:
1、建立新的動態(tài)web項目:
2、為項目命名為:SpringMVC_01
3、添加tomcat運(yùn)行時環(huán)境依賴庫 如果是MyEclipse的話創(chuàng)建web項目時就不需要此步驟
右鍵項目,點擊Build Path->Add Librares:
添加完后會多出tomcat 的 Servlet包
4、最后添加Spring及SpringMVC所需要的jar,我添加以下jar到項目中
二、配置文件:
1、首先在web.xml中配置一個DispatcherServlet,并通過<servlet-mapping>指定需要攔截的url。 下面xml中配置一個攔截.html為后綴的url.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<!-- 配置Spring MVC DispatcherServlet --> < servlet > < servlet-name >MVC</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > <!-- 初始化參數(shù) --> < init-param > <!-- 加載SpringMVC的xml到 spring的上下文容器中 --> < param-name >contextConfigLocation</ param-name > < param-value > /WEB-INF/classes/mvc*.* </ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > <!-- 配置DispatcherServlet所需要攔截的 url --> < servlet-mapping > < servlet-name >MVC</ servlet-name > < url-pattern >*.html</ url-pattern > </ servlet-mapping > |
先配置一個servlet 然后 加載SpringMVC的xml文件到Spring的上下文中。然后配置servlet-mapping,servlet-name為剛剛的servlet中的配置的name,然后指定要攔截的url為*.html
2、配置Spring的上下文監(jiān)聽器,并且指定Spring的xml配置文件的路徑。
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- 監(jiān)聽spring上下文容器 --> < listener > < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > <!-- 加載spring的xml配置文件到 spring的上下文容器中 --> < context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:root-context.xml</ param-value > </ context-param > |
這里指定的路徑classpath為 項目編譯后的classes文件中。
最終web.xml文件內(nèi)容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app version = "3.0" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> < display-name ></ display-name > <!-- 監(jiān)聽spring上下文容器 --> < listener > < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > <!-- 加載spring的xml配置文件到 spring的上下文容器中 --> < context-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:root-context.xml</ param-value > </ context-param > <!-- 配置Spring MVC DispatcherServlet --> < servlet > < servlet-name >MVC</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > <!-- 初始化參數(shù) --> < init-param > <!-- 加載SpringMVC的xml到 spring的上下文容器中 --> < param-name >contextConfigLocation</ param-name > < param-value > /WEB-INF/classes/mvc*.* </ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > <!-- 配置DispatcherServlet所需要攔截的 url --> < servlet-mapping > < servlet-name >MVC</ servlet-name > < url-pattern >*.html</ url-pattern > </ servlet-mapping > < welcome-file-list > < welcome-file >index.html</ welcome-file > </ welcome-file-list > </ web-app > |
3、創(chuàng)建SpringMVC所需要的xml文件和applicationContext的xml文件,這里由于第一步中配置的servlet中init-param所需要加載的格式為:mvc*.* 就是去尋找為mvc開頭的文件所以創(chuàng)建SpringMVC的xml文件時必須要有mvc開頭,我命名為:mvc-context.xml,并且按照context-param中的配置,將applicationContext文件命名為:root-context.xml;
4、配置mvc-context.xml:
首先通過import標(biāo)簽 導(dǎo)入root-context.xml,然后通過component-scan標(biāo)簽掃描指定包名,讓該包下的所有Java類的spring注解生效
然后配置SpringMVC的視圖渲染解析器,讓其前綴為/page/ 后綴為.jsp 這樣能夠SpringMVC 所需要渲染的路徑能夠在/page/返回值.jsp中尋找。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- 加載Spring的全局配置文件 --> < beans:import resource = "root-context.xml" /> <!-- SpringMVC配置 --> <!-- 通過component-scan 讓Spring掃描org.swinglife.controller下的所有的類,讓Spring的代碼注解生效 --> < context:component-scan base-package = "org.swinglife.controller" ></ context:component-scan > <!-- 配置SpringMVC的視圖渲染器, 讓其前綴為:/page/ 后綴為.jsp 將視圖渲染到/page/<method返回值>.jsp中 --> < beans:bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix = "/page/" p:suffix = ".jsp" > </ beans:bean > |
最后mvc-context.xml和root-context.xml為:
mav-context.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans:beans xmlns = "http://www.springframework.org/schema/mvc" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:beans = "http://www.springframework.org/schema/beans" xmlns:p = "http://www.springframework.org/schema/p" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 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.xsd"> <!-- 加載Spring的全局配置文件 --> < beans:import resource = "root-context.xml" /> <!-- SpringMVC配置 --> <!-- 通過component-scan 讓Spring掃描org.swinglife.controller下的所有的類,讓Spring的代碼注解生效 --> < context:component-scan base-package = "org.swinglife.controller" ></ context:component-scan > <!-- 配置SpringMVC的視圖渲染器, 讓其前綴為:/ 后綴為.jsp 將視圖渲染到/page/<method返回值>.jsp中 --> < beans:bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix = "/page/" p:suffix = ".jsp" > </ beans:bean > </ beans:beans > |
root-context.xml:
1
2
3
4
5
6
7
8
9
10
11
|
<? 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> </ beans > |
三、編寫Controller
1、創(chuàng)建org.swinglife.controller的package,用來存放Controller類,接著新建HomeController.java,用來編寫首頁的Controller
2、使用注解@Controller將HomeController類定義為一個Controller,并且在方法中通過@RequestMapping(“value”)來指定所需要訪問的路徑或者方法名。 SpringMVC可以通過一個@Controller注解將一個POJO轉(zhuǎn)化為處理請求的控制器,通過@RequestMapping為控制器指定哪些需要的請求。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Controller public class HomeController { /*** * 首頁 返回至/page/home.jsp頁面 * @return */ @RequestMapping ( "index" ) public ModelAndView index(){ //創(chuàng)建模型跟視圖,用于渲染頁面。并且指定要返回的頁面為home頁面 ModelAndView mav = new ModelAndView( "home" ); return mav; } } |
方法中定義了ModelAndView對象,通過該對象指定所需要渲染的視圖為home最后返回ModelAndView 將頁面渲染到home.jsp中。
3、最后在WebContent目錄中 創(chuàng)建/page/home.jsp使SpringMVC能夠?qū)ふ也秩驹擁撁嬉晥D。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030" > < title >home</ title > </ head > < body > < h2 >spring mvc 實例</ h2 > </ body > </ html > |
現(xiàn)在一個完整的SpringMVC的模式已經(jīng)搭建完成了,可以運(yùn)行項目來進(jìn)行測試。
四、編寫參數(shù)的提交與傳遞:
1、編寫一個新的UserController類來假定用戶登錄,將username,password提交到Controller中進(jìn)行處理,并且登陸成功后將username,password傳遞到成功的頁面。
創(chuàng)建UserController.java
創(chuàng)建/page/succ.jsp頁面 作為用戶成功登陸頁面
UserController中的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
@Controller public class UserController { /*** * 用戶登陸 * <p>注解配置,只允許POST提交到該方法 * @param username * @param password * @return */ @RequestMapping (value= "login" ,method=RequestMethod.POST) public ModelAndView login(String username,String password){ //驗證傳遞過來的參數(shù)是否正確,否則返回到登陸頁面。 if ( this .checkParams( new String[]{username,password})){ //指定要返回的頁面為succ.jsp ModelAndView mav = new ModelAndView( "succ" ); //將參數(shù)返回給頁面 mav.addObject( "username" ,username); mav.addObject( "password" , password); return mav; } return new ModelAndView( "home" ); } /*** * 驗證參數(shù)是否為空 * @param params * @return */ private boolean checkParams(String[] params){ for (String param:params){ if (param== "" ||param== null ||param.isEmpty()){ return false ; } } return true ; } |
首先指定@Controller,然后指定@RequestMapping為login方法;
需要注意的是這次@RequestMapping中指定了頁面方法模式必須為POST模式否則將無法訪問。其次value參數(shù)指定訪問路徑。
并且在login方法中設(shè)定帶參,參數(shù)為表單中的name屬性。
然后通過ModelAndView的 addObject方法將參數(shù)加入到request中,這樣則能夠在返回的頁面中顯示這些參數(shù)。
在此之外還有其他將參數(shù)傳遞到頁面中的方式為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/*** * 另一種參數(shù)傳遞的形式 * 通過request來處理請求過來的參數(shù)。 * @param username * @param password * @param request * @return */ @RequestMapping (value= "login" ,method=RequestMethod.POST) public ModelAndView login(String username,String password,HttpServletRequest request){ request.setAttribute( "username" , username); request.setAttribute( "password" , password); return new ModelAndView( "succ" ); } |
以上這種方式則是直接通過將參數(shù)加入到request中來使用。
2、編寫succ.jsp頁面跟表單頁面:
succ.jsp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<body> <h2>登陸</h2> username:${username } <p> password:${password } </body> form: <form action= "login.html" method= "post" > username:<input type= "text" name= "username" /> <p> password:<input type= "password" name= "password" /> <p> <input type= "submit" value= "submit" /> </form> |
3、最后運(yùn)行項目來進(jìn)行測試:
OK都完成了。以上就是一個比較簡單的SpringMVC的示例搭建了。
在給出的源碼中,還有另一中直接用String當(dāng)做返回值來指定顯示頁面的方法。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/swingpyzf/article/details/8904205