简单的springmvc框架

来源:互联网 发布:t95e6最新版本数据 编辑:程序博客网 时间:2024/06/09 23:42

1、新建一个WEB项目


2 、插入spring配置文件   applicationContext.xml,插入文件中有一个选项是否导入jar ,如果没有选则自己导入,我有jar自己导入



3、导入spring所需求jar


4、编辑applicationContext.xml、spring-mvc.xml文件

applicationContext.xml  建立与数据的链接

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"    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/task      http://www.springframework.org/schema/task/spring-task-3.1.xsd      http://www.springframework.org/schema/tx     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd     http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context.xsd">    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="${c3p0.driverClass}"></property>        <property name="jdbcUrl" value="${c3p0.url}"></property>        <property name="user" value="${c3p0.user}"></property>        <property name="password" value="${c3p0.password}"></property>        <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>        <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>        <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>        <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>        <property name="minPoolSize" value="${c3p0.minPoolSize}"></property>        <property name="acquireRetryDelay" value="1000"></property>        <property name="acquireRetryAttempts" value="60"></property>        <property name="breakAfterAcquireFailure" value="false"></property>    </bean>        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource"></property>    </bean>        <bean id="transationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource"></property>    </bean>        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>          <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="locations">            <list>              <value>classpath*:c3p0.properties</value>            </list>        </property>     </bean>        <bean id="defaultTransactionDefinition" class="org.springframework.transaction.support.DefaultTransactionDefinition"></bean>        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer" >        <property name="properties" ref="configProperties"></property>    </bean>      </beans>

spring-mvc.xml  

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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.xsd      http://www.springframework.org/schema/util                      http://www.springframework.org/schema/util/spring-util-3.1.xsd      http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">                         <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->   <context:component-scan base-package="org.springframework.core.serializer.annotation"></context:component-scan>   <mvc:annotation-driven>    <mvc:message-converters register-defaults="true">        <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">               <property name="prefixJson" value="false"></property>            <property name="supportedMediaTypes">                <list>                    <value>application/json</value>                    <value>text/json</value>                </list>            </property>        </bean>    </mvc:message-converters>   </mvc:annotation-driven>      <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->   <context:component-scan base-package="com.action" />   <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp"></bean>    <!-- 上传 -->    <bean id="multipartResolver"        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <property name="defaultEncoding" value="utf-8"></property>        <property name="maxUploadSize" value="10485760000"></property>        <property name="maxInMemorySize" value="40960"></property>    </bean> </beans>

文档结构路径如下: