Freemark与SpringMVC的整合
整合springMVC的配置,web.xml的配置如下
[html]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
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">
contextConfigLocation
classpath:applicationContext-.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
springmvc
.action
index.jsp
2、FreeMarker与SpringMVC整合,首先,在springmvc的配置文件普通视图之前,加入freemarker的视图。SpringMVC的配置文件,
springmvc.xml
[html]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
20
UTF-8
UTF-8
yyyy-MM-ddHH:mm:ss
HH:mm:ss
0.####
true,false
true
auto_detect
UTF-8
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
org.springframework.web.servlet.view.freemarker.FreeMarkerView
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
3、Spring相关配置文件的加载
3.1db.properties
[java]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/student
jdbc.username=root
jdbc.password=1234
3.2applicationContext-dao.xml
[html]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
destroy-method="close">
3.3applicationContext-transaction.xml
[html]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
3.4applicationContext-service.xml
[html]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://wwww.baiyuewang.net.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
4、Mybatis相关的配置文件
4.1sqlMapConfig.xml
[html]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
PUBLIC"-//mybatis.org//DTDConfig3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
4.2NavMapper.xml
[html]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
PUBLIC"-//mybatis.org//DTDMapper3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
SELECT
FROMnavWHEREid=#{id}
SELECT
FROM
nav
insertintonav(name)
values(#{name})
updatenavsetname=#{name}
whereid=#{id}
deletefromnavwhereid=#{id}
4.3NavMapper.Java与NavMapper.xml
5、log4j相关的配置文件
[html]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
#Globalloggingconfiguration
log4j.rootLogger=DEBUG,stdout
#Consoleoutput...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p[%t]-%m%n
6、关于页面的放置
7、NavController的实现
[java]viewplaincopyprint?在CODE上查看代码片派生到我的代码片
importjava.util.List;
importjava.util.Map;
importjavax.annotation.Resource;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
importcom.entity.Nav;
importcom.service.NavService;
@Controller
@RequestMapping("/navController")
publicclassNavController{
@Resource
privateNavServicenavService;
publicvoidsetNavService(NavServicenavService){
this.navService=navService;
}
@RequestMapping("/jsp")
publicStringtoJspPage(){
return"index";
}
@RequestMapping("/freemark")
publicStringtoFreemarkPage(Mapmodel){
List |
|