国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

基于注解配置的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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乳源| 清水河县| 章丘市| 南京市| 英德市| 建始县| 合肥市| 宜君县| 通山县| 芒康县| 勐海县| 公主岭市| 吉木萨尔县| 东乡族自治县| 湛江市| 雷州市| 彭泽县| 通辽市| 常州市| 温宿县| 珠海市| 鄂托克前旗| 潍坊市| 酒泉市| 东阳市| 岚皋县| 新宁县| 庐江县| 汉寿县| 保山市| 桂平市| 和政县| 栾城县| 美姑县| 台北市| 长治市| 姜堰市| 宁乡县| 乐亭县| 营口市| 永和县|