《Java從入門到放棄》入門篇:springMVC基本用法

來源:互聯網
上載者:User

標籤:java   springmvc   

springMVC可以理解成用來做資料顯示處理的架構,主要內容就是控制器和視圖的處理。

在已經安裝了spring架構的基礎上繼續下面的步驟(我使用的MyEclipse2014)。

    1. 修改web.xml檔案

    2. 在WEB-INF目錄建立springmvc的設定檔

    3. 建立一個用來放控制器的包

    4. 在包中建立控制器類

    5. 訪問對應地址

不廢話,直接幹!!!650) this.width=650;" src="http://img.baidu.com/hi/jx2/j_0001.gif" alt="j_0001.gif" />

一、修改web.xml檔案

    <servlet>        <servlet-name>spmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>spmvc</servlet-name>        <url-pattern>*.form</url-pattern>    </servlet-mapping>

二、在WEB-INF目錄建立springmvc的設定檔

<?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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"><!-- 自動註冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter兩個bean,是spring MVC為@Controllers分發請求所必須的 --><mvc:annotation-driven></mvc:annotation-driven><!-- 自動掃描base-package對應的路徑或者該路徑的子包下面的java檔案,如果掃描到檔案中帶有@Service,@Component,@Repository,@Controller等這些註解的類,則把這些類註冊為bean --><context:component-scan base-package="com.pxy.web.controller"></context:component-scan></beans>


三、建立放控制器的包(請各位客官自己做吧,因為我不知道各位的命名有什麼嗜好哇! - -)


四、在包中建立控制器類,這兒的兩個註解(@Controller表示當前這個類是控制器類,@RequestMapping用來設定訪問路徑)

@Controller@RequestMapping("yy")public class MyController {    @RequestMapping("/go")    public String goIndex(){        return "../index.jsp";    }}

@RequestMapping的常用屬性如下,各位可以在能正常訪問後自己玩玩!

屬性名稱

說明

value 指定請求的實際地址,指定的地址可以是URI  Template 模式,該屬性用的最多
method 指定請求的method類型, GET、POST、PUT、DELETE等
consumes 指定處理請求的提交內容類型(Content-Type),如application/JSON, text/html
produces 指定返回的內容類型,僅當request要求標頭中的Accept類型中包含該指定類型時才返回
params 指定request中必須包含某些參數值,才使用該方法處理
headers 指定request中必須包含某些指定的header值,才能使用該方法處理請求


五、訪問項目中的yy/go.form資源。

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/9D/E5/wKioL1mILYmBWsyBAAAnsflyOD8395.png" title="11.png" alt="wKioL1mILYmBWsyBAAAnsflyOD8395.png" />


看,springMVC的使用是不是超級簡單,快來皈依我佛吧!650) this.width=650;" src="http://img.baidu.com/hi/jx2/j_0028.gif" alt="j_0028.gif" />


本文出自 “軟體思維” 部落格,請務必保留此出處http://softi.blog.51cto.com/13093971/1954251

《Java從入門到放棄》入門篇:springMVC基本用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.