當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringMVC环境简单搭建
生活随笔
收集整理的這篇文章主要介紹了
SpringMVC环境简单搭建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文介紹使用maven搭建Spring MVC
首先先介紹MVC(Model View Controller)模式:
創建Spring MVC工程
applicationContext.xml??
在applicationContext.xml中主要為:
- 啟動注解: <context:component-scan base-package="***" ?/>
- 加載屬性文件:<context:property-placeholder location="" />
- 連接數據源
- 添加事務
- 配置錯誤頁面
?
1 <?xml version="1.0" encoding="UTF-8"?>2 <beans xmlns="http://www.springframework.org/schema/beans"3 xmlns:security="http://www.springframework.org/schema/security"4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"6 xmlns:context="http://www.springframework.org/schema/context"7 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc"8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd10 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 11 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd 12 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 14 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd 15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 17 http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd 18 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd 19 http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd 20 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd 21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd22 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">23 24 <!-- ====================================================================================== -->25 <!-- 啟用基于注解(Annotation-based)的配置 -->26 <!-- ====================================================================================== -->27 <context:component-scan base-package="cn.telling" />28 <!-- ====================================================================================== -->29 <!-- 基于注解的事務配置 <tx:annotation-driven transaction-manager="txManager" /> -->30 <!-- ====================================================================================== -->31 <tx:annotation-driven transaction-manager="txManager" />32 <!-- ====================================================================================== -->33 <!-- 啟用Spring對@AspectJ切面配置的支持 <aop:aspectj-autoproxy /> -->34 <!-- ====================================================================================== -->35 <aop:aspectj-autoproxy />36 <!-- ====================================================================================== -->37 <!-- 加載屬性文件 -->38 <!-- ====================================================================================== -->39 <context:property-placeholder location="classpath:/properties/*.properties" />40 <!-- ====================================================================================== -->41 <!-- 配置 數據源 連接池 c3p0 -->42 <!-- ====================================================================================== -->43 <!-- 讀寫數據源 -->44 <bean id="b2b-ds-1" class="com.mchange.v2.c3p0.ComboPooledDataSource"45 destroy-method="close" lazy-init="default">46 <property name="driverClass" value="${b2b-ds-1.driver}"></property>47 <property name="jdbcUrl" value="${b2b-ds-1.url}"></property>48 <property name="user" value="${b2b-ds-1.username}"></property>49 <property name="password" value="${b2b-ds-1.password}"></property>50 <property name="initialPoolSize" value="${b2b-ds-1.initialPoolSize}"></property>51 <property name="minPoolSize" value="${b2b-ds-1.minPoolSize}"></property>52 <property name="maxPoolSize" value="${b2b-ds-1.maxPoolSize}"></property>53 <property name="maxIdleTime" value="${b2b-ds-1.maxIdleTime}"></property>54 <property name="acquireIncrement" value="${b2b-ds-1.acquireIncrement}"></property>55 <property name="idleConnectionTestPeriod" value="${b2b-ds-1.idleConnectionTestPeriod}"></property>56 <property name="acquireRetryAttempts" value="${b2b-ds-1.acquireRetryAttempts}"></property>57 <property name="breakAfterAcquireFailure" value="${b2b-ds-1.breakAfterAcquireFailure}"></property>58 <property name="maxStatements" value="${b2b-ds-1.maxStatements}"></property>59 <property name="maxStatementsPerConnection" value="${b2b-ds-1.maxStatementsPerConnection}"></property>60 <property name="testConnectionOnCheckout" value="${b2b-ds-1.testConnectionOnCheckout}"></property>61 <property name="numHelperThreads" value="${b2b-ds-1.numHelperThreads}"></property>62 </bean>63 <!-- ====================================================================================== -->64 <!-- 事務配置 -->65 <!-- ====================================================================================== -->66 <!--事務管理器 -->67 <bean id="txManager"68 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">69 <property name="dataSource" ref="b2b-ds-1" />70 </bean>71 <!-- ====================================================================================== -->72 <!-- Spring Oracle大字段處理類 配置 -->73 <!-- ====================================================================================== -->74 <bean id="nativeJdbcExtractor"75 class="org.springframework.jdbc.support.nativejdbc.C3P0NativeJdbcExtractor"76 lazy-init="true" />77 <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"78 lazy-init="true">79 <property name="nativeJdbcExtractor" ref="nativeJdbcExtractor" />80 </bean>81 <!-- ====================================================================================== -->82 <!-- 全局異常錯誤處理 -->83 <!-- ======================================================================================= -->84 <bean id="exceptionResolver"85 class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">86 <property name="defaultErrorView">87 <value>/error/error</value>88 <!-- 跳轉到error下的error頁面 -->89 </property>90 <property name="defaultStatusCode">91 <value>500</value>92 </property>93 <!-- 需要在log4j中也有對應的配置 -->94 <property name="warnLogCategory">95 <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver96 </value>97 </property>98 <property name="exceptionMappings">99 <props> 100 <prop key="java.sql.SQLException">/error/error</prop> 101 </props> 102 </property> 103 </bean> 104 </beans>?
?
spring-mvc.xml:表示Controller返回的ModelAndView的基礎上,加上目錄前綴 /WEB-INF/webPage,加上文件后綴名.jsp,由此等待下個頁面/WEB-INF/webPage/xxx.jsp。
1 <?xml version="1.0" encoding="UTF-8"?>2 3 <beans xmlns="http://www.springframework.org/schema/beans"4 xmlns:security="http://www.springframework.org/schema/security"5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"6 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"7 xmlns:context="http://www.springframework.org/schema/context"8 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc"9 xsi:schemaLocation=" 10 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 11 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 12 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd 13 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 14 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 15 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd 16 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 17 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 18 http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd 19 http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd 20 http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd 21 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd 22 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd 23 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 24 25 26 <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前后綴 --> 27 <bean id="viewResolver" 28 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 29 <property name="viewClass" 30 value="org.springframework.web.servlet.view.JstlView"></property> 31 <property name="prefix" value="/WEB-INF/webPage/" /> 32 <property name="suffix" value=".jsp" /> 33 </bean> 34 35 </beans>?
屬性文件:自行根據需要進行修改
1 b2b-ds-1.driver=oracle.jdbc.driver.OracleDriver2 b2b-ds-1.url= jdbc:oracle:thin:XXX3 b2b-ds-1.username=xxx4 b2b-ds-1.password=xxx5 b2b-ds-1.initialPoolSize=16 b2b-ds-1.minPoolSize=1 7 b2b-ds-1.maxPoolSize=3 8 b2b-ds-1.maxIdleTime=0 9 b2b-ds-1.acquireIncrement=10 10 b2b-ds-1.idleConnectionTestPeriod=0 11 b2b-ds-1.acquireRetryAttempts=3 12 b2b-ds-1.breakAfterAcquireFailure=false 13 b2b-ds-1.maxStatements=10 14 b2b-ds-1.maxStatementsPerConnection=10 15 b2b-ds-1.testConnectionOnCheckout=false 16 b2b-ds-1.numHelperThreads=10?
web.xml
1 <?xml version="1.0" encoding="UTF-8"?>2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"5 id="WebApp_ID" version="3.0">6 <display-name>xxx</display-name>7 <!-- 加載配置文件 -->8 <context-param>9 <param-name>contextConfigLocation</param-name>10 <param-value>/WEB-INF/xmlConfig/globalConfig/*.xml,11 /WEB-INF/xmlConfig/interceptConfig/*.xml</param-value>12 </context-param>13 <!-- 加載配置log4j日志 -->14 <context-param>15 <param-name>log4jConfigLocation</param-name>16 <param-value>/WEB-INF/properties/log4j.properties</param-value>17 </context-param>18 <context-param>19 <param-name>log4jRefreshInterval</param-name>20 <param-value>5000</param-value>21 </context-param>22 <!-- 過濾器,過濾頁面,進行字符編碼設置 -->23 <filter>24 <filter-name>encodingFilter</filter-name>25 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>26 <init-param>27 <param-name>encoding</param-name>28 <param-value>UTF-8</param-value>29 </init-param>30 <init-param>31 <param-name>forceEncoding</param-name>32 <param-value>true</param-value>33 </init-param>34 </filter>35 <filter-mapping>36 <filter-name>encodingFilter</filter-name>37 <url-pattern>/*</url-pattern>38 </filter-mapping>39 <filter>40 <description>sessionfilter</description>41 <display-name>sessionfilter</display-name>42 <filter-name>sessionfilter</filter-name>43 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>44 <init-param>45 <description>46 </description>47 <param-name>targetBeanName</param-name>48 <param-value>sessionfilter</param-value>49 </init-param>50 </filter>51 <filter-mapping>52 <filter-name>sessionfilter</filter-name>53 <url-pattern>/*</url-pattern>54 </filter-mapping>55 56 <!-- Log4j日志監聽 -->57 <listener>58 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>59 </listener>60 61 <!-- 監聽spring -->62 <listener>63 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>64 </listener>65 <!-- 部署applicationContext的xml文件 -->66 <servlet>67 <servlet-name>SpringMVC_FrontControl</servlet-name>68 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>69 <init-param>70 <param-name>contextConfigLocation</param-name>71 <param-value>/WEB-INF/xmlConfig/webConfig/*.xml,</param-value>72 </init-param>73 <load-on-startup>1</load-on-startup>74 </servlet>75 76 <!-- 過濾spring請求 -->77 <servlet-mapping>78 <servlet-name>SpringMVC_FrontControl</servlet-name>79 <url-pattern>*.html</url-pattern>80 </servlet-mapping>81 82 <welcome-file-list>83 <welcome-file>index.html</welcome-file>84 </welcome-file-list>85 <session-config>86 <session-timeout>30</session-timeout>87 </session-config>88 89 90 <error-page>91 <error-code>404</error-code>92 <location>/WEB-INF/404.jsp</location>93 </error-page>94 <error-page>95 <error-code>500</error-code>96 <location>/WEB-INF/500.jsp</location>97 </error-page>98 <error-page>99 <exception-type>java.lang.Exception</exception-type> 100 <location>/WEB-INF/exception.jsp</location> 101 </error-page> 102 </web-app>以上就基本將springmvc環境搭建完成了。
?
新建測試類:訪問localhost:8080/xxx/index.html,來訪問并跳轉到pages/common下的index頁面
1 @Controller 2 public class IndexController { 3 4 @RequestMapping("/index") 5 public String index(HttpServletRequest request) { 6 return "pages/common/index"; 7 } 8 9 }?
轉載于:https://www.cnblogs.com/yany/p/5209132.html
總結
以上是生活随笔為你收集整理的SpringMVC环境简单搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 神经网络入门连载
- 下一篇: 浏览器上网 (Safari Chrom