1、Spring MVC架構搭建——基於註解方式

來源:互聯網
上載者:User

本篇文章只是基於Spring MVC架構,不整合Hibernate等其它組件。所用Spring版本為spring-framework-3.2.2.RELEASE-dist。

首先把需要的包複製到lib檔案夾,為了簡單,把Spring3.2.2中libs檔案中的包都複製過去,但還需要加入其它的一些必須包才能正常運行,具體如下圖


然後,修改web.xml檔案,具體如下

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>SpringMVC</display-name>    <servlet>  <servlet-name>springMvc</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <load-on-startup>1</load-on-startup>  </servlet>    <servlet-mapping>  <servlet-name>springMvc</servlet-name>  <url-pattern>/</url-pattern>  </servlet-mapping>    <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

預設情況下,Spring MVC會到WEB-INF下去搜尋以 ‘-servlet.xml’結尾的檔案,然後解析他們。

本文建立名為springMvc-servlet.xml的xml檔案具體如下

<?xml version="1.0" encoding="UTF-8"?><!--看到下面的beans這個元素標籤沒有,必須有標籤的聲明 --><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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd    http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 對web包中的所有類進行掃描,以完成Bean建立和自動依賴注入的功能 --><context:component-scan base-package="qust.thb.*" /><!-- 支援spring3.0新的mvc註解 --><mvc:annotation-driven /> <!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的映射 -->    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/><!-- ViewResolver --><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="viewClass"value="org.springframework.web.servlet.view.JstlView" /><property name="prefix" value="/WEB-INF/" /><property name="suffix" value=".jsp" /></bean></beans>
然後,建立UserController類,具體如下

package qust.thb.usermanage.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;@Controller@RequestMapping("/user")public class UserController {@RequestMapping(value = "/getUser.do", method = RequestMethod.GET)public ModelAndView getUser() {ModelAndView mv = new ModelAndView();mv.addObject("message", "Hello World!!!");mv.setViewName("/view/hello");return mv;}}
在hello.jsp中的body中,就寫了如下內容

<body>${message}</body>
本工程的目錄結構如下


輸入地址http://localhost:8080/SpringMVC/user/getUser.do,就可以看到結果了

註:

如果你想使用@Autowired註解,那麼就必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。傳統聲明方式如下:

<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
如果想使用@Required等註解,也必須要進行相似的配置,由於一些註解我們經常使用,例如@Autowired,按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,於是spring給我們提供<context:annotation-config/>的簡化配置方式,自動幫你完成聲明。但,實現了對bean的定義和注入方式之後,還需要指定那些bean會被注入,這時我們使用註解一般都會配置掃描包路徑選項,如下面的

<context:component-scan base-package="qust.thb.*" />
該配置項其實也包含了自動注入上述processor的功能,因此當使用 <context:component-scan/> 後,就可以將 <context:annotation-config/> 移除了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.