SpringMVC环境简单搭建
本文介绍使用maven搭建Spring MVC
首先先介绍MVC(Model View Controller)模式:
- Model(模型):是应用程序中用于处理应用程序数据逻辑部分。通常模型对象负责在数据库中存取数据
- View(视图):是应用程序虫用于处理数据显示的部分。通常视图是依据数据模型创建的。
- Controller(控制器):是应用程序中处理用户交互的部分。通常控制器负责从视图中读取数据,控制用户输入,并向模型发送数据。
创建Spring MVC工程
- 使用创建Maven工程创建一个Web工程
- 为工程添加基本spring的依赖
- 添加spring的配置文件 applicationContext.xml,spring-mvc.xml
- 添加对应spring的属性文件
- 在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.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环境简单搭建相关推荐
- LAMP环境简单搭建
一.简介 LAMP 是Linux Apache MySQL PHP的简写,其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行php的脚本语言.Apache是最常用 ...
- windows 程序设计_Python 在windows环境简单搭建
Python是一种计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项目的开发. 官方网站: ...
- springmvc环境搭建及实例
一. 软件环境 eclipse-jee-mars-R-win32-x86_64 jdk1.7.0_79 apache-tomcat-7.0.52 spring-framework-3.2.0.RELE ...
- 【SpringMVC入门】SpringMVC环境搭建、接收参数的几种方式、视图解析器、@ResponseBody
一.SpringMVC 简介 1.SpringMVC 中重要组件 1.1 DispatcherServlet: 前端控制器,接收所有请求(如果配置/不包含jsp) 1.2 HandlerMapping ...
- Windows下NLB(分工作组与域环境)、服务器群集简单搭建
实验 NLB(网络负载平衡)群集{工作组环境}.NLB(网络负载平衡){域环境} Cluster(服务器群集)的具体配置 {注意:本次实验采用VMWare 5.5.1版本} 首先我们来进行NLB(工作 ...
- plsql的环境与介绍:环境的搭建和plsql的简单介绍
PLSQL编程 1.环境的搭建 (1)创建一个存储表空间 SQL> conn /as sysdba Connected. SQL> create tablespace plsql data ...
- python环境的搭建以及pycharm的安装和简单配置
python环境的搭建以及pycharm的安装和简单配置 Python的环境搭建 PIP工具的使用 pip介绍 pip的使用 查看pip版本 普通安装 pip升级 指定版本安装 卸载已安装的库 列出已 ...
- CentOS7.4中Postfix邮件服务器的搭建(一)-----环境配置及简单搭建
CentOS7.4中Postfix邮件服务器的搭建(一)-----环境配置及简单搭建 实验内容: 1. 初始环境的优化 2. DNS服务器的搭建和postfix域名解析 3. Postfix服务器安装 ...
- 简单搭建一个SSM项目(一)
简单搭建一个用户管理的SSM项目框架,虽然也能用servlet+jdbc搭建更简单的,不过个人感觉工作中更多用的ssm框架项目,这里就简单用ssm来搭建需要的项目吧. 准备工具:eclipse.jdk ...
最新文章
- 关于android设备唯一区分device id的取得
- 作为程序员的你第一套房子是多少岁?多少万?
- 2018年黑龙江各口岸进口俄大豆80.3万吨 同比增长60.1%
- js记录用户访问页面和停留时间
- jQuery以JSONP的访问调用一个WCF REST服务
- php青茶什么时候拆,青茶的香味应该如何评判(天赐露)
- linux curl 编译命令,linux 编译 curl 出错
- linux boost教程,Linux上安装使用Boost入门指导
- servlet学习笔记二
- python对csv数据提取某列的某些行_python pandas获取csv指定行 列的操作方法
- 请教大家一个问题,有关于数据库的设计
- 【python】多线程小结
- 为什么我们要设定更高的目标?
- mapxtreme java_MapXtreme Java Edition 4.8使用心得(二)
- python的scipy库无法使用_scipy库内存错误
- AutoJs学习-音量键控制脚本运行
- 如何实现网页的自动登录
- 实用计算机相关日语词汇,日语分类词汇:计算机类(1)
- 新年最美表白烟花-祝大家新年快乐,表白成功
- 东软睿驰与联合电子达成战略合作,抢占国产化基础软件市场新风口
热门文章
- 计算机无法找到实达打印机,实达打印机使用方法教程
- python爬虫scrapy框架爬取网页数据_Scrapy-Python
- 重装系统后不重装matlab的解决办法
- 世界四大重要检索系统简介
- hust1344(阶层问题+暴力)
- Codeforces 698D Limak and Shooting Points (搜索)
- pythonargmaxaxis1_详解numpy的argmax的具体使用
- sql having是什么意思_sql之汇总查询
- samba 实现linux 共享,用Samba实现Linux之间的文件共享机制
- python与人工智能的关系_python和人工智能之间的关系是什么?老男孩Python人工智能...