SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

时间:2022-12-26 23:16:49
序言:

搭建框架是一件比较爽歪歪的事,正所谓:“工欲善其事必先利其器”,配一把好的砍柴刀是非常必要的。

所以整合一个架构新一点的MVC项目,目的:时常提醒框架构思的重要性,不要局限在某一个模块,只有大局观有了,局部很好了解。

建议:

温馨提醒:eclipse编码最好全局为UTF-8,这样项目也是跟着UTF-8。

 

1,框架版本:JDK1.8   Spring-4.2.5.RELEASE   hibernate-5.0.8.Final   

太新的就不试了,和有些地方还是有冲突,尽量选择稳定版,冲突上排错不好调整。

 

2,准备工具:myeclipse和eclipse  ide 随意。本人选择eclipse4.6.2 。

  这里我把eclipse.ini贴出来,我的目的是搭建一个强大且有扩展性的。

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

第一个指定jdk,16行到19行版本,最下面的是内存大小,一般8G的机子MX是1G,MS一般256,512,如果后期真要拿来用,运行内存可以在run config tomcat里再加,在installed jre 里再追加内存,都可以。

 

2.1  :eclipse有了,我讲下maven,在我看来maven只是一个很简单的管理插件。

maven:jar包管理仓库,方便引用可以打包,项目结构也比经典的WEB项目要清晰。

1:先弄清楚版本对应,我常用的对应是:jdk1.6对3.2.5 , jdk1.7对3.3.3 ,  jdk1.8对3.3.9,maven配置变量完毕后,还要修改下conf下配置文件。

2:不要默认C盘user  .M2下的repository,找着很费劲。

maven配置:现在开始配置远程仓库和本地磁盘存储位置,配置好了这个,以后随便走哪里,拷贝一个配置文件替换可以了。找到maven目录下的conf文件夹下的settings.xml 修改为:

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

2个重点:一个是远程仓库,二是本地存放管理的jar包,路径可以按照自己需要修改。

 

上面的配置没有在eclipse指定是起不到任何作用的:

 

如果preferences里没有maven插件,那编辑器老了,下载一个较新的版本,用老版本安装maven插件也是可以的。

一个指向,一个引用本地磁盘和远程仓库。

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

 

 

3,开始搭建:

eclipse + maven 都准备好了,开始搭建:

右键new project --- >  other

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

 选择骨架,还是自定义,我一般是骨架(勾选create a simple project),因为maven是配置好了的,很方便。

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

选择骨架后:

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

搭建完成后的样子,这里不需要自己添加web.xml,用JAVAEE TOOLS 的 Generate Deployement Descriptor Stub,自动生成。

生成完毕:

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

右键项目,把java build path 换成指向的jdk1.8

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

且java编译 java complier 保持1.8

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

project facets 修改

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

 

4,修改web.xml(改变了web版本,必须修改)

 

 SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

修改为3.0,保存后自动下载引用版本编译。

 

5,配置pom.xml 

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.yugh.test</groupId>
<artifactId>SpringMvcTest</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<!-- POM文件重构于2016-03-28 by- yugh -->
<dependencies>
<!-- 新增导入包为pom引用包(版本最新) -->
<!-- https://mvnrepository.com/ -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
<!-- https://mvnrepository.com/ -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!-- https://mvnrepository.com/ -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
<!-- c3p0数据源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5-pre10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.4</version>
</dependency>
<!-- 和上述有区分 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-xml-provider</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.2</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.8.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.9.0</version>
</dependency>
<!-- hibernate end -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.28</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
<!-- Json 支持包 ezmorph 正常1.0.6 降低版本1.0.4 -->
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jdom/jdom -->
<!-- xml 转 json 用到的支持包 by yugh 08-09 -->
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMvcTest</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>nexus distribution snapshot repository</name>
<url>http://maven.apache.org</url>
<!-- POM文件重构于2016-03-28 by- yugh -->
</snapshotRepository>
</distributionManagement>
</project>

 

  

 

 

6,新建applicationContext.xml   跳转文件,数据源加载文件,log4j文件。

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

 Spring文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop
="http://www.springframework.org/schema/aop"
xsi:schemaLocation
="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 属性文件位置 -->
<bean id="annotationPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:init.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy
-method="close">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 请求超时时间 -->
<property name="checkoutTimeout" value="30000" />
<!-- 每60秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->
<property name="idleConnectionTestPeriod" value="30" />
<!-- 连接数据库连接池最大空闲时间 -->
<property name="maxIdleTime" value="30" />
<!-- 连接池初始化连接数 -->
<property name="initialPoolSize" value="5" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->
<property name="acquireIncrement" value="5" />
</bean>
<!-- 配置hibernate的SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入数据源 相关信息看源码 -->
<property name="dataSource" ref="dataSource" />
<!-- hibernate配置信息 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!-- 开启二级缓存 ehcache -->
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.cache.provider_configuration_file_resource_path}
</prop>
</props>
</property>
<!-- 扫描hibernate注解配置的entity -->
<property name="packagesToScan" value="com.yugh" />
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置事务增强处理Bean,指定事务管理器 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<!-- 配置详细事务处理语义 -->
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="load*" propagation="SUPPORTS" read-only="true" />
<!-- 其他采用默认事务方式 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- Spring aop事务管理 -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut id="transactionPointcut"
expression
="execution(* org.yugh.services.*.*(..))" />
<!-- 指定在txAdvice切入点应用txAdvice事务增强处理 -->
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>
</beans>

数据源我换成c3p0了,pom.xml里的阿里的druid可以不用引用。

哪些需要再新增的,在spring里自定义添加即可。

 

数据源init.properties

#mysql database setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/bit?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = false
hibernate.hbm2ddl.auto = update
hibernate.cache.use_second_level_cache = false
hibernate.cache.use_query_cache = false
hibernate.current_session_context_class=thread
hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory
hibernate.cache.provider_configuration_file_resource_path = ehcache.xml

我测试用的mysql数据库。

 

日志文件log4j.properties

 

 ### set log levels ###  INFO
log4j.rootLogger = INFO,stdout,E

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.logger.org.quartz=INFO
log4j.appender.E= org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =logs/mylogs.log
log4j.appender.E.DatePattern=yyyy-MM-dd'.log'
log4j.appender.E.Threshold =INFO
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss}[%c] [%t\:%r] - [%p] %m%n

  

 

跳转文件,springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc
="http://www.springframework.org/schema/mvc"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 启用spring mvc 注解 -->
<!-- 扫描注解 -->
<context:component-scan base-package="org.yugh.controller" />
<!-- <mvc:annotation-driven /> -->
<!-- 处理请求时返回json字符串的中文乱码问题 -->
<!-- <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value>
</list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> -->

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean name="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>

 

 最后是web.xml文件

 

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)SpringMVC框架详细整合(Spring4+HIbernate5+Maven)
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
3 <display-name>SpringMvcTest</display-name>
4 <context-param>
5 <param-name>contextConfigLocation</param-name>
6 <param-value>classpath:applicationContext.xml</param-value>
7 </context-param>
8 <context-param>
9 <param-name>log4jConfigLocation</param-name>
10 <param-value>classpath:log4j.properties</param-value>
11 </context-param>
12 <filter>
13 <filter-name>encodingFilter</filter-name>
14 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
15 <async-supported>true</async-supported>
16 <init-param>
17 <param-name>encoding</param-name>
18 <param-value>UTF-8</param-value>
19 </init-param>
20 </filter>
21 <filter-mapping>
22 <filter-name>encodingFilter</filter-name>
23 <url-pattern>/*</url-pattern>
24 </filter-mapping>
25 <listener>
26 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
27 </listener>
28 <listener>
29 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
30 </listener>
31 <servlet>
32 <servlet-name>springmvc</servlet-name>
33 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
34 <init-param>
35 <param-name>contextConfigLocation</param-name>
36 <param-value>classpath:springmvc-servlet.xml</param-value>
37 </init-param>
38 <load-on-startup>1</load-on-startup>
39 <async-supported>true</async-supported>
40 </servlet>
41 <servlet-mapping>
42 <servlet-name>springmvc</servlet-name>
43 <url-pattern>/</url-pattern>
44 </servlet-mapping>
45 <session-config>
46 <session-timeout>20</session-timeout>
47 </session-config>
48 <welcome-file-list>
49 <welcome-file>/index.jsp</welcome-file>
50 </welcome-file-list>
51 </web-app>
View Code

 

 

项目搭建完成,初次搭建完成后install一下,拉下repo1的jar包,方便后期写代码的引用。

 

7,简单的跳转实现

 

1:index.jsp

web.xml里设置的初始页是index.jsp

 

 1 <form id="myform" action="${ctx}/yugh/testPage" method="post">
2 <table cellpadding="0" cellspacing="0" border="0" class="tab_case_01" >
3 <tr>
4 <td colspan="1" style="text-align: right;">
5 <input type="button" id="seache" value="测试跳转" onclick="subData();" class="button icon edit" />
6 </td>
7 </tr>
8 </table>
9 </div>
10 </form>

 

 

一个简单的跳转按钮,提交看下面

1 function subData() {
2 $("#myform").attr("action","${ctx}/yugh/testPage");
3 $("#myform").submit();
4 }

 

 

2:controller类

 

/**
* @author: YuGH
* @e_mail:
* @name: TestController.java
* @creation: 2017年6月23日 上午10:36:46
* @description:测试控制类
* @version: 1.0
*/
@Controller
@RequestMapping("/yugh")
public class TestController {

@RequestMapping("/testPage")
public String controllerTest(ModelMap map){
String runTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
map.addAttribute("date", runTime);
System.out.println("进入controller里,当前时间:"+runTime);
return "testPage/test";
}
}

  

 

 8,测试查看

 

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

SpringMVC框架详细整合(Spring4+HIbernate5+Maven)

 

 

9,总结 :WEB端已经搭建完毕,新项目搭建主要是熟练整合,我继续拿来写后台。