springmvc+mybatis+velocity配置
2015-12-21 10:40
429 查看
首先说明,在java的web框架中,最难的一个步骤,就是设置配置文件,配置文件复杂繁多,而且特别容易配置错误,经过不懈努力,一步步配置好了。
1、文件目录
本工程是eclipse+maven的webapp工程
2、webx.xml配置
3、spring-servlet.xml
4、applicationContext.xml
配置完成后,再加上对于各种model controller的设置,就可以正常访问到velocity的页面。
4、task/list.vm
5、结果
其中任务名称就是循环出来的结果。
1、文件目录
本工程是eclipse+maven的webapp工程
2、webx.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index/list</welcome-file> </welcome-file-list> <!-- Spring的log4j监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 核心控制器 --> <servlet> <servlet-name>book</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet-mapping> <servlet-name>book</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 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-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 自动扫描包 --> <mvc:annotation-driven /> <context:component-scan base-package="click.youhua.*" /> <!-- velocity模板引擎 --> <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer"> <property name="resourceLoaderPath" value="WEB-INF/view"/> <property name="velocityProperties"> <props> <prop key="input.encoding">utf-8</prop> <prop key="output.encoding">utf-8</prop> </props> </property> </bean> <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="suffix" value=".vm"/> <property name="contentType" value="text/html;charset=utf-8"/> </bean> <!-- 拦截器 --> <mvc:interceptors> <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 --> <mvc:interceptor> <mvc:mapping path="/index.do/**"/> <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 --> <bean class="click.youhua.interceptor.LoginInterceptor"/> </mvc:interceptor> </mvc:interceptors> </beans>
4、applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://121.40.55.231:3306/youhua" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- configLocation:用于指定Mybatis的配置文件位置 --> </bean> <context:annotation-config /> <tx:annotation-driven /> <!-- 自动扫描和注册Mapper接口 basePackage是用来指定Mapper接口文件所在的基包, 在这个基包或其所有子包下面的Mapper接口都将被搜索到。 多个基包之间可以使用逗号或者分号进行分隔 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value=" click.youhua.mapper " /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> </beans>
配置完成后,再加上对于各种model controller的设置,就可以正常访问到velocity的页面。
4、task/list.vm
#foreach($data in $list) <tr> <td>$data.id</td> <td>$data.article_name</td> <td>ipsum</td> <td>dolor</td> <td>sit</td> </tr> #end
5、结果
其中任务名称就是循环出来的结果。
相关文章推荐
- maven学习
- maven插件maven-war-plugin的使用
- jenkins------部署项目到jboss eap下
- maven使用经验集
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- 浅析Mybatis 在CS程序中的应用
- Java Mybatis框架入门基础教程
- window下安装和配置maven环境
- Java简单实现SpringMVC+MyBatis分页插件
- 使用maven+eclipse搭建struts2开发环境
- 浅析mybatis和spring整合的实现过程
- mybatis的动态sql详解(精)
- MyBatis入门学习教程(一)-MyBatis快速入门
- Spring与Mybatis的整合方法有哪些
- 解决springmvc+mybatis+mysql中文乱码问题
- Java随手笔记8之包、环境变量和访问控制及maven profile实现多环境打包
- Spring+Mybatis+Mysql搭建分布式数据库访问框架的方法
- maven中下载jar包源码和javadoc的命令介绍
- SpringMVC+MyBatis项目总结(一)