首页 > 其他分享 >springmvc+mybatis+velocity配置

springmvc+mybatis+velocity配置

时间:2023-03-05 10:03:28浏览次数:40  
标签:www http springmvc spring springframework velocity mybatis org schema


首先说明,在java的web框架中,最难的一个步骤,就是设置配置文件,配置文件复杂繁多,而且特别容易配置错误,经过不懈努力,一步步配置好了。

1、文件目录

springmvc+mybatis+velocity配置_web app


本工程是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、结果

springmvc+mybatis+velocity配置_spring mvc_02


其中任务名称就是循环出来的结果。


标签:www,http,springmvc,spring,springframework,velocity,mybatis,org,schema
From: https://blog.51cto.com/u_15990596/6101112

相关文章

  • SSM框架-SpringMVC学习日记1
    SpringMVC概述SpringMVC的特点:轻量级,简单易学高效,基于请求响应的MVC框架与Spring兼容性好,无缝结合约定优于配置功能强大:RESTful、数据验证、格式化、......
  • SSM框架-SpringMVC学习日记2
    新建一个SpringMVC程序1、新建一个Moudle,springmvc-02-hello,添加web的支持!2、确定导入了SpringMVC的依赖!3、配置web.xml ,注册DispatcherServlet<?xmlversion=......
  • mybatis缓存
    一级缓存  二级缓存   二级缓存相关配置  mybatis缓存查询的顺序 ......
  • SpringMVC:文件上传下载如何实现?
      一、文件下载如果在响应时候没有设置响应头中的Content-Disposition属性,则会使用默认值inline,此时客户端访问静态资源的时候,能解析显示的就会解析显示,不能解析......
  • SpringMVC:各个响应中的数据流转如何实现?
    依赖request、session、application对象进行数据流转。 一、普通流转直接从三大对象(request、session、application)中存入、取出数据。例子:此响应器会把数......
  • springmvc整合thymeleaf之helloword
    版本说明:代码地址:https://gitee.com/joy521125/ssm-senior.git  thymeleaf分支;基于https://gitee.com/joy521125/ssm-senior.gitmaster分支修改而来;1.加入jar包:1......
  • mybatis动态标签——sql标签
    mapper接口EmpgetEmpById(@Param("id")Integerid); mapper.xml<!--sql片段:可以记录一段sql,在需要用的地方使用include标签进行引用--><......
  • mybatis动态标签——foreach批量添加和删除
    <!--【foreach标签】collection:设置要循环的数组或集合item:用一个字符串表示数组或集合中的每一个数据separator:设置每次循环的数据之间的分隔符......
  • mybatis动态标签——choose、when、otherwise
    <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><m......
  • mybatis动态标签——trim
    <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><m......