麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

基于注解配置的Spring MVC 簡單的HelloWorld實例應用

2019-11-15 01:19:27
字體:
來源:轉載
供稿:網友
基于注解配置的SPRing MVC 簡單的HelloWorld實例應用2.1 問題

使用注解的方式重構helloworld應用案例。

2.2 方案

1. @RequestMapping注解應用

@RequestMapping可以用在類定義和方法定義上,它標明這個類或方法與哪一個客戶請求對應。實例代碼如下:

@RequestMapping("/day01")public class HelloController {    @RequestMapping("/hello.form")    public String execute() throws Exception {        return "hello";    }}

2. 開啟@RequestMapping注解映射,需要在Spring的xml配置文件進行配置,配置代碼如下:

<mvc:annotation-driven/>

3. @Controller注解應用

推薦使用@Controller注解聲明Controller組件,這樣可以使得Controller定義更加靈活,可以不用實現Controller接口,請求處理的方法也可以靈活定義。代碼如下:

@Controller@RequestMapping("/day01")public class HelloController {    @RequestMapping("/hello.form")    public String execute() throws Exception {        return "hello";    }}

4. 為了使@Controller注解生效,需要在Spring的XML配置文件中開啟組件掃描定義,并指定Controller組件所在包,配置代碼如下:

 <context:component-scan base-package="com.souvc.controller"/>

使用的環境是eclipse ,jdk7.0 ,Tomcat 7.0,導入Spring依賴包。

2.3 步驟

步驟一:新建一個新工程,工程名為SpringControler,如圖下:

步驟二:增加HelloController類

使用注解的方式,修改HelloController類,代碼如下所示:

package com.souvc.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/day01")public class HelloController {    @RequestMapping("/hello.form")    public String execute() throws Exception {        return "hello";    }}

步驟三:spring-mvc.xml的配置

在spring-mvc.xml文件中,開啟@RequestMapping注解映射以及組件掃描,代碼如下所示:

<?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" xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">         <context:component-scan base-package="com.souvc.controller"/>        <mvc:annotation-driven/>        <!-- 定義視圖解析器ViewResolver -->    <bean id="viewResolver"        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/" />        <property name="suffix" value=".jsp" />    </bean></beans>

web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 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_2_5.xsd">    <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>            org.springframework.web.servlet.DispatcherServlet        </servlet-class>        <!-- 指定Spring的配置文件 -->        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:spring-mvc.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>*.form</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list></web-app>

步驟四:測試

通過地址“http://localhost:8080/SpringControler/day01/hello.form”訪問HelloController:

源碼如下:http://yunpan.cn/cm3CqEXD9TeJf 訪問密碼 2d10
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美特一级片 | 在线成人一区二区 | 成人午夜久久 | 欧美黑人伦理 | 狠狠干天天操 | av在线免费看网站 | 一区二区三区欧美在线观看 | 欧美成人免费电影 | av一二三四区 | 日韩黄色免费电影 | 一区二区免费看 | 久久久久一本一区二区青青蜜月 | 国产日韩一区二区三区在线观看 | 亚洲国产网址 | 久久区二区 | 精品乱码久久久久 | 黄色成人av在线 | 亚洲精久| 国产高潮失禁喷水爽到抽搐视频 | 91av在线国产 | 久久国产精品区 | 久国产 | 成人福利免费在线观看 | 成年免费视频黄网站在线观看 | 中文字幕亚洲情99在线 | 久久久久亚洲国产精品 | 成人爱爱电影 | 欧美日韩免费一区二区三区 | 国产免费一级淫片 | 91精品福利视频 | 黄色7777| 黄色香蕉视频 | 黄视频网址 | 国产成人精品网站 | 黄色特级视频 | 激情小说激情图片激情电影 | 九九热视频这里只有精品 | 91九色蝌蚪国产 | 免费国产人成网站 | 草久在线| 777sesese|