SpringMVC环境简单搭建

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC环境简单搭建相关的知识,希望对你有一定的参考价值。

本文介绍使用maven搭建Spring MVC

首先先介绍MVC(Model View Controller)模式:

  1. Model(模型):是应用程序中用于处理应用程序数据逻辑部分。通常模型对象负责在数据库中存取数据
  2. View(视图):是应用程序虫用于处理数据显示的部分。通常视图是依据数据模型创建的。
  3. Controller(控制器):是应用程序中处理用户交互的部分。通常控制器负责从视图中读取数据,控制用户输入,并向模型发送数据。

创建Spring MVC工程

  1. 使用创建Maven工程创建一个Web工程
  2. 为工程添加基本spring的依赖
  3. 添加spring的配置文件 applicationContext.xml,spring-mvc.xml
  4. 添加对应spring的属性文件
  5. 在web.xml中配置spring(要注意配置文件所放的路径)

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.xsd
 10         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.xsd
 22         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.SimpleMappingExceptionResolver
 96             </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.OracleDriver
 2 b2b-ds-1.url= jdbc:oracle:thin:XXX
 3 b2b-ds-1.username=xxx
 4 b2b-ds-1.password=xxx
 5 b2b-ds-1.initialPoolSize=1
 6 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 }

 

以上是关于SpringMVC环境简单搭建的主要内容,如果未能解决你的问题,请参考以下文章

node 环境下简单web服务器搭建代码

SpringMVC第二课 SpringMVC框架的简单使用

Spring+SpringMvc-简单模拟微信红包

SpringMVC环境搭建 配置文件_2

各种Java项目环境搭建-文档引用汇总记录

SpringMVC_[0]概述和环境搭建