亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解SpringMVC 基礎(chǔ)教程 簡單入門實例

 更新時間:2016年12月19日 12:07:22   作者:SwingPyzf  
這篇文章主要介紹了詳解SpringMVC 基礎(chǔ)教程 簡單入門實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一個簡單的入門實例教程

該實例的源碼和實例中的jar

源碼:http://xiazai.jb51.net/201612/yuanma/SpringMVC_jb51.zip

所需要的jar: http://xiazai.jb51.net/201612/yuanma/SpringMVCjar_jb51.zip

另外一篇關(guān)于SpringMVC 文件上傳,多文件上傳:http://chabaoo.cn/article/100491.htm

簡單注解配置的實例:

一、創(chuàng)建項目:

1、建立新的動態(tài)web項目:

2、為項目命名為:SpringMVC_01

3、添加tomcat運行時環(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.

<!-- 配置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配置文件的路徑。

<!-- 監(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)容:

<?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標簽 導入root-context.xml,然后通過component-scan標簽掃描指定包名,讓該包下的所有Java類的spring注解生效

然后配置SpringMVC的視圖渲染解析器,讓其前綴為/page/ 后綴為.jsp  這樣能夠SpringMVC 所需要渲染的路徑能夠在/page/返回值.jsp中尋找。

<!-- 加載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:

<?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:

<?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為控制器指定哪些需要的請求。

@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。

<%@ 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)搭建完成了,可以運行項目來進行測試。

四、編寫參數(shù)的提交與傳遞:

1、編寫一個新的UserController類來假定用戶登錄,將username,password提交到Controller中進行處理,并且登陸成功后將username,password傳遞到成功的頁面。

創(chuàng)建UserController.java

創(chuàng)建/page/succ.jsp頁面 作為用戶成功登陸頁面

UserController中的代碼:

@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ù)傳遞到頁面中的方式為:

/*** 
 * 另一種參數(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:

<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、最后運行項目來進行測試:



 

OK都完成了。以上就是一個比較簡單的SpringMVC的示例搭建了。

在給出的源碼中,還有另一中直接用String當做返回值來指定顯示頁面的方法。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot輕松實現(xiàn)ip解析(含源碼)

    SpringBoot輕松實現(xiàn)ip解析(含源碼)

    IP地址一般以數(shù)字形式表示,如192.168.0.1,IP解析是將這個數(shù)字IP轉(zhuǎn)換為包含地區(qū)、城市、運營商等信息的字符串形式,如“廣東省深圳市 電信”,這樣更方便人理解和使用,本文給大家介紹了SpringBoot如何輕松實現(xiàn)ip解析,需要的朋友可以參考下
    2023-10-10
  • java文件下載代碼實例(單文件下載和多文件打包下載)

    java文件下載代碼實例(單文件下載和多文件打包下載)

    這篇文章主要介紹了java文件下載代碼實例(單文件下載和多文件打包下載),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 前端與RabbitMQ實時消息推送未讀消息小紅點實現(xiàn)示例

    前端與RabbitMQ實時消息推送未讀消息小紅點實現(xiàn)示例

    這篇文章主要為大家介紹了前端與RabbitMQ實時消息推送未讀消息小紅點實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • mybatisplus解除分頁限制的實現(xiàn)

    mybatisplus解除分頁限制的實現(xiàn)

    這篇文章主要介紹了mybatisplus解除分頁限制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 淺談java二進制、十進制、十六進制、字符串之間的相互轉(zhuǎn)換

    淺談java二進制、十進制、十六進制、字符串之間的相互轉(zhuǎn)換

    下面小編就為大家?guī)硪黄獪\談二進制、十進制、十六進制、字符串之間的相互轉(zhuǎn)換。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考,一起跟隨小編過來看看吧
    2016-06-06
  • Java如何修改JsonObject中的屬性值

    Java如何修改JsonObject中的屬性值

    這篇文章主要介紹了Java如何修改JsonObject中的屬性值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • JavaWeb的監(jiān)聽器和過濾器你了解嗎

    JavaWeb的監(jiān)聽器和過濾器你了解嗎

    這篇文章主要為大家詳細介紹了JavaWeb的監(jiān)聽器和過濾器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • JPA?通過Specification如何實現(xiàn)復雜查詢

    JPA?通過Specification如何實現(xiàn)復雜查詢

    這篇文章主要介紹了JPA?通過Specification如何實現(xiàn)復雜查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 詳解JDK9特性之JPMS模塊化

    詳解JDK9特性之JPMS模塊化

    JDK9引入了一個特性叫做JPMS(Java Platform Module System),也可以叫做Project Jigsaw。模塊化的本質(zhì)就是將一個大型的項目拆分成為一個一個的模塊,每個模塊都是獨立的單元,并且不同的模塊之間可以互相引用和調(diào)用。本文將詳細介紹JDK9特性之JPMS模塊化。
    2021-06-06
  • Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼

    Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼

    這篇文章主要介紹了Apache Commons Math3探索之多項式曲線擬合實現(xiàn)代碼,小編覺得挺不錯的,這里分享給大家,供需要的朋友參考。
    2017-10-10

最新評論