搭建基于spring MVC框架 + RESTful架构风格技术总结
生活随笔
收集整理的這篇文章主要介紹了
搭建基于spring MVC框架 + RESTful架构风格技术总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
實戰篇: 在SpringMVC框架中搭建RESTful架構風格來完成客戶端與服務器端的低耦合度、可擴展性、高并發與大數據流量的訪問。 用RESTful架構的創建步驟: 1.創建一個全新的Web工程 2.導包,導入所需要的所有第三方jar包。(springMVC+Hibernate的基本包是必須的) 3.作配置,針對不同的項目需求和不同的搭建設計,開發人員可以按照自己的編碼風格來設計符合項目開發具體 應該用多少篇配置文件。但是這幾篇配置文件是必不可少的: 3-1.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"?version="2.5"> <display-name>epetrestful</display-name> <servlet> <servlet-name>springMVCReSTful</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring/spring-action.xml</param-value> </init-param> <load-on-startup>1</load-on-startup>???? </servlet> <servlet-mapping> <servlet-name>springMVCReSTful</servlet-name> <url-pattern>/</url-pattern><!--?直接通過/去匹配路徑?--> </servlet-mapping> <!--?如果有亂碼我們則需要配置字符編碼集的過濾器來防止亂碼問題?--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>???? <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>login.html</welcome-file> </welcome-file-list> </web-app> 注: 3-2.配置spring-commoms.xml文件。(要注意我們需要將連接數據庫資源的信息用一篇外部的database.prpertise的屬性文件來 具體的配置到該spring-commoms.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" xmlns:aop="http://www.springframework.org/schema/aop"?xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName"> <!--?告知外部的database.propertise文件到spring容器中?--> <bean?class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property?name="location"></property><!--這里name屬性的值必須是location,它是在? PropertyPlaceholderConfigurer類中調用了這個location的屬性--> <list> <value>classpath:database.prppertise</value><!--?綁定外部寫的database.prppertise屬性文件的路徑?--> </list> </bean> <!--?配置數據源的驅動連接,這樣配置的優勢在于:效率得到了提高,具有pool池,我們在進行增刪 改查時就不用每次都要連接數據庫這樣的一個操作。?--> <bean?id="dataSoure"?class="org.apache.commons.dbcp.BasicDataSource"?> <property?name="driverClassName"> <value>${driverClassName}</value> </property> <property?name="url"> <value>${url}</value> </property> <property?name="username"> <value>${username}</value> </property> <property?name="password"> <value>${password}</value> </property>??? </bean> <!--?告知Hibernate的sessionFactory?-->???? <bean?id="sessionFactory"? class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property?name="dataSource"?ref="dataSoure"></property>?????? <property?name="hibernateProperties"></property> <props?key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</props>? <props?key="hibernate.show_sql">true</props> <props?key="hibernate.format_sql">true</props> <property?name="packagesToScan"> <list> <value>com.lh.model</value> </list> </property> </bean> <!--?事物管理器的配置?--> <bean?id="transactionManager"? class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property?name="sessionFactory"?ref="sessionFactory"></property>??? </bean> <!--?掃描Spring注解的配置?--> <context:component-scan?base-package="com.lh"></context:component-scan> <!--?添加注解事物支持的配置?-->???? <tx:annotation-driven?transaction-manager="transationManager"></tx:annotation-driven> </beans> 3-3.配置一篇spring-action.xml文件(其中在該配置文件中需要將上面的spring-commoms.xml的配置 文件導入到其間,這里體現了在輕量級的spring容器中spring?MVC框架是包含在spring容器之中的。) 基本配置如下: <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName" > <!--導入外部的spring-commons.xml的配置文件?-->??????? <import?resource="classpath:config/spring/spring-commons.xml"/> <!--配置spring?MVC的注解?驅動?--> <mvc:annotation-driven/> <!--配置靜態文件??-->?????? <mvc:default-servlet-handler/> </beans> 故,綜上所述:在一個用spring?MVC框架來實現RESTful架構風格的互聯網終端接口至少都需要3篇 或3篇以上的配置文件(關鍵看程序員自己的風格來決定)。 4.設計頁面(可用html,jsp) 5.書寫model類(表現層),因為后端的開發用到了Hibernate框架的(ORM映射)對象關系映射技術,故,model的對象屬性要與數據庫表的字段 相對應,于此,才能達到關系數據庫和面向對象之間的映射(即采用hibernate的注解形式將關系和對象進行綁定)。 6.書寫dao(數據訪問層)和daoImpl(接口實現類)。 7.書寫service(業務層)和serviceImpl(接口實現類)。 8.書寫單元測試,進行校驗功能是否滿足要求。 9.重要環節:導入我們在開發過程中所需要使用的所有js,css,jQuery,在一個Web項目中他們都應該放入WebRoot的同級目錄下。轉載于:https://my.oschina.net/lwhmdj0823/blog/530638
總結
以上是生活随笔為你收集整理的搭建基于spring MVC框架 + RESTful架构风格技术总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天府通怎么刷脸
- 下一篇: 饥荒海难虎鲨巢怎么找