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 }
时间: 2024-10-03 14:25:29

SpringMVC环境简单搭建的相关文章

springmvc4环境简单搭建和定时任务

之前复制粘贴创建了几个ssm的项目,然而回头让自己写的时候还是一头雾水,究其原因是spring的陌生.仅仅是写过几个helloworld而已.而且是照着写.我都不知道springmvc到底需要多少jar,都用来干嘛.所以,接下来要用心看spring原理了. 最近由于有定时任务的需求,简单搭建了一个springmvc4的helloworld. ide采用的是idea,当然eclipse应该也是没问题的,因为都是maven项目. 1.创建好maven结构. 可以通过new->project->ma

FFmpeg Windows 开发环境简单搭建 教程

本文来自:http://www.itechzero.com/ffmpeg-windows-develop-environment-simply-set-up-tutorial.html FFmpeg 是一个优秀的程序库,开源.跨平台并且使用者众多,可以使用在 Windows 和 Linux 等平台下,本文将简单讲解 Windows 平台下 FFmpeg 开发环境的简单搭建过程. 平台:Windows 8.1 专业版 + Visual Studio 2013 打开 FFmpeg 官网下载页面,直接

springMVC环境的搭建

一.创建一个web项目springMVC,修改编码方式:utf-8. 二.在web-INF下lib导入所需.jar包.(本次加入包有限只为基本环境使用) 三.环境搭建于测试 3.1在src下建包ckx.spring.mvc.controller 在包下new一个Controller测试类TestController.java代码如下: package ckx.spring.mvc.controller; import org.springframework.stereotype.Controll

第一次课:springMVC环境的搭建

一.程序结构 二.设置步骤 1.创建一个web项目mvc1,拷贝jar包到lib文件夹中.下载地址:http://pan.baidu.com/s/1i4ty2UT 2.配置web.xml文件: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http:

spring入门(五)【springMVC环境搭建】

springMVC作为spring的一个WEB组件,是一个MVC的思想,减少了WEB开发的难度,现介绍springMVC环境的搭建,具体的原理放在后面介绍.用过框架的朋友都知道要在WEB项目中使用一个框架,必须要引入这个框架,和其他框架的引用方式一样,springMVC的引入方式是通过DispatcherServlet,那么我们就要在web.xml中配置此servlet,在lib目录下我已经把用到的jar包全部导入,下面看我的web.xml文件的配置, <?xml version="1.0

SpringMVC环境搭建--从简入手

一看到SpringMVC是什么东西?很多人第一个想知道,他能干嘛!怎么用?其实我自己的理解Spring MVC就是,Spring框架中的一个拓展,我们平时开发网站不用其他的框架,本身java Web自带的就有我们熟悉的Servlet对吧.Spring MVC分解开就是Model.View.Controller,Spring帮我们做了这三个东西.就是我们熟悉的MVC模式嘛! 1.帮我们如何去管理Model(如User类的set和get的管理). 2.帮我们如何传数据到View层.(JSP等) 3.

(1)Jenkins Linux环境下的简单搭建

(1)Jenkins Linux环境下的简单搭建 Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. ----百度百科 这是一款基于Java开发的工具.种种原因,最近刚开始接触,决定研究一下.Jenkins的搭建方法不止一种,一下就是个人总结的其中一种,文章内容比较浅显,不足之处,欢迎指正. 首先,所需要准备的工具JDK.Maven.资料上显示JDK版本最好高于1.7,并没有研究1.7以下版本,所谓"没有实际调研,就没有发言权",在此就不做过多

plsql的环境与介绍:环境的搭建和plsql的简单介绍

PLSQL编程 1.环境的搭建 (1)创建一个存储表空间 SQL> conn /as sysdbaConnected. SQL> create tablespace plsql datafile '/u01/oracle/oradata/ORCL/plsql01.dbf' size 1G; Tablespace created. (2)创建PLSQL用户SQL> create user plsql identified by plsql default tablespace plsql;

SpringMVC环境搭建(xml版本)

SpringMVC是Web层框架,它隶属于Spring框架,只是Spring这个庞大框架下的一个小模块,用于替换Servlet 1.下载jar包(20) http://zhidao.baidu.com/link?url=guH_VTC2FKGftWBtdCW_AU_z9t5QgyGqlr-DEwkryPCNhhlySr9wJACJVJu3hRiib50j_GNFkQNxZr4-k2v3z5jRpS1poxR3YZeb5dfThuS我是根据这个网址找到一个压缩包,解压后libs下会发现很多jar包